I have a site that I want to do some two way encryption. I got this script working BUT for some reason on FireFox it displays "?" characters at the end after I decrypt the values. It seems to work fine on other browsers. I seem to have the correct content-type (using: charset=iso-8859-1)? Is there anything else that could be causing this problem? Below is my code:
$key='Df#5Fgs';
function decrypt_data($c_t) {
if ($c_t != "") {
global $key;
$td = mcrypt_module_open('des', '', 'ecb', '');
$key = substr($key, 0, mcrypt_enc_get_key_size($td));
$iv_size = mcrypt_enc_get_iv_size($td);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
if (mcrypt_generic_init($td, $key, $iv) != -1) {
mcrypt_generic_init($td, $key, $iv);
$p_t = mdecrypt_generic($td, $c_t);
/* Clean up */
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
}
if (strncmp($p_t, $plain_text, strlen($plain_text)) == 0) {
return $p_t;
} else {
return "error";
}
}
}
function encrypt_data($plain_text) {
if ($plain_text != "") {
global $key;
$td = mcrypt_module_open('des', '', 'ecb', '');
$key = substr($key, 0, mcrypt_enc_get_key_size($td));
$iv_size = mcrypt_enc_get_iv_size($td);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
if (mcrypt_generic_init($td, $key, $iv) != -1) {
/* Encrypt data */
$c_t = mcrypt_generic($td, $plain_text);
mcrypt_generic_deinit($td);
/* Clean up */
//mcrypt_generic_deinit($td);
//mcrypt_module_close($td);
return $c_t;
}
return 'error';
}
}
$the_text='This is a test';
echo 'Before Encryption: '.$the_text.'<br/><br/>';
$the_text=encrypt_data($the_text);
echo 'After Encryption: '.$the_text.'<br/><br/>';
$the_text=decrypt_data($the_text);
echo 'After Decryption: '.$the_text; // ON FIREFOX IT PRINTS OUT This is a test??