PHP
no longer supports mcrypt
. I have to make an OpenSSL alternative that has the exact same output as I only have access to half the code base. My attempts have all failed. As you can see below OS doesn't match MC. I've tried different $methods
and combinations of OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING
.
Where am I going wrong?
const n = "\n";
$text= 'hello my friends';
$method = 'AES-128-CBC';
$key = base64_decode('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA');
$text = base64_decode('MDEyMzQ1Njc4OUFCQ0RFRgAAAAYxMjM0NTYxMjMDAwM=');
$size = openssl_cipher_iv_length($method);
$iv = substr($key, 0, $size);
// MCRYPT METHOD
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
mcrypt_generic_init($module, $key, $iv);
$mcrypt = mcrypt_generic($module, $text);
// OPENSSL METHOD
$method = 'AES-128-CBC';
$openssl = openssl_encrypt($text, $method, $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
echo "MC: " . base64_encode($mcrypt) . n;
echo "OS: " . base64_encode($openssl) . n;
/*
MC: 9+gMhSSAHhJ4g4rdjwP02YQJTfU2qEThBco+W9ob9UU=
OS: Qsz5HitF4X+2DV48wh7ExCjWjGEOAl88MKXk/g24Z/I=
*/