/** * */ public function DecryptMsg($params, $data) { $mpa = TMS_APP::G('mp\\mpaccount'); $msg_signature = $params['msg_signature']; $timestamp = $params['timestamp']; $nonce = $params['nonce']; $sMsg = ""; $wxcpt = new WXBizMsgCrypt($mpa->token, $mpa->qy_encodingaeskey, $mpa->qy_corpid); $errCode = $wxcpt->DecryptMsg($msg_signature, $timestamp, $nonce, $data, $sMsg); if ($errCode != 0) { return array(false, $errCode); } return array(true, $sMsg); }
public function responseMsg() { if (!$this->checkSignature()) { exit; } $this->timestamp = $_GET['timestamp']; $this->nonce = $_GET["nonce"]; $this->msg_signature = $_GET['msg_signature']; $this->encrypt_type = isset($_GET['encrypt_type']) && $_GET['encrypt_type'] == 'aes' ? "aes" : "raw"; $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)) { //解密 if ($this->encrypt_type == 'aes') { $pc = new WXBizMsgCrypt(TOKEN, EncodingAESKey, APPID); $decryptMsg = ""; //解密后的明文 $errCode = $pc->DecryptMsg($this->msg_signature, $this->timestamp, $this->nonce, $postStr, $decryptMsg); $postStr = $decryptMsg; } if ($this->check_php_version("5.2.11")) { libxml_disable_entity_loader(true); } $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $rxType = trim($postObj->MsgType); //消息类型分离 switch ($rxType) { case "event": $result = $this->receiveEvent($postObj); break; case "text": $result = $this->receiveText($postObj); break; default: $result = "unknown msg type: " . $rxType; break; } //加密 if ($this->encrypt_type == 'aes') { $encryptMsg = ''; //加密后的密文 $errCode = $pc->encryptMsg($result, $this->timeStamp, $this->nonce, $encryptMsg); $result = $encryptMsg; } echo $result; } else { echo ""; exit; } }
public function decryptMessage($dataArr = array()) { try { extract($dataArr); $data = ""; //decrypted data $returnArr = array('hasError' => false); if ($encrypt_type == 'aes') { $pc = new WXBizMsgCrypt($this->token, $this->encodingAESKey, $this->appid); $errCode = $pc->DecryptMsg($msg_signature, $timestamp, $nonce, $raw_data, $data); if ($errCode != 0) { // decrypt failed throw new Exception('Decryption error: ' . $errCode); } } else { $data = $raw_data; } // parse XML libxml_disable_entity_loader(true); $postObj = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA); $returnArr['postArr'] = (array) $postObj; } catch (Exception $ex) { # log the exception log_error('WeChat: decryptMessage', null, $ex->getMessage()); $returnArr = array('hasError' => true, 'err' => $ex->getMessage()); } return $returnArr; }
/** * 接收消息 */ public static function receiveMsg() { $corpId = self::$corpId; $token = self::$token; $signature = self::$signature; $timestamp = self::$timestamp; $encodingAesKey = self::$encodingAesKey; $nonce = self::$nonce; #接收数据 $xmlStr = file_get_contents("php://input", "r"); $wxcpt = new WXBizMsgCrypt($token, $encodingAesKey, $corpId); $data = array(); $code = $wxcpt->DecryptMsg($signature, $timestamp, $nonce, $xmlStr, $data); if (self::$debug) { } return $data; }
2.验证消息体签名的正确性。 3.将post请求的数据进行xml解析,并将<Encrypt>标签的内容进行解密,解密出来的明文即是用户回复消息的明文,明文格式请参考官方文档 第2,3步可以用公众平台提供的库函数DecryptMsg来实现。 */ // $sReqMsgSig = HttpUtils.ParseUrl("msg_signature"); $sReqMsgSig = "477715d11cdb4164915debcba66cb864d751f3e6"; // $sReqTimeStamp = HttpUtils.ParseUrl("timestamp"); $sReqTimeStamp = "1409659813"; // $sReqNonce = HttpUtils.ParseUrl("nonce"); $sReqNonce = "1372623149"; // post请求的密文数据 // $sReqData = HttpUtils.PostData(); $sReqData = "<xml><ToUserName><![CDATA[wx5823bf96d3bd56c7]]></ToUserName><Encrypt><![CDATA[RypEvHKD8QQKFhvQ6QleEB4J58tiPdvo+rtK1I9qca6aM/wvqnLSV5zEPeusUiX5L5X/0lWfrf0QADHHhGd3QczcdCUpj911L3vg3W/sYYvuJTs3TUUkSUXxaccAS0qhxchrRYt66wiSpGLYL42aM6A8dTT+6k4aSknmPj48kzJs8qLjvd4Xgpue06DOdnLxAUHzM6+kDZ+HMZfJYuR+LtwGc2hgf5gsijff0ekUNXZiqATP7PF5mZxZ3Izoun1s4zG4LUMnvw2r+KqCKIw+3IQH03v+BCA9nMELNqbSf6tiWSrXJB3LAVGUcallcrw8V2t9EL4EhzJWrQUax5wLVMNS0+rUPA3k22Ncx4XXZS9o0MBH27Bo6BpNelZpS+/uh9KsNlY6bHCmJU9p8g7m3fVKn28H3KDYA5Pl/T8Z1ptDAVe0lXdQ2YoyyH2uyPIGHBZZIs2pDBS8R07+qN+E7Q==]]></Encrypt><AgentID><![CDATA[218]]></AgentID></xml>"; $sMsg = ""; // 解析之后的明文 $errCode = $wxcpt->DecryptMsg($sReqMsgSig, $sReqTimeStamp, $sReqNonce, $sReqData, $sMsg); if ($errCode == 0) { // 解密成功,sMsg即为xml格式的明文 // TODO: 对明文的处理 // For example: $xml = new DOMDocument(); $xml->loadXML($sMsg); $content = $xml->getElementsByTagName('Content')->item(0)->nodeValue; print "content: " . $content . "\n\n"; // ... // ... } else { print "ERR: " . $errCode . "\n\n"; //exit(-1); } /*
/** * Lancy webserver recieve client side data; * @param: string $appid; * return: string $sMsg; */ public function get_msg($appid) { switch ($appid) { case '1': $encodingAesKey = "1yrAF6xYc5Zp9kNz2npfBWssc6jkwWBMQr65WF0Btlh"; $token = "BtWjuMH7Db3ugSg5ENNORHoIp6J"; break; case '2': $encodingAesKey = "i1YU8HYolgq3PkhmQgLnokDU2vOdeGJ3wFfSMTL4mcn"; $token = "kCL5zfI"; break; case '3': $encodingAesKey = "oO969aJa3KNcVy8tiYEB7fBCzJIOkEvd1sYVpd2RPmU"; $token = "RCNLgSDmHlIWsmO1w3"; break; case '13': $encodingAesKey = "agmE6ZaJt4X9IAMqTVTQkuhole4tmeCz6nuvudXpAdG"; $token = "a2X0UtCiDHqhR1"; break; } $corpId = "wx2575de58198c6b26"; $wxcpt = new WXBizMsgCrypt($token, $encodingAesKey, $corpId); $sReqMsgSig = $_GET["msg_signature"]; $sReqTimeStamp = $_GET["timestamp"]; $sReqNonce = $_GET["nonce"]; $sReqData = file_get_contents("php://input"); $sMsg = ""; $errCode = $wxcpt->DecryptMsg($sReqMsgSig, $sReqTimeStamp, $sReqNonce, $sReqData, $sMsg); //sMsg为用户输入message信息 return $sMsg; }
/** * event * */ public function event() { \Log::info($this->request->getRequestUri()); // $sReqMsgSig = HttpUtils.ParseUrl("msg_signature"); $sReqMsgSig = $this->request->get('msg_signature'); // $sReqTimeStamp = HttpUtils.ParseUrl("timestamp"); $sReqTimeStamp = $this->request->get('timestamp'); // $sReqNonce = HttpUtils.ParseUrl("nonce"); $sReqNonce = $this->request->get('nonce'); // post请求的密文数据 // $sReqData = HttpUtils.PostData(); \Log::info($sReqMsgSig); \Log::info($sReqTimeStamp); \Log::info($sReqNonce); $sReqData = (string) file_get_contents("php://input"); \Log::info($sReqData); // 假设企业号在公众平台上设置的参数如下 $encodingAesKey = self::ASE_KEY; $token = self::TOKEN; $corpId = self::CORP_ID; \Log::info($corpId); $wxcpt = new \WXBizMsgCrypt($token, $encodingAesKey, $corpId); $sMsg = ""; // 解析之后的明文 $errCode = $wxcpt->DecryptMsg($sReqMsgSig, $sReqTimeStamp, $sReqNonce, $sReqData, $sMsg); if ($errCode == 0) { // 解密成功,sMsg即为xml格式的明文 // TODO: 对明文的处理 // For example: $xml = new \DOMDocument(); $xml->loadXML($sMsg); $content = $xml->getElementsByTagName('SuiteTicket')->item(0)->nodeValue; \Log::info("content: " . $content . "\n\n"); // ... // ... } else { \Log::error("ERR: " . $errCode . "\n\n"); //exit(-1); } $this->setSuitTicketInRedis(self::SUIT_TICKET_KEY, $content); }