我有以下函数来加密和解密 id。但解密后$id=298
给出。�����bt
错误信息是:
无效的 utf8 字符串:'\x8A\xBC\x86\xFD'
我无法更改密钥,因为我有超过 300 次使用此 $key 进行登录。
function mencrypt($input,$key)
{
$key = substr(md5($key),0,24);
$td = mcrypt_module_open ('tripledes', '', 'ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
mcrypt_generic_init ($td, $key, $iv);
$encrypted_data = mcrypt_generic ($td, $input);
mcrypt_generic_deinit ($td);
mcrypt_module_close ($td);
return trim(chop(url_base64_encode($encrypted_data)));
}
function mdecrypt($input,$key)
{
$input = trim(chop(url_base64_decode($input)));
$td = mcrypt_module_open ('tripledes', '', 'ecb', '');
$key = substr(md5($key),0,24);
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
mcrypt_generic_init ($td, $key, $iv);
$decrypted_data = mdecrypt_generic ($td, $input);
mcrypt_generic_deinit ($td);
mcrypt_module_close ($td);
return trim(chop($decrypted_data));
}
$id = 298;
$key ="21232F297A57A5A743894A0E4A801FC3";
echo $encrypted_id = mencrypt($id,$key);
echo "<br>";
echo mdecrypt($encrypted_id,$key);