我在 PHP 中遇到了一个关于加密/解密的奇怪问题。看下面的代码:
<?php
function encrypt($encrypt, $mc_key, $iv)
{
$passcrypt = trim(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($encrypt), MCRYPT_MODE_ECB, $iv));
$encode = base64_encode($passcrypt);
return $encode;
}
function decrypt($decrypt, $mc_key, $iv)
{
$decoded = base64_decode($decrypt);
$decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($decoded), MCRYPT_MODE_ECB, $iv));
return $decrypted;
}
$key = 'SAtFIAI2g_r=supL43QkO#dTQYjS0JCt';
$iv = '#CvmQT1TlpINQgnc4q%9$k8mt170MXll';
$message = 'AAABzefYdjTYBAFLNMHfLHJZCtAkTKm';
$encrypted = encrypt($message, $key, $iv);
echo $encrypted."\n";
$decrypted = decrypt($encrypted, $key, $iv);
echo $decrypted."\n";
?>
我有 $key, $iv 用来编码 $message。所以第二行应该是我的解密消息。但事实并非如此,请参阅输出:
MXk9zteonXhz2zPVK7o4oUJJuXXQRlwOzTO7JbqbsA==
tq֞u�4�&�
1P&���_pŬ�.��
_
但是如果你稍微改变一下 $message 就可以了。前任。如果 $message 是: AAABzefYdjTYBAFLNMHfLHJZCtAkTKn 输出是:
fAEeJAkluHht8l6ain99YMAMHJtiUAg47XsJBECdrLA=
AAABzefYdjTYBAFLNMHfLHJZCtAkTKn
任何帮助找到解决方案将不胜感激。