There are 2 problems:
- MCrypt uses zero padding, while Openssl uses PKCS # 7 by default
- Openssl requires the input string to have the correct length (a multiple of the length of the block)
To solve this problem:
- add OPENSSL_ZERO_PADDING flag to openssl_encrypt / openssl_decrypt
- if the length of the input string is not a multiple of the length of the block, add the null characters "\ 0" to the input string [aka chr (0)];
In doing so, this should solve the problem:
// key/iv in ASCII binary data, $str base64 function decrypt_stuff($key, $str, $iv) { // $plaintext_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($str), MCRYPT_MODE_CBC, $iv); $plaintext_dec = openssl_decrypt(base64_decode($str), "aes-256-cbc", $key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv); return $plaintext_dec; } // key/iv in ascii binary data, $str ascii function encrypt_stuff($key, $str, $iv) { // $ciphertext = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_CBC, $iv)); if (($l = (strlen($str) & 15)) > 0) { $str .= str_repeat(chr(0), 16 - $l); } $ciphertext = base64_encode(openssl_encrypt($str, "aes-256-cbc", $key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv)); return $ciphertext; }
source share