Hello, need help with mcrypt,
Just got it installed, running some tests to make sure it works. I want to encrypt sensitive info, store in mysql, retrieve, and decrypt.
With testing I can get it to encrypt, but have had no luck decrypting. Also, reading some other posts/examples on this forum and others, what is a good field type and length to store the encrypted values. I have tried a few examples from the php.net site, and this forum but no luck with the decrypting...., just spits out the encrytped value. Code and output is below:
PHP
echo('This is the test to see if mycrypt works<br/><br/>');
$key = '1233543565784534534';
$data = 'maynard';
echo('Gonna try to encrypt it now<br/><br/>');
$mcrypt_module = mcrypt_module_open('blowfish', '', MCRYPT_MODE_ECB, '');
//$mcrypt_module = mcrypt_module_open('tripledes', '', 'ecb', '');
//getting initialization vector
$mcrypt_iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($mcrypt_module), MCRYPT_RAND);
echo('Initialization vector is:'.$mcrypt_iv.'<br/>');
//getting key size for this encryption mode
$key_size = mcrypt_enc_get_key_size($mcrypt_module);
echo('Key size is:'.$key_size.'<br/>');
$key = substr(md5($key), 0, $key_size);
echo('Actuall key after md5 is:'.$key.'<br/><br/>');
mcrypt_generic_init($mcrypt_module,$key,$mcrypt_iv);
$encrypted_data = mcrypt_generic($mcrypt_module, $data);
// Terminate encryption handler
mcrypt_generic_deinit($mcrypt_module);
// Initialize encryption module for decryption
mcrypt_generic_init($mcrypt_module, $key, $mcrypt_iv);
// Decrypt encrypted string
$decrypted_data = mdecrypt_generic($mcrypt_module, $encrypted_data);
// Terminate decryption handle and close module
mcrypt_generic_deinit($mcrypt_module);
mcrypt_module_close($mcrypt_module);
// Show string
echo('Here is the data un-encrypted:'.$data.'<br/>');
echo('Here is the encrypted data:'.$encrypted_data.'<br/>');
echo('Here is my decrypted data:'.trim($decrypted_data).'<br/>');
HTML
This is the test to see if mycrypt works
Gonna try to encrypt it now
Initialization vector is:äYz•Î1
Key size is:56
Actuall key after md5 is:a69961fcf9e6c7005624eef5e7c5d9c6
Here is the datad un-encrypted:maynard
Here is the encrypted_data:yȇ”€\…
Here is my decrypted data:yȇ”€\…
Any suggestions? Am I missing something simple?