/** * 输出消息 * @return [encypted] msg */ private function sendout($msg) { if ($this->encrypted) { $errCode = $this->msgCryptor->encryptMsg($msg, $this->getRequest('timestamp'), $this->getRequest('nonce'), $msg); if ($errCode != 0) { exit($errCode); } } exit($msg); }
public function encodeMsg($sRespData) { import("@.ORG.aes.WXBizMsgCrypt"); $pc = new WXBizMsgCrypt($this->_config["wechat_token"], $this->_config["wechat_encodingaeskey"], $this->_config["wechat_appid"]); $sRespData = str_replace("<?xml version=\"1.0\"?>", "", $sRespData); $encryptMsg = ""; $errCode = $pc->encryptMsg($sRespData, $this->sTimeStamp, $this->nonce, $encryptMsg); if ($errCode == 0) { return $encryptMsg; } else { return $errCode; } }
public function encodeMsg($sRespData) { import('source.class.aes.WXBizMsgCrypt'); $pc = new WXBizMsgCrypt($this->_config['wx_token'], $this->_config['wx_encodingaeskey'], $this->_config['wx_appid']); $sRespData = str_replace('<?xml version="1.0"?>', '', $sRespData); $encryptMsg = ''; $errCode = $pc->encryptMsg($sRespData, $this->sTimeStamp, $this->nonce, $encryptMsg); if ($errCode == 0) { return $encryptMsg; } else { return $errCode; } }
public function encodeMsg($sRespData) { import("WXBizMsgCrypt", './source/class/aes'); $pc = new WXBizMsgCrypt($this->_config['wechat_token'], $this->_config['wechat_encodingaeskey'], $this->_config['wechat_appid']); $sRespData = str_replace('<?xml version="1.0"?>', '', $sRespData); $encryptMsg = ""; // 解析之后的明文 $errCode = $pc->encryptMsg($sRespData, $this->sTimeStamp, $this->nonce, $encryptMsg); if ($errCode == 0) { return $encryptMsg; } else { return $errCode; } }
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 encodeMsg($sRespData) { $sReqTimeStamp = time(); $sReqNonce = $_GET['nonce']; $encryptMsg = ""; import("@.ORG.aes.WXBizMsgCrypt"); $pc = new WXBizMsgCrypt($this->pigsecret, $this->wxuser['aeskey'], $this->wxuser['appid']); $sRespData = str_replace('<?xml version="1.0"?>', '', $sRespData); $errCode = $pc->encryptMsg($sRespData, $sReqTimeStamp, $sReqNonce, $encryptMsg); if ($errCode == 0) { return $encryptMsg; } else { return $errCode; } }
public function responseMsg() { $wxMsgCrypt = new WXBizMsgCrypt($this->token, $this->enaeskey, $this->appid); $Common = new Common(); $WxModel = new WxModel(); $postStr = file_get_contents("php://input"); $WxModel->recordLog($postStr); if (!empty($postStr)) { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $WxModel->recordLog('signature=' . $signature); $WxModel->recordLog('timestamp=' . $timestamp); $WxModel->recordLog('nonce=' . $nonce); $msg = ''; $errCode = $wxMsgCrypt->decryptMsg($signature, $timestamp, $nonce, $postStr, $msg); if ($errCode == 0) { $WxModel = new WxModel(); $WxModel->recordLog($msg); libxml_disable_entity_loader(true); $postObj = simplexml_load_string($msg, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $MsgType = $postObj->MsgType; $Event = $postObj->Event; $WxModel->recordLog('MsgType=' . $MsgType . ' Event=' . $Event); $keyword = trim($postObj->Content); $resultStr = $WxModel->fix($MsgType, $Event, $keyword, $fromUsername, $toUsername, $timestamp); $WxModel->recordLog($resultStr); if ($resultStr) { $encryptMsg = ''; $errCode = $wxMsgCrypt->encryptMsg($resultStr, $timestamp, $nonce, $encryptMsg); if ($errCode == 0) { echo $encryptMsg; } else { $WxModel->recordLog('encryptMsg:errCode=' . $errCode); } } } else { $WxModel->recordLog('decryptMsg:errCode=' . $errCode); } } else { echo ""; exit; } }
public function encryptMessage($dataArr = array()) { try { extract($dataArr); $returnArr = array('hasError' => false); if ($encrypt_type == 'aes') { $encryptMsg = ''; // encrypted message $pc = new WXBizMsgCrypt($this->token, $this->encodingAESKey, $this->appid); $errCode = $pc->encryptMsg($responseXML, $timestamp, $nonce, $encryptMsg); if ($errCode == 0) { // encrypt success $returnArr['responseStr'] = $encryptMsg; } else { // failed throw new Exception('Encryption error: ' . $errCode); } } else { $returnArr['responseStr'] = $responseXML; } } catch (Exception $ex) { # log the exception log_error('WeChat: decryptMessage', null, $ex->getMessage()); $returnArr = array('hasError' => true, $ex->getMessage()); } return $returnArr; }
<?php include_once "wxBizMsgCrypt.php"; // 第三方发送消息给公众平台 $encodingAesKey = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG"; $token = "pamtest"; $timeStamp = "1409304348"; $nonce = "xxxxxx"; $appId = "wxb11529c136998cb6"; $text = "<xml><ToUserName><![CDATA[oia2Tj我是中文jewbmiOUlr6X-1crbLOvLw]]></ToUserName><FromUserName><![CDATA[gh_7f083739789a]]></FromUserName><CreateTime>1407743423</CreateTime><MsgType><![CDATA[video]]></MsgType><Video><MediaId><![CDATA[eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0]]></MediaId><Title><![CDATA[testCallBackReplyVideo]]></Title><Description><![CDATA[testCallBackReplyVideo]]></Description></Video></xml>"; $pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId); $encryptMsg = ''; $errCode = $pc->encryptMsg($text, $timeStamp, $nonce, $encryptMsg); if ($errCode == 0) { print "加密后: " . $encryptMsg . "\n"; } else { print $errCode . "\n"; } $xml_tree = new DOMDocument(); $xml_tree->loadXML($encryptMsg); $array_e = $xml_tree->getElementsByTagName('Encrypt'); $array_s = $xml_tree->getElementsByTagName('MsgSignature'); $encrypt = $array_e->item(0)->nodeValue; $msg_sign = $array_s->item(0)->nodeValue; $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>"; $from_xml = sprintf($format, $encrypt); // 第三方收到公众号平台发送的消息 $msg = ''; $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg); if ($errCode == 0) { print "解密后: " . $msg . "\n";
private function route() { $nonce = isset($_REQUEST['nonce']) ? $_REQUEST['nonce'] : ''; $sTimeStamp = isset($_REQUEST['timestamp']) ? $_REQUEST['timestamp'] : time(); $msg_signature = isset($_REQUEST['msg_signature']) ? $_REQUEST['msg_signature'] : ''; $xml = $GLOBALS["HTTP_RAW_POST_DATA"]; import("@.ORG.aes.WXBizMsgCrypt"); $pc = new WXBizMsgCrypt($this->config['wx_token'], $this->config['wx_encodingaeskey'], $this->config['wx_appid']); $sMsg = ""; $pc->decryptMsg($msg_signature, $sTimeStamp, $nonce, $xml, $sMsg); $data = $this->api_notice_increment('http://we-cdn.net', $sMsg); $data = str_replace('<?xml version="1.0"?>', '', $data); $encryptMsg = ""; $pc->encryptMsg($data, $sTimeStamp, $nonce, $encryptMsg); return $encryptMsg; }
/** * 被动响应微信发送的信息(被动回复),密文模式已调通。 * @param string $to 接收用户名 * @param string $from 发送者用户名 * @param array $content 回复信息,文本信息为string类型 * @param string $type 消息类型 * @param string $flag 是否新标刚接受到的信息 * @return string XML字符串 */ public function response($content, $type = 'text', $flag = 0) { /* 原来SDK的代码:定义本类的基础数据$data */ $timenow = NOW_TIME; $this->data = array('ToUserName' => $this->data['FromUserName'], 'FromUserName' => $this->data['ToUserName'], 'CreateTime' => $timenow, 'MsgType' => $type); /* 添加类型数据 */ $this->{$type}($content); /* 添加状态 */ $this->data['FuncFlag'] = $flag; // (这里我先省去了,形参中原来有个$flag = 0标识自己服务器的新旧消息) /* 转换数据为XML */ $response = self::array2Xml($this->data); if ($this->is_auth == 0) { // 普通接入回复模式 if ($this->encode) { // 如果采用密文模式,则加密后再发送 $nonce = $_GET['nonce']; // $nonce用$_GET ['nonce']获取!2015/04/24,不可以自己生成一个,否则签名比对不上 //$nonce = md5 ( $timenow . randCode ( 4, 1 ) ); $xmlStr['Encrypt'] = $this->AESencode($response); // 采用密文模式加密 $xmlStr['MsgSignature'] = self::getSHA1($xmlStr['Encrypt'], $nonce); $xmlStr['TimeStamp'] = $timenow; $xmlStr['Nonce'] = $nonce; $response = ''; $response = self::array2Xml($xmlStr); // 将本类数组$data转换数据为XML } } else { if ($this->is_auth == 1) { // 开放平台回复模式 $appId = C('COMPONENT_APPID'); $token = C('COMPONENT_TOKEN'); $encodingAesKey = C('COMPONENT_ASEKEY'); $timestamp = $_GET['timestamp']; $nonce = $_GET['nonce']; $openresponse = ""; $msgcrypt = new WXBizMsgCrypt($token, $encodingAesKey, $appId); $errCode = $msgcrypt->encryptMsg($response, $timeStamp, $nonce, $openresponse); if ($errCode == 0) { $response = $openresponse; // 开放平台回包加密成功 } else { $response = ""; // 开放平台回包加密出错 } } } exit($response); // 输出结果回应微信 }
private function retRes($resultStr) { if ($this->restype == 'aes') { $pc = new WXBizMsgCrypt(TOKEN, $this->encodingAesKey, APP_ID); $encryptMsg = ''; $errCode = $pc->encryptMsg($resultStr, $_GET['timestamp'], $_GET['nonce'], $encryptMsg); if ($errCode > 0) { Yii::log($errCode, 'error', 'WX_MESSAGE_AES_ERROR'); } else { echo $encryptMsg; } } else { echo $resultStr; } }
/** * 向微信回复消息 * * @param YDWXAnswerMsg $msg */ function ydwx_answer_msg(YDWXAnswerMsg $msg) { ob_start(); if (YDWX_WEIXIN_COMPONENT_APP_ID) { //第三方平台要加密 $crypt = new WXBizMsgCrypt(YDWX_WEIXIN_COMPONENT_TOKEN, YDWX_WEIXIN_COMPONENT_ENCODING_AES_KEY, YDWX_WEIXIN_COMPONENT_APP_ID); $encryptMsg = ""; $crypt->encryptMsg($msg->toXMLString(), time(), uniqid(), $encryptMsg); echo $encryptMsg; } else { echo $msg->toXMLString(); } ob_end_flush(); }
public function platform_encryptMsg($xml) { $encodingAesKey = $this->platform_encodingAesKey; $token = $this->platform_token; $timeStamp = get_gmtime(); $nonce = rand(77, 999) * rand(605, 888) * rand(11, 99); $appId = $this->platform_appid; $text = $xml; $pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId); $encryptMsg = ''; $errCode = $pc->encryptMsg($text, $timeStamp, $nonce, $encryptMsg); if ($errCode == 0) { $this->log("加密后: " . $encryptMsg . "\n"); } else { $this->log($errCode . "\n"); } $xml_tree = new DOMDocument(); $xml_tree->loadXML($encryptMsg); $array_e = $xml_tree->getElementsByTagName('Encrypt'); $array_s = $xml_tree->getElementsByTagName('MsgSignature'); $encrypt = $array_e->item(0)->nodeValue; $msg_sign = $array_s->item(0)->nodeValue; //$format = "<xml><ToUserName><![CDATA[".$this->_msg['ToUserName']."]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>"; $format = "<xml><Encrypt><![CDATA[%s]]></Encrypt><MsgSignature><![CDATA[%s]]></MsgSignature><TimeStamp><![CDATA[%s]]></TimeStamp><Nonce><![CDATA[%s]]></Nonce></xml>"; $from_xml = sprintf($format, $encrypt, $msg_sign, $timeStamp, $nonce); return $from_xml; }