# tp5-wechat-message-crypt-sdk **Repository Path**: lujizhang/tp5-wechat-message-crypt-sdk ## Basic Information - **Project Name**: tp5-wechat-message-crypt-sdk - **Description**: 适用于TP5+PHP7的微信官方消息加密解密SDK修改 - **Primary Language**: PHP - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-11-25 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # tp5-wechat-message-crypt-sdk #### 介绍 适用于TP5+PHP7的微信官方消息加密解密SDK修改 #### 软件架构 基于TP5和微信官方SDK,适用php7 openssl_encrypt openssl_decrypt 修改 #### 安装教程 1. 下载所有文件放置于 tp5项目根目录/extend/wechat/component目录 2. 如果你需要放置其他目录,请自行修改命名空间 #### 使用说明 PHP7已经弃用mcrypt* 系列函数,改用openssl_encrypt和openssl_decrypt替代。 微信官方PHP版SDK就没法用了,这时候需要改写SDK中加密解密方法。 网上修改的例子很多,最初解密按网上示例 用的 ```PHP openssl_decrypt($ciphertext_dec,'AES-256-CBC',$this->key,OPENSSL_RAW_DATA,$iv); ``` 自己测试加密后再解密时正常的。 但发现提交微信测试平台发布测试时会有部分微信发过来的数据无法解密,后更改为 ```PHP openssl_decrypt($ciphertext_dec,'AES-256-CBC',$this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING ,$iv); ``` 解密成功。 此处要注意,加密时仅能使用OPENSSL_RAW_DATA,不能用 OPENSSL_ZERO_PADDING,也不能用 OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, 不然加密后再用解密方法反向解密,解密失败。 ``` use wechat\component\WXBizMsgCrypt; ... // 加密 $timeStamp = time(); $nonce = substr(md5(date("Y-m-d H:i:s")),0,8); $token = 'your message token'; //消息校验Token $encodingAesKey = 'your message encoding aes key length=43'; //消息加解密Key $appId = 'your wechat app id'; $pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId); $code = $pc->encryptMsg($result, $timeStamp, $nonce, $encryptMsg); $result = $encryptMsg; // 解密 $from_xml = $encryptMsg; $xml_tree = new \DOMDocument(); $xml_tree->loadXML($encryptMsg); $array_e = $xml_tree->getElementsByTagName('Encrypt'); $encrypt = $array_e->item(0)->nodeValue; // 这里必须有ToUserName 字段否则需要改XMLParse文件 $format = ""; $from_xml = sprintf($format, $encrypt); // 第三方收到公众号平台发送的消息 $msg = ''; // subLog($msg_sign.';'.$timeStamp.':'.$nonce.':'.$appId,'decrypt param','wechat'); $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg); if ($errCode == 0) { // subLog("解密后: " . $msg . "\n","decryptMsg",'wechat'); return $msg; } else { subLog($errCode . "\n",'decryptMsgError','wechat'); return false; } ... ``` #### 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request #### 特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)