PFinal\Wechat\SDK\WXBizMsgCrypt::decryptMsg PHP Method

decryptMsg() public method

  1. 利用收到的密文生成安全签名,进行签名验证
  2. 若验证通过,则提取xml中的加密消息
  3. 对消息进行解密
public decryptMsg ( $msgSignature, $timestamp = null, $nonce, $postData, &$msg ) : integer
$msgSignature string 签名串,对应URL参数的msg_signature
$timestamp string 时间戳 对应URL参数的timestamp
$nonce string 随机串,对应URL参数的nonce
$postData string 密文,对应POST请求的数据
return integer 成功0,失败返回对应的错误码
    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;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * 解密
  * @param string $msgSignature
  * @param string $timestamp
  * @param string $nonce
  * @param string $encryptMsg
  * @return string
  * @throws WechatException
  */
 protected function decryptMsg($msgSignature, $timestamp, $nonce, $encryptMsg)
 {
     $msg = '';
     //传入公众号第三方平台的token(申请公众号第三方平台时填写的接收消息的校验token), 公众号第三方平台的appid, 公众号第三方平台的 EncodingAESKey(申请公众号第三方平台时填写的接收消息的加密symmetric_key)
     $pc = new WXBizMsgCrypt($this->token, $this->encodingAesKey, $this->appId);
     // 第三方收到公众号平台发送的消息
     $errCode = $pc->decryptMsg($msgSignature, $timestamp, $nonce, $encryptMsg, $msg);
     if ($errCode == 0) {
         Log::debug((string) $msg);
         return $msg;
     }
     throw new WechatException('decrypt msg error. error code ' . $errCode);
 }