I had huge problems storing encrypted information in mysql database, mySam engine
I encrypt the information as follows:
function in($plaintext) { $cipher = 'rijndael-256'; $mode = 'cbc'; $key = 'key'; $td = mcrypt_module_open($cipher, '', $mode, ''); $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); mcrypt_generic_init($td, $key, $iv); $crypttext = mcrypt_generic($td, $plaintext); mcrypt_generic_deinit($td); return $iv.$crypttext; }
Then the data is saved in blob. When I come to derypt info, it looks like about 10% of the time it was corrupted due to storage in the database.
I can verify that this is a database problem, since I run scripts to bulk encrypt and decrypt data without problems.
Any ideas? Thanks in advance...
[change decryption procedure]
function decrypt($crypttext) { $cipher = 'rijndael-256'; $mode = 'cbc'; $key = '$key'; $plaintext = ''; $td = mcrypt_module_open($cipher, '', $mode, ''); $ivsize = mcrypt_enc_get_iv_size($td); $iv = substr($crypttext, 0, $ivsize); $crypttext = substr($crypttext, $ivsize); if ($iv) { mcrypt_generic_init($td, $key, $iv); $plaintext = mdecrypt_generic($td, $crypttext); } return $plaintext; }
source share