脚本宝典收集整理的这篇文章主要介绍了微信小程序加密数据(encryptedData)解密中的PHP代码,php7.1报错,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
问题描述
最近在开发微信小程序涉及到加密数据(encryptedData)的解密,用的是PHP代码,在运行后报错mcrypt_module_ xxx is dePRecated,提示方法已过时了
经研究得知,是php7.1版本引起的,可以使用OPEnssl方法代替解密.
首先要知道微信方使用的是AES-128-CBC加密的:
所以我们采用openssl也应该对应:
/**
* 对密文进行解密
* @param string $aesCipher 需要解密的密文
* @param string $aesIV 解密的初始向量
* @return string 解密得到的明文
*/
public function decrypt( $aesCipher, $aesIV )
{
try {
// $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
// mcrypt_generic_init($module, $this->key, $aesIV);
// //解密
// $decrypted = mdecrypt_generic($module, $aesCipher);
// mcrypt_generic_deinit($module);
// mcrypt_module_close($module);
$decrypted = openssl_decrypt($aesCipher, "aes-128-cbc", $this->key, OPENSSL_RAW_DATA ,$aesIV);
} catch (Exception $e) {
return array(ErrorCode::$IllegalBuffer, null);
}
try {
//去除补位字符
$pkc_encoder = new PKCS7Encoder;
$result = $pkc_encoder->decode($decrypted);
} catch (Exception $e) {
//print $e;
return array(ErrorCode::$IllegalBuffer, null);
}
return array(0, $result);
}
特别注意
很多解密失败是因为在使用openssl_decrypt解密的时候又使用了一次base_decode,实际上微信demo在调用这个方法之前就已经把所有参数都base_decode了一次:
by KingFer
以上是脚本宝典为你收集整理的微信小程序加密数据(encryptedData)解密中的PHP代码,php7.1报错全部内容,希望文章能够帮你解决微信小程序加密数据(encryptedData)解密中的PHP代码,php7.1报错所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。