微信小程序加密数据(encryptedData)解密中的PHP代码,php7.1报错mcrypt_module_ is deprecated

来源:互联网 发布:c语言对二维数组排序 编辑:程序博客网 时间:2024/05/16 09:47

最近在开发微信小程序涉及到加密数据(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了一次:


所以解密的方法应该是如此

openssl_decrypt($aesCipher, "aes-128-cbc", $this->key, OPENSSL_RAW_DATA ,$aesIV);





阅读全文
0 0
原创粉丝点击