爱程序网

微信公众平台开发者中心安全模式消息体加解密实现

来源: 阅读:

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

原文 http://www.cnblogs.com/txw1958/p/weixin-aes-encrypt-decrypt.html  

一、消息体加解密

微信公众平台在配置服务器时,提供了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消息 ,即一个已加密的消息

<xml>    <ToUserName><![CDATA[gh_680bdefc8c5d]]></ToUserName>    <Encrypt><![CDATA[MNn4+jJ/VsFh2gUyKAaOJArwEVYCvVmyN0iXzNarP3O6vXzK62ft1/KG2/XPZ4y5bPWU/jfIfQxODRQ7sLkUsrDRqsWimuhIT8Eq+w4E/28m+XDAQKEOjWTQIOp1p6kNsIV1DdC3B+AtcKcKSNAeJDr7x7GHLx5DZYK09qQsYDOjP6R5NqebFjKt/NpEl/GU3gWFwG8LCtRNuIYdK5axbFSfmXbh5CZ6Bk5wSwj5fu5aS90cMAgUhGsxrxZTY562QR6c+3ydXxb+GHI5w+qA+eqJjrQqR7u5hS+1x5sEsA7vS+bZ5LYAR3+PZ243avQkGllQ+rg7a6TeSGDxxhvLw+mxxinyk88BNHkJnyK//hM1k9PuvuLAASdaud4vzRQlAmnYOslZl8CN7gjCjV41skUTZv3wwGPxvEqtm/nf5fQ=]]></Encrypt></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 rn".$postStr);    $decryptMsg = "";  //解密后的明文    $errCode = $pc->DecryptMsg($msg_signature, $timestamp, $nonce, $postStr, $decryptMsg);    $postStr = $decryptMsg;}

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

<xml>    <ToUserName><![CDATA[gh_680bdefc8c5d]]></ToUserName>    <FromUserName><![CDATA[oIDrpjpQ8j8mBuQ8nM26HWzNEZgg]]></FromUserName>    <CreateTime>1414243737</CreateTime>    <MsgType><![CDATA[text]]></MsgType>    <Content><![CDATA[?]]></Content>    <MsgId>6074130599188426998</MsgId></xml>

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

<xml>    <ToUserName><![CDATA[oIDrpjpQ8j8mBuQ8nM26HWzNEZgg]]></ToUserName>    <FromUserName><![CDATA[gh_680bdefc8c5d]]></FromUserName>    <CreateTime>1414243733</CreateTime>    <MsgType><![CDATA[text]]></MsgType>    <Content><![CDATA[2014-10-25 21:28:53技术支持 方倍工作室http://www.fangbei.org/]]></Content></xml>

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

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

加密后的内容如下

<xml>    <Encrypt><![CDATA[pE6gp6qvVBMHwCXwnM7illFBrh9LmvlKFlPUDuyQo9EKNunqbUFMd2KjiYoz+3K1B+93JbMWHt+19TI8awdRdyopRS4oUNg5M2jwpwXTmc6TtafkKNjvqlvPXIWmutw0tuMXke1hDgsqz0SC8h/QjNLxECuwnczrfCMJlt+APHnX2yMMaq/aYUNcndOH387loQvl2suCGucXpglnbxf7frTCz9NQVgKiYrvKOhk6KFiVMnzuxy6WWmoe3GBiUCPTtYf5b1CxzN2IHViEBm28ilV9wWdNOM9TPG7BSSAcpgY4pcwdIG5+4KhgYmnVU3bc/ZJkk42TIdidigOfFpJwET4UWVrLB/ldUud4aPexp3aPCR3Fe53S2HHcl3tTxh4iRvDftUKP3svYPctt1MlYuYv/BZ4JyzUQV03H+0XrVyDY2tyVjimgCrA2c1mZMgHttOHTQ6VTnxrMq0GWlRlH0KPQKqtjUpNQzuOH4upQ8boPsEtuY3wDA2RaXQPJrXon]]></Encrypt>    <MsgSignature><![CDATA[6c46904dc1f58b2ddf2dd0399f1c6cf41f33ecb9]]></MsgSignature>    <TimeStamp>1414243733</TimeStamp>    <Nonce><![CDATA[1792106704]]></Nonce></xml>

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

 

三、完整代码

  1 <?php  2 /*  3     方倍工作室 http://www.cnblogs.com/txw1958/  4     CopyRight 2014 All Rights Reserved  5 */  6 define("TOKEN", "weixin");  7 define("AppID", "wxbad0b45542aa0b5e");  8 define("EncodingAESKey", "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG");  9 require_once('wxBizMsgCrypt.php'); 10  11 $wechatObj = new wechatCallbackapiTest(); 12 if (!isset($_GET['echostr'])) { 13     $wechatObj->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 rn".$postStr); 51                 $decryptMsg = "";  //解密后的明文 52                 $errCode = $pc->DecryptMsg($msg_signature, $timestamp, $nonce, $postStr, $decryptMsg); 53                 $postStr = $decryptMsg; 54             } 55             $this->logger(" R rn".$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 rn".$result); 70             //加密 71             if ($encrypt_type == 'aes'){ 72                 $encryptMsg = ''; //加密后的密文 73                 $errCode = $pc->encryptMsg($result, $timeStamp, $nonce, $encryptMsg); 74                 $result = $encryptMsg; 75                 $this->logger(" E rn".$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 = "<xml>136     <ToUserName><![CDATA[%s]]></ToUserName>137     <FromUserName><![CDATA[%s]]></FromUserName>138     <CreateTime>%s</CreateTime>139     <MsgType><![CDATA[text]]></MsgType>140     <Content><![CDATA[%s]]></Content>141 </xml>";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 = "        <item>153             <Title><![CDATA[%s]]></Title>154             <Description><![CDATA[%s]]></Description>155             <PicUrl><![CDATA[%s]]></PicUrl>156             <Url><![CDATA[%s]]></Url>157         </item>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 = "<xml>164     <ToUserName><![CDATA[%s]]></ToUserName>165     <FromUserName><![CDATA[%s]]></FromUserName>166     <CreateTime>%s</CreateTime>167     <MsgType><![CDATA[news]]></MsgType>168     <ArticleCount>%s</ArticleCount>169     <Articles>170 $item_str    </Articles>171 </xml>";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 = "<Music>181         <Title><![CDATA[%s]]></Title>182         <Description><![CDATA[%s]]></Description>183         <MusicUrl><![CDATA[%s]]></MusicUrl>184         <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>185     </Music>";186 187         $item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']);188 189         $xmlTpl = "<xml>190     <ToUserName><![CDATA[%s]]></ToUserName>191     <FromUserName><![CDATA[%s]]></FromUserName>192     <CreateTime>%s</CreateTime>193     <MsgType><![CDATA[music]]></MsgType>194     $item_str195 </xml>";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."rn", FILE_APPEND);213         }214     }215 }216 ?>

 

相关文章列表:
关于爱程序网 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助