博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
微信公众平台开发者中心安全模式消息体加解密实现
阅读量:4648 次
发布时间:2019-06-09

本文共 11265 字,大约阅读时间需要 37 分钟。

关键字:微信公众平台 消息体签名 消息体加解密 EncodingAESKey 安全模式

原文   

一、消息体加解密

微信公众平台在配置服务器时,提供了3种加解密的模式供开发者选择,即明文模式、兼容模式、安全模式,选择兼容模式和安全模式前,需在开发者中心填写消息加解密密钥EncodingAESKey。

  • 明文模式:维持现有模式,没有适配加解密新特性,消息体明文收发,默认设置为明文模式
  • 兼容模式:公众平台发送消息内容将同时包括明文和密文,消息包长度增加到原来的3倍左右;公众号回复明文或密文均可,不影响现有消息收发;开发者可在此模式下进行调试
  • 安全模式(推荐):公众平台发送消息体的内容只含有密文,公众账号回复的消息体也为密文,建议开发者在调试成功后使用此模式收发消息

什么是EncodingAESKey?

  • 微信公众平台采用AES对称加密算法对推送给公众帐号的消息体对行加密,EncodingAESKey则是加密所用的秘钥。公众帐号用此秘钥对收到的密文消息体进行解密,回复消息体也用此秘钥加密。AES对称加密算法的原理可以参考 http://www.cnblogs.com/txw1958/p/aes.html

  加解密的详细技术方案可以参考官方文档 http://mp.weixin.qq.com/wiki/index.php?title=%E6%8A%80%E6%9C%AF%E6%96%B9%E6%A1%88

 

二、开发实现及数据分析

1. 配置

假设本次的开发配置中URL为

http://www.fangbei.org/index.php

接口程序中需要配置以下三项参数

/*    方倍工作室 http://www.cnblogs.com/txw1958/    CopyRight 2014 All Rights Reserved*/define("TOKEN", "weixin");define("AppID", "wxbad0b45542aa0b5e");define("EncodingAESKey", "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG");require_once('wxBizMsgCrypt.php');

 

2. 加解密实现

当用户向公众账号发送消息时,微信公众账号将会在URL中带上signature、timestamp、nonce、encrypt_type、msg_signature等参数,如下所示

http://www.fangbei.org/index.php?signature=35703636de2f9df2a77a662b68e521ce17c34db4&timestamp=1414243737&nonce=1792106704&encrypt_type=aes&msg_signature=6147984331daf7a1a9eed6e0ec3ba69055256154

同时向该接口推送如下XML消息 ,即一个已加密的消息

这时,程序需要从url中获得以下参数

$timestamp  = $_GET['timestamp'];$nonce = $_GET["nonce"];$msg_signature  = $_GET['msg_signature'];$encrypt_type = $_GET['encrypt_type'];

这些参数将用于加解密过程

收到消息后,先进行解密,解密部分代码如下

$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];if ($encrypt_type == 'aes'){    $pc = new WXBizMsgCrypt(TOKEN, EncodingAESKey, AppID);                    $this->logger(" D \r\n".$postStr);    $decryptMsg = "";  //解密后的明文    $errCode = $pc->DecryptMsg($msg_signature, $timestamp, $nonce, $postStr, $decryptMsg);    $postStr = $decryptMsg;}

解密完成后,把解密内容又返回给$postStr,这是为了保证将消息中解密后的内容和明文模式时的消息统一,方便后续处理,解密后的XML如下

1414243737
6074130599188426998

对消息在自己的原来代码中处理,完成之后,要回复的消息如下

1414243733

把上述消息进行加密,返回给微信公众账号

//加密if ($encrypt_type == 'aes'){    $encryptMsg = ''; //加密后的密文    $errCode = $pc->encryptMsg($result, $timeStamp, $nonce, $encryptMsg);    $result = $encryptMsg;    $this->logger(" E \r\n".$result);}

加密后的内容如下

1414243733

这样,一个安全模式下的加解密消息就完成了。

 

三、完整代码

1 
responseMsg(); 14 }else{ 15 $wechatObj->valid(); 16 } 17 18 class wechatCallbackapiTest 19 { 20 //验证签名 21 public function valid() 22 { 23 $echoStr = $_GET["echostr"]; 24 $signature = $_GET["signature"]; 25 $timestamp = $_GET["timestamp"]; 26 $nonce = $_GET["nonce"]; 27 $tmpArr = array(TOKEN, $timestamp, $nonce); 28 sort($tmpArr); 29 $tmpStr = implode($tmpArr); 30 $tmpStr = sha1($tmpStr); 31 if($tmpStr == $signature){ 32 echo $echoStr; 33 exit; 34 } 35 } 36 37 //响应消息 38 public function responseMsg() 39 { 40 $timestamp = $_GET['timestamp']; 41 $nonce = $_GET["nonce"]; 42 $msg_signature = $_GET['msg_signature']; 43 $encrypt_type = (isset($_GET['encrypt_type']) && ($_GET['encrypt_type'] == 'aes')) ? "aes" : "raw"; 44 45 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; 46 if (!empty($postStr)){ 47 //解密 48 if ($encrypt_type == 'aes'){ 49 $pc = new WXBizMsgCrypt(TOKEN, EncodingAESKey, AppID); 50 $this->logger(" D \r\n".$postStr); 51 $decryptMsg = ""; //解密后的明文 52 $errCode = $pc->DecryptMsg($msg_signature, $timestamp, $nonce, $postStr, $decryptMsg); 53 $postStr = $decryptMsg; 54 } 55 $this->logger(" R \r\n".$postStr); 56 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); 57 $RX_TYPE = trim($postObj->MsgType); 58 59 //消息类型分离 60 switch ($RX_TYPE) 61 { 62 case "event": 63 $result = $this->receiveEvent($postObj); 64 break; 65 case "text": 66 $result = $this->receiveText($postObj); 67 break; 68 } 69 $this->logger(" R \r\n".$result); 70 //加密 71 if ($encrypt_type == 'aes'){ 72 $encryptMsg = ''; //加密后的密文 73 $errCode = $pc->encryptMsg($result, $timeStamp, $nonce, $encryptMsg); 74 $result = $encryptMsg; 75 $this->logger(" E \r\n".$result); 76 } 77 echo $result; 78 }else { 79 echo ""; 80 exit; 81 } 82 } 83 84 //接收事件消息 85 private function receiveEvent($object) 86 { 87 $content = ""; 88 switch ($object->Event) 89 { 90 case "subscribe": 91 $content = "欢迎关注方倍工作室 "; 92 break; 93 } 94 95 $result = $this->transmitText($object, $content); 96 return $result; 97 } 98 99 //接收文本消息100 private function receiveText($object)101 {102 $keyword = trim($object->Content);103 if (strstr($keyword, "文本")){104 $content = "这是个文本消息";105 }else if (strstr($keyword, "单图文")){106 $content = array();107 $content[] = array("Title"=>"单图文标题", "Description"=>"单图文内容", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");108 }else if (strstr($keyword, "图文") || strstr($keyword, "多图文")){109 $content = array();110 $content[] = array("Title"=>"多图文1标题", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");111 $content[] = array("Title"=>"多图文2标题", "Description"=>"", "PicUrl"=>"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");112 $content[] = array("Title"=>"多图文3标题", "Description"=>"", "PicUrl"=>"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");113 }else if (strstr($keyword, "音乐")){114 $content = array();115 $content = array("Title"=>"最炫民族风", "Description"=>"歌手:凤凰传奇", "MusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3", "HQMusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3");116 }else{117 $content = date("Y-m-d H:i:s",time())."\n".$object->FromUserName."\n技术支持 方倍工作室";118 }119 120 if(is_array($content)){121 if (isset($content[0])){122 $result = $this->transmitNews($object, $content);123 }else if (isset($content['MusicUrl'])){124 $result = $this->transmitMusic($object, $content);125 }126 }else{127 $result = $this->transmitText($object, $content);128 }129 return $result;130 }131 132 //回复文本消息133 private function transmitText($object, $content)134 {135 $xmlTpl = "
136
137
138
%s
139
140
141
";142 $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content);143 return $result;144 }145 146 //回复图文消息147 private function transmitNews($object, $newsArray)148 {149 if(!is_array($newsArray)){150 return;151 }152 $itemTpl = "
153
<![CDATA[%s]]>154
155
156
157
158 ";159 $item_str = "";160 foreach ($newsArray as $item){161 $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);162 }163 $xmlTpl = "
164
165
166
%s
167
168
%s
169
170 $item_str
171
";172 173 $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));174 return $result;175 }176 177 //回复音乐消息178 private function transmitMusic($object, $musicArray)179 {180 $itemTpl = "
181
<![CDATA[%s]]>182
183
184
185
";186 187 $item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']);188 189 $xmlTpl = "
190
191
192
%s
193
194 $item_str195
";196 197 $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time());198 return $result;199 }200 201 //日志记录202 public function logger($log_content)203 {204 if(isset($_SERVER['HTTP_APPNAME'])){ //SAE205 sae_set_display_errors(false);206 sae_debug($log_content);207 sae_set_display_errors(true);208 }else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL209 $max_size = 500000;210 $log_filename = "log.xml";211 if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){
unlink($log_filename);}212 file_put_contents($log_filename, date('Y-m-d H:i:s').$log_content."\r\n", FILE_APPEND);213 }214 }215 }216 ?>

 

转载于:https://www.cnblogs.com/txw1958/p/weixin-aes-encrypt-decrypt.html

你可能感兴趣的文章
强名称程序集(strong name assembly)——为程序集赋予强名称
查看>>
1028. List Sorting (25)
查看>>
BZOJ 1613: [Usaco2007 Jan]Running贝茜的晨练计划
查看>>
ubuntu 重启命令,ubuntu 重启网卡方法
查看>>
Linux的学习:
查看>>
JavaScript中的原型继承原理
查看>>
Python logger模块
查看>>
jquery控制css的display(控制元素的显示与隐藏)
查看>>
关于python做人工智能的一个网页(很牛逼)
查看>>
判断控件的CGRect是否重合,获取控件的最大XY值
查看>>
POJ-1128 Frame Stacking
查看>>
浏览器调试淘宝首页看到有趣的招聘信息
查看>>
ASP.NET Identity “角色-权限”管理 4
查看>>
[转][译]ASP.NET MVC 4 移动特性
查看>>
SurfaceView+MediaPlay的bug们
查看>>
网络表示学习总结
查看>>
完成评论功能
查看>>
far和near
查看>>
Python爬虫实战四之抓取淘宝MM照片
查看>>
2015 Multi-University Training Contest 1
查看>>