I am trying to encrypt sensitive user data like personal messages in my php powered website before entering into the database. I have researched a bit on the internet and I
You're in Luck.
The openssl
extension has some pretty easy to use methods for AES-256. The steps you need to take are basically...
$encryption_key = openssl_random_pseudo_bytes(32);
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
openssl_encrypt()
openssl_encrypt($data, 'aes-256-cbc', $encryptionKey, $options, $initializationVector)
$options
can be set to 0
for default options or changed to OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING
$encrypted = $encrypted . ':' . $iv;
explode(':' , $encrypted);
openssl_decrypt()
openssl_decrypt($encryptedData, 'aes-256-cbc', $encryptionKey, $options, $initializationVector)
openssl_functions()
won't be available by default, you can enable this extension in your php.ini
file by uncommenting the line. ;extension=php_openssl.dll
by removing the leading ;
http://phpfiddle.org/lite/code/9epi-j5v2