I need to be able to decrypt values using OpenSSL that were generated using Mcrypt under PHP.
I have this working, with the exception that the key used to encrypt them was ascii.
The following is my code that demonstrates a working case where OpenSSL can decrypt the value encrypted with Mcrypt, when the key is an MD5.
<?php
$message = 'test';
$key = md5('Quigibo');
$iv = openssl_random_pseudo_bytes(0);
$encrypted = mcrypt_encrypt(
MCRYPT_BLOWFISH,
$key,
$message,
MCRYPT_MODE_ECB,
$iv
);
$decrypted = openssl_decrypt(
$encrypted,
'bf-ecb',
$key,
OPENSSL_RAW_DATA | OPENSSL_NO_PADDING,
$iv
);
$trimDecrypted = rtrim($decrypted);
var_export(
[
'Original message' => $message,
'Encrypted' => bin2hex($encrypted),
'Decrypted' => $decrypted,
'Trim decrypted' => $trimDecrypted,
'Message length' => mb_strlen($message, '8bit'),
'Decrypted length' => mb_strlen($decrypted, '8bit'),
'Message == decrypted' => $message === $trimDecrypted
]
);
However, if you change $key
to be the value "Quigibo" (as opposed to an MD5 hash of that value) the encrypted value cannot be decoded with OpenSSL.
Is there a form of encoding I can apply to the ASCII key prior to use with OpenSSL such that it will correctly decrypt the value?