I have a php program that stores encrpyted data in the database using openssl_encrypt.
I need to be able to decrypt this data in mysql to perform queries on it.
The problem is that im not sure what the value of my key should be in the AES_DECRYPT function of mysql.
My php code uses a base64 key that decrypts to a non ascii value.
The code base64 encodes the encrypted value and adds the IV at the end, so in mysql i decode and split this. All of that is working fine, but when i go to decrypt it returns null. presumably because i dont know how to put the key in correctly.
Ive tried UNHEX("key")
FROM_BASE64("key")
etc
function encrypt($data)
{
if (!$data) {
return null;
}
$key = "lvvs9pxoekc54zsBpsp+/H1235BdjsAscdRRAxr47uw="; //this is not my real key
$encryption_key = base64_decode($key);
$hex = bin2hex($encryption_key);
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
$encrypted = openssl_encrypt($data, 'aes-256-cbc', $encryption_key, 0, $iv);
return base64_encode($encrypted . '::' . $iv);
}
In mysql i am doing this
AES_DECRYPT(
FROM_BASE64(SUBSTRING_INDEX(
UPPER(
CAST(
FROM_BASE64(member_ssn)
AS CHAR(1000)
)
), '::', 1))
, '*KEY* what do i need to put here?') as decrpyted_string