123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- include_once "sha1.php";
- include_once "xmlparse.php";
- include_once "pkcs7Encoder.php";
- include_once "errorCode.php";
- class WXBizMsgCrypt
- {
- private $token;
- private $encodingAesKey;
- private $appId;
-
- public function WXBizMsgCrypt($token, $encodingAesKey, $appId)
- {
- $this->token = $token;
- $this->encodingAesKey = $encodingAesKey;
- $this->appId = $appId;
- }
-
- 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];
-
- $xmlparse = new XMLParse;
- $encryptMsg = $xmlparse->generate($encrypt, $signature, $timeStamp, $nonce);
- return ErrorCode::$OK;
- }
-
- public function decryptMsg($msgSignature, $timestamp = null, $nonce, $postData, &$msg)
- {
- if (strlen($this->encodingAesKey) != 43) {
- return ErrorCode::$IllegalAesKey;
- }
- $pc = new Prpcrypt($this->encodingAesKey);
-
- $xmlparse = new XMLParse;
- $array = $xmlparse->extract($postData);
- $ret = $array[0];
- if ($ret != 0) {
- return $ret;
- }
- if ($timestamp == null) {
- $timestamp = time();
- }
- $encrypt = $array[1];
- $touser_name = $array[2];
-
- $sha1 = new SHA1;
- $array = $sha1->getSHA1($this->token, $timestamp, $nonce, $encrypt);
- $ret = $array[0];
- if ($ret != 0) {
- return $ret;
- }
- $signature = $array[1];
- if ($signature != $msgSignature) {
- return ErrorCode::$ValidateSignatureError;
- }
- $result = $pc->decrypt($encrypt, $this->appId);
- if ($result[0] != 0) {
- return $result[0];
- }
- $msg = $result[1];
- return ErrorCode::$OK;
- }
- }
|