I tried to migrate my encryption from mcrypt to openssl but the decryption keeps failing. The code Below shows my encryption function. I placed the var_dump in the encryption function to verify decryption with identical variables works, which it does not.
I tried the following changes to the code, none worked:
base64_decode($encrypted)- just out of curiosity
base64_encode($encrypted) - all these variants with options set to:0(default value),OPENSSL_RAW_DATA,OPENSSL_ZERO_PADDING
function encryptString($data){
$key = "1A534";
do{
$iv = random_bytes(100);
}while(strpos($iv,"|Z|")!==false);
$encrypted = openssl_encrypt($data,"aes-256-gcm",$key,false,$iv);
var_dump(openssl_decrypt($encrypted,"aes-256-gcm",$key,OPENSSL_RAW_DATA,$iv));
$output = $iv . "|Z|" . $encrypted;
$output = base64_encode($output);
return $output;
}
PHP Version is 7.0.5-1+deb.sury.org~trusty+1
OpenSSL Version is OpenSSL 1.0.2g 1 Mar 2016
Why keeps openssl_decrypt returning false? I successfully encrypted with the same variables just the line above.