PHP7.2 下mcrypt_module_open() 无法使用的解决方法

星期五, 2020-02-28 | Author: Lee | php | 3,291 views

php版本升级到7.2后,其他看下来没有什么问题,在小程序的授权上,出现了错误,无法找到mcrypt_module_open的函数.

查询网上方法修复之.

主要是使用 openssl_decrypt来替换解决,解密协议为AES-128-CBC,

如果无法解析可以尝试AES-256-CBC,本人使用AES-128-CBC正常解析的

微信那边解密错误会返回错误码-41003,则解析错误.

1.修改文件wxBizDataCrypt.php

        /**
	 * 检验数据的真实性,并且获取解密后的明文.
	 * @param $encryptedData string 加密的用户数据
	 * @param $iv string 与用户数据一同返回的初始向量
	 * @param $data string 解密后的原文
         *
	 * @return int 成功0,失败返回对应的错误码
	 */
	public function decryptData($encryptedData, $iv, &$data)
	{
		if (strlen($this->sessionKey) != 24) {
			return ErrorCode::$IllegalAesKey;
		}
 
		$aesKey = base64_decode($this->sessionKey);
 
		if (strlen($iv) != 24) {
			return ErrorCode::$IllegalIv;
		}
 
		$aesIV = base64_decode($iv);
                //###此处取消base64_decode解密##########
		//$aesCipher = base64_decode($encryptedData);
		$aesCipher=$encryptedData;
 
		$pc = new Prpcrypt($aesKey);
		$result = $pc->decrypt($aesCipher, $aesIV);
 
		if ($result[0] != 0) {
			return $result[0];
		}
 
		$dataObj = json_decode($result[1]);
 
		if ($dataObj == NULL) {
			return ErrorCode::$IllegalBuffer;
		}
 
		if ($dataObj->watermark->appid != $this->appid) {
			return ErrorCode::$IllegalBuffer;
		}
 
		$data = $result[1];
		return ErrorCode::$OK;
	}

2.修改pkcs7Encoder.php文件

   class Prpcrypt
   {
	public $key;
 
	public function __construct($k)
	{
		$this->key = $k;
	}
 
	/**
     * 对密文进行解密
     * @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_ZERO_PADDING,$aesIV);
		}
		catch (Exception $e) {
			return array(ErrorCode::$IllegalBuffer, NULL);
		}
 
		try {
			$pkc_encoder = new PKCS7Encoder();
			$result = $pkc_encoder->decode($decrypted);
		}
		catch (Exception $e) {
			return array(ErrorCode::$IllegalBuffer, NULL);
		}
 
		return array(0, $result);
	}
  }

Tags: ,

文章作者: Lee

本文地址: https://www.pomelolee.com/2045.html

除非注明,Pomelo Lee文章均为原创,转载请以链接形式标明本文地址

No comments yet.

Leave a comment

Search

文章分类

Links

Meta