php7.1微信公众平台解密失败DecryptAESError =

发布时间:2024-12-10 01:32

'微信公众号':关注商家或平台,定期推送优惠券信息 #生活技巧# #省钱技巧# #优惠券领取平台#

php7.1发布后新特性吸引了不少PHPer,大家都在讨论新特性带来的好处与便利。但是从php7.0 升级到 php7.1 废弃(过时)了一个在过去普遍应用的扩展(mcrypt扩展)。官方提供了相应的解决提示,却没有提供更详细的解决办法。于是坑来了….

首页要确保类的构造方法支持php7版本,PHP 7开始使用和类名相同的方法名作为构造方法会报E_DEPRECATED级别的错误,提示在未来版本中会彻底抛弃类同名方法作为构造函数,但程序仍然会正常执行。

Deprecated: Methods with the same name as their class will not be constructor...

这里涉及两个文件的构造函数的修改:

pkcs7Encoder.php,将构造函数改为

class Prpcrypt

{

public $key;

function __construct($k)

{

$this->key = base64_decode($k . "=");

}

以下代码省略

topClient.php,构造函数改为

class wXBizMsgCrypt

{

private $token;

private $encodingAesKey;

private $appId;

function __construct($token, $encodingAesKey, $appId)

{

$this->token = $token;

$this->encodingAesKey = $encodingAesKey;

$this->appId = $appId;

}

以下代码省略

wxBizMsgCrypt.php,以下代码改为

class WXBizMsgCrypt

{

private $token;

private $encodingAesKey;

private $appId;

function __construct($token, $encodingAesKey, $appId)

{

$this->token = $token;

$this->encodingAesKey = $encodingAesKey;

$this->appId = $appId;

}

以下代码省略

下面是微信官方提供的消息加密解密算法中的核心部分,来自文件pkcs7Encoder.php

public function encrypt($text, $appid)

{

try {

$random = $this->getRandomStr();

$text = $random . pack("N", strlen($text)) . $text . $appid;

$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);

$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');

$iv = substr($this->key, 0, 16);

$pkc_encoder = new PKCS7Encoder;

$text = $pkc_encoder->encode($text);

mcrypt_generic_init($module, $this->key, $iv);

$encrypted = mcrypt_generic($module, $text);

mcrypt_generic_deinit($module);

mcrypt_module_close($module);

return array(ErrorCode::$OK, base64_encode($encrypted));

} catch (Exception $e) {

return array(ErrorCode::$EncryptAESError, null);

}

}

public function decrypt($encrypted, $appid)

{

try {

$ciphertext_dec = base64_decode($encrypted);

$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');

$iv = substr($this->key, 0, 16);

mcrypt_generic_init($module, $this->key, $iv);

$decrypted = mdecrypt_generic($module, $ciphertext_dec);

mcrypt_generic_deinit($module);

mcrypt_module_close($module);

} catch (Exception $e) {

return array(ErrorCode::$DecryptAESError, null);

}

try {

$pkc_encoder = new PKCS7Encoder;

$result = $pkc_encoder->decode($decrypted);

if (strlen($result) < 16)

return "";

$content = substr($result, 16, strlen($result));

$len_list = unpack("N", substr($content, 0, 4));

$xml_len = $len_list[1];

$xml_content = substr($content, 4, $xml_len);

$from_appid = substr($content, $xml_len + 4);

} catch (Exception $e) {

return array(ErrorCode::$IllegalBuffer, null);

}

if ($from_appid != $appid)

return array(ErrorCode::$ValidateAppidError, null);

return array(0, $xml_content);

}

以上代码中使用了mcrypt扩展

php官方只是轻飘飘的说mcrypt扩展被 OpenSSL取代了,却并未提供相应的转换办法。网络提供的算法大多是mcrypt加密的使用mcrypt解密或者OpenSSL加密的使用OpenSSL解密。并未提供互通替换的方案。

对比研究
作者开始分析这两种算法时感觉总是摸不着头脑,感觉两种算法的结果插件太多,完全不相关联的样子。通过一番查阅和反复测试终于明白这两种算法的区别了。

在算法、data、key、vi 一致的情况下

openssl_encrypt 加密相当于将 mcrypt_encrypt 的加密结果执行一次 base64_encode

openssl_decode 解密相当于 先将加密结果执行一次base64_decode 然后再通过mcrypt_encrypt 解密

调整后的代码,兼容php7

public function encrypt($text, $appid)

{

try {

if(version_compare(PHP_VERSION, '7','>=')) {

$random = $this->getRandomStr();

$text = $random . pack("N", strlen($text)) . $text . $appid;

$iv = substr($this->key, 0, 16);

$pkc_encoder = new PKCS7Encoder;

$text = $pkc_encoder->encode($text);

$encrypted = openssl_encrypt($text,'AES-256-CBC',substr($this->key, 0, 32),OPENSSL_ZERO_PADDING,$iv);

} else {

$random = $this->getRandomStr();

$text = $random . pack("N", strlen($text)) . $text . $appid;

$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);

$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');

$iv = substr($this->key, 0, 16);

$pkc_encoder = new PKCS7Encoder;

$text = $pkc_encoder->encode($text);

mcrypt_generic_init($module, $this->key, $iv);

$encrypted = mcrypt_generic($module, $text);

mcrypt_generic_deinit($module);

mcrypt_module_close($module);

$encrypted = base64_encode($encrypted);

}

return array(ErrorCode::$OK, $encrypted);

} catch (Exception $e) {

return array(ErrorCode::$EncryptAESError, null);

}

}

public function decrypt($encrypted, $appid)

{

try {

if(version_compare(PHP_VERSION, '7','>=')) {

$iv = substr($this->key, 0, 16);

$decrypted = openssl_decrypt($encrypted,'AES-256-CBC',substr($this->key, 0, 32),OPENSSL_ZERO_PADDING,$iv);

} else {

$ciphertext_dec = base64_decode($encrypted);

$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');

$iv = substr($this->key, 0, 16);

mcrypt_generic_init($module, $this->key, $iv);

$decrypted = mdecrypt_generic($module, $ciphertext_dec);

mcrypt_generic_deinit($module);

mcrypt_module_close($module);

}

} catch (Exception $e) {

return array(ErrorCode::$DecryptAESError, null);

}

try {

$pkc_encoder = new PKCS7Encoder;

$result = $pkc_encoder->decode($decrypted);

if (strlen($result) < 16)

return "";

$content = substr($result, 16, strlen($result));

$len_list = unpack("N", substr($content, 0, 4));

$xml_len = $len_list[1];

$xml_content = substr($content, 4, $xml_len);

$from_appid = substr($content, $xml_len + 4);

if(version_compare(PHP_VERSION, '7','>=')) {

if (!$appid) {

$appid = $from_appid;

}

}

} catch (Exception $e) {

return array(ErrorCode::$IllegalBuffer, null);

}

if ($from_appid != $appid)

return array(ErrorCode::$ValidateAppidError, null);

if(version_compare(PHP_VERSION, '7','>=')) {

return array(0, $xml_content, $from_appid);

} else {

return array(0, $xml_content);

}

}

网址:php7.1微信公众平台解密失败DecryptAESError = https://www.yuejiaxmz.com/news/view/429762

相关内容

微信公众平台新闻传播效应探析
生活辅导微信公众平台招新啦!
微信公众平台在企业网络营销中的应用
国内十大商铺转让平台推荐避免转让失败,轻松找到合适的租户
springboot整合微信公众号实现模版消息推送
微信公众号
微帮便民信息发布服务平台
大众网开通官方微信平台 新闻资讯同步推送
玩转微信 为您推荐十大生活服务类微信公众号
我校企业微信平台上线了!

随便看看