mcrypt_encrypt错误怎么 解决

WBOY
Release: 2016-06-23 13:38:41
Original
1731 people have browsed it

目前项目中的一个密码对接放在正式环境之后出现了问题,
原因是在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;    }
Copy after login


回复讨论(解决方案)

有没有人在PHP5.6+的版本中使用过这个加密方法的?出来帮助一下新人吧

你的代码是有问题的(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 即可

抱歉,我之前看一个帖子说秘钥长度不够,就给MD5了一下

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!