/** * 将公众平台回复用户的消息加密打包. * <ol> * <li>对要发送的消息进行AES-CBC加密</li> * <li>生成安全签名</li> * <li>将消息密文和安全签名打包成xml格式</li> * </ol> * * @param $replyMsg string 公众平台待回复用户的消息,xml格式的字符串 * @param $timeStamp string 时间戳,可以自己生成,也可以用URL参数的timestamp * @param $nonce string 随机串,可以自己生成,也可以用URL参数的nonce * @param &$encryptMsg string 加密后的可以直接回复用户的密文,包括msg_signature, timestamp, nonce, encrypt的xml格式的字符串, * 当return返回0时有效 * * @return int 成功0,失败返回对应的错误码 */ public function encryptMsg($replyMsg, $timeStamp, $nonce, &$encryptMsg) { $pc = new Prpcrypt($this->encodingAesKey); //加密 $array = $pc->encrypt($replyMsg, $this->appId); $ret = $array[0]; if ($ret != 0) { return $ret; } if ($timeStamp == null) { $timeStamp = time(); } $encrypt = $array[1]; //生成安全签名 $sha1 = new SHA1(); $array = $sha1->getSHA1($this->token, $timeStamp, $nonce, $encrypt); $ret = $array[0]; if ($ret != 0) { return $ret; } $signature = $array[1]; //生成发送的xml $xmlparse = new XMLParse(); $encryptMsg = $xmlparse->generate($encrypt, $signature, $timeStamp, $nonce); return ErrorCode::$OK; }
public function encrypt($replyMsg, $timeStamp, $nonce, &$encryptMsg) { $pc = new Prpcrypt($this->encodingAesKey); $array = $pc->encrypt($replyMsg, $this->appId); $ret = $array[0]; if ($ret != 0) { return $ret; } if ($timeStamp == null) { $timeStamp = time(); } $encrypt = $array[1]; $sha1 = new SHA1; $array = $sha1->getSHA1($this->token, $timeStamp, $nonce, $encrypt); $ret = $array[0]; if ($ret != 0) { return $ret; } $signature = $array[1]; $format = "<xml> <Encrypt><![CDATA[%s]]></Encrypt> <MsgSignature><![CDATA[%s]]></MsgSignature> <TimeStamp>%s</TimeStamp> <Nonce><![CDATA[%s]]></Nonce> </xml>"; $encryptMsg = sprintf($format, $encrypt, $signature, $timeStamp, $nonce); return ErrorCode::$OK; }
public function EncryptMsg($plain, $timeStamp, $nonce, &$encryptMsg) { $pc = new Prpcrypt($this->m_encodingAesKey); $array = $pc->encrypt($plain, $this->m_suiteKey); $ret = $array[0]; if ($ret != 0) { return $ret; } if ($timeStamp == null) { $timeStamp = time(); } $encrypt = $array[1]; $sha1 = new SHA1(); $array = $sha1->getSHA1($this->m_token, $timeStamp, $nonce, $encrypt); $ret = $array[0]; if ($ret != 0) { return $ret; } $signature = $array[1]; $encryptMsg = json_encode(array("msg_signature" => $signature, "encrypt" => $encrypt, "timeStamp" => $timeStamp, "nonce" => $nonce)); return ErrorCode::$OK; }
/** * * 回复微信服务器, 此函数支持链式操作 * Example: $this->text('msg tips')->reply(); * @param string $msg 要发送的信息, 默认取$this->_msg * @param bool $return 是否返回信息而不抛出到浏览器 默认:否 */ public function reply($msg = array(), $return = false) { if (empty($msg)) { if (empty($this->_msg)) { //防止不先设置回复内容,直接调用reply方法导致异常 return false; } $msg = $this->_msg; } $xmldata = $this->xml_encode($msg); $this->log($xmldata); if ($this->encrypt_type == 'aes') { //如果来源消息为加密方式 $pc = new Prpcrypt($this->encodingAesKey); $array = $pc->encrypt($xmldata, $this->appid); $ret = $array[0]; if ($ret != 0) { $this->log('encrypt err!'); return false; } $timestamp = time(); $nonce = rand(77, 999) * rand(605, 888) * rand(11, 99); $encrypt = $array[1]; $tmpArr = array($this->token, $timestamp, $nonce, $encrypt); //比普通公众平台多了一个加密的密文 sort($tmpArr, SORT_STRING); $signature = implode($tmpArr); $signature = sha1($signature); $xmldata = $this->generate($encrypt, $signature, $timestamp, $nonce); $this->log($xmldata); } if ($return) { return $xmldata; } else { echo $xmldata; } }
<?php include_once "request.php"; $action = @get("action"); $source = @post("source"); $aesKey = @post("key"); $no = @post("no"); if (isset($source)) { $pc = new Prpcrypt($aesKey); if ($action == "encrypt") { $result = $pc->encrypt($source, $no); } else { $result = $pc->decrypt($source); // var_dump($result); } $response = array("success" => true, "result" => $result); printf(json_encode($response)); } /** * PKCS7Encoder class * * 提供基于PKCS7算法的加解密接口. */ class PKCS7Encoder { public static $block_size = 32; /** * 对需要加密的明文进行填充补位 * @param $text 需要进行填充补位操作的明文 * @return 补齐明文字符串 */