mcrypt replacement by AES-128 in PHP 7

Problem:

My client has mcrypt code that running on PHP 5.6 but this code deprecated and cannot use in PHP 7

Known:

My mcrypt code is shown as below:

<?php

$teks = "Prasangka Baik";
$key = "IniLoRahasiaSaya";
$vi =  "minumkopisusuair";
echo mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $teks, MCRYPT_MODE_CBC, $vi);

?>

Request:

How to replace the mcrypt with openssl_enrypt but must output same encryption string

Answer:

Just use this code below:

<?php

$teks = "Prasangka Baik";
$key = "IniLoRahasiaSaya";
$vi =  "minumkopisusuair";

$str_len = mb_strlen($teks, '8bit');
$pad_len = 16 - ($str_len % 16);
$teks .= str_repeat(chr(0), $pad_len);
echo openssl_encrypt($teks, "AES-128-CBC", $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $vi);

?>

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *