I'd use the mcrypt functions in PHP for this task.
First, install the "php5-mcrypt" package and restart Apache.
You want to use the "MCRYPT_RIJNDAEL_256" cipher for 256-bit AES.
I once needed to write a couple of PHP functions to encrypt a string and store it in a database. I chose AES256 to do the encryption, then used Base64 encoding to create a nice plain-text string that can be stored in a character field. Here are the functions I wrote; you're welcome to them:
Code:
function b64_encrypt($string,$key) {
# encrypt a string with Rijndael-256 and $key
# return the base64 encoding of the encrypted result
# from the PHP manual
$td = mcrypt_module_open('rijndael-256', '', 'ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
# added base64 encoding here
$encrypted_data = base64_encode(mcrypt_generic($td, $string));
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $encrypted_data;
}
function b64_decrypt($string,$key) {
# convert a base-64 encoded string into a Rijndael-256
# cipher then decrypt using $key
$td = mcrypt_module_open('rijndael-256', '', 'ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$decrypted_data = mdecrypt_generic($td, base64_decode($string));
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
# trim any padding
return trim($decrypted_data);
}
Bookmarks