skip to Main Content

I have encryption code as below & it was working in PHP version 5.6. But it is not working in PHP version 7.2.

**

Warning : Use of undefined constant MCRYPT_RIJNDAEL_128 – assumed
‘MCRYPT_RIJNDAEL_128’ (this will throw an Error in a future version of
PHP)

**

Any alternative of this?

$serviceid="2951";
$secretkey = "fQ5FHy0qzM6ljp97";

function pkcs5_pad ($text, $blocksize)
{
  $pad = $blocksize - (strlen($text) % $blocksize);
  return $text . str_repeat(chr($pad), $pad);
}

function encrypt($plaintext, $key) {
    $plaintext = pkcs5_pad($plaintext, 16);
    $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaintext, MCRYPT_MODE_ECB);
    $ciphertext_base64 = base64_encode($ciphertext);
    $ciphertext_base64; 
}
$data = $serviceid;
$auth= encrypt($data ,$secretkey); 

Value of encryption is AGchNk2xOnHHxhgYv02XJw==

I tried with below code in PHP 7.2

function encrypt($plaintext, $key) 
{
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-128-cbc'));
$encrypted = openssl_encrypt($plaintext, 'aes-128-cbc', $key, 0, $iv);
return base64_encode($encrypted . '::' . $iv);
}

but output keeps changing

2

Answers


  1. Chosen as BEST ANSWER

    I have found solution of my query. Please check below

    function encrypt($plaintext,$key)
    {
        return base64_encode(openssl_encrypt($plaintext, 'aes-128-ecb', $key, OPENSSL_RAW_DATA));
    }
    

  2. As you can see into documentation mcrypt_encrypt is REMOVED from PHP 7.2.0

    Here an SO question for best alternative

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search