mcrypt_encrypt错误怎么 解决

WBOY
Libérer: 2016-06-23 13:38:41
original
1731 Les gens l'ont consulté

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


回复讨论(解决方案)

有没有人在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了一下

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!