mcrypt_encrypt错误怎么 解决
目前项目中的一个密码对接放在正式环境之后出现了问题,
原因是在php5.6以上的版本中修改了mcrypt_encrypt和mcrypt_decrypt,
有人知道在新版本的php中应该怎么使用这两个方法吗?
protected function encrypt($string) {
//加密用的密钥文件
$key = md5(xxxxxxxx);
//加密方法
$cipher_alg = mcrypt_tripledes;
//初始化向量来增加安全性
$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg,mcrypt_mode_ecb), mcrypt_rand);
//开始加密
$encrypted_string = mcrypt_encrypt($cipher_alg, $key, $string, mcrypt_mode_ecb, $iv);
var_dump($encrypted_string);
return base64_encode($encrypted_string);//转化成16进制
}
protected function decrypt($string) {
$string = base64_decode($string);
//加密用的密钥文件
$key = md5(xxxxxxxx);
//加密方法
$cipher_alg = mcrypt_tripledes;
//初始化向量来增加安全性
$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg,mcrypt_mode_ecb), mcrypt_rand);
//开始解密
$decrypted_string = mcrypt_decrypt($cipher_alg, $key, $string, mcrypt_mode_ecb, $iv);
return $decrypted_string;
}
------解决思路----------------------
你的代码是有问题的(php 5.4.31)
会有 size of key is too large for this algorithm 信息出现:键的尺寸太大
不用 md5 则可正常
而在 php 5.6.3 中
不用 md5 时的错误信息是:key of size 8 not supported by this algorithm. only keys of size 24 supported
用 md5 时的错误信息是:key of size 32 not supported by this algorithm. only keys of size 24 supported
所以你只需调整key 的长度为 24 即可