So i'm just doing some basic data encryption on my mysql tables. I followed the guidelines found here https://dev.mysql.com/doc/refman/5.6/en/encryption-functions.html#function_aes-encrypt
But i'm running into an issue. While i know i can just use aes_decrypt in the mysql query to decrypt the data. I want to also have the ability for php to do so itself.
I've gotten this part working. If MySQL does the very basic AES_ENCRYPTION like so
INSERT INTO tablename (dataset) VALUES (AES_ENCRYPT('testvalue','mysecretphrase'))
I'm able to decrypt this with php like so
openssl_decrypt(base64_encode($dR['dataset']), 'aes-128-ecb', 'mysecretphrase')
My problem shows up when i use the recommended UNHEX(SHA2('mysecretphrase',512)) that MySQL mentions in the url above.
The php sha* functions i tried using and can confirm that they both generate the same string as MySQLs sha2()
openssl_digest('mysecretphrase', 'sha512')
// AND
hash('sha512', 'mysecretphrase')
And lastly to work around the UNHEX() that mysql uses, after some research I turns out that PHP hex2bin == unhex http://www.php.net/manual/en/function.hex2bin.php
However, i'm just not getting any result when decrypting the data. This is where it keeps failing. I feel as though i'm either missing something but this just does not decrypt the data and only returns empty results.
openssl_decrypt(base64_encode($dR['dataset']), 'aes-128-ecb', hex2bin(openssl_digest('mysecretphrase', 'sha512')))
Any help, pointers or hints would be greatly appreciated.