As mcrypt_encrypt is removed from PHP 8, I need to convert the following code to using openssl_encrypt
function encryptValue($input,$sKey)
{
$key = hex2bin($sKey);
$block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$pad = $block - (strlen($input) % $block);
$input .= str_repeat(chr($pad), $pad);
$encrypted_text = bin2hex(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $input, MCRYPT_MODE_ECB));
return $encrypted_text;
}
I have it replaced by the following:
function encryptData($input, $sKey)
{
$key= hex2bin($sKey);
$encrypted_text = bin2hex(openssl_encrypt($input, 'AES-256-ECB', $key, OPENSSL_RAW_DATA));
return $encrypted_text;
}
As openssl does default padding, I did not pad the plaintext like in the original code. I believe the secret key is being converted to binary. If I leave out the key conversion from both code, then I get the same encrypted text. But if I use the key conversion, then the encrypted texts are different.
If I remove the below code from both, then both returns the same encrypted text.
$key= hex2bin($sKey);
But without this, it will not work with the external API.