Пример #1
0
        // 检验消息的真实性,并且获取解密后的明文
        // @param sMsgSignature: 签名串,对应URL参数的msg_signature
        // @param sTimeStamp: 时间戳,对应URL参数的timestamp
        // @param sNonce: 随机串,对应URL参数的nonce
        // @param sPostData: 密文,对应POST请求的数据
        // @param sMsg: 解密后的原文,当return返回0时有效
        // @return: 成功0,失败返回对应的错误码
        public int DecryptMsg(IWxMsgToken token, ref string sMsg)
        {
            if (m_sEncodingAESKey.Length != 43)
            {
                return((int)WXBizMsgCryptErrorCode.WXBizMsgCrypt_IllegalAesKey);
            }
            XmlDocument doc = new XmlDocument();
            XmlNode     root;
            string      sEncryptMsg;

            try
            {
                doc.LoadXml(token.ReqMsg);
                root        = doc.FirstChild;
                sEncryptMsg = root["Encrypt"].InnerText;
            }
            catch (Exception)
            {
                return((int)WXBizMsgCryptErrorCode.WXBizMsgCrypt_ParseXml_Error);
            }
            //verify signature
            int ret = 0;

            ret = VerifySignature(m_sToken, token.TimeStamp, token.Nonce, sEncryptMsg, token.Signature);
            if (ret != 0)
            {
                return(ret);
            }
            //decrypt
            string cpid = "";

            try
            {
                sMsg = Cryptography.AES_decrypt(sEncryptMsg, m_sEncodingAESKey, ref cpid);
            }
            catch (FormatException)
            {
                return((int)WXBizMsgCryptErrorCode.WXBizMsgCrypt_DecodeBase64_Error);
            }
            catch (Exception)
            {
                return((int)WXBizMsgCryptErrorCode.WXBizMsgCrypt_DecryptAES_Error);
            }
            if (cpid != m_sAppID)
            {
                return((int)WXBizMsgCryptErrorCode.WXBizMsgCrypt_ValidateAppid_Error);
            }
            return(0);
        }
Пример #2
0
        public void Handle(IWxMsgToken token)
        {
            var crypt  = new WXBizMsgCrypt(token);
            var reqMsg = string.Empty;
            var retVal = crypt.DecryptMsg(token, ref reqMsg);

            if (retVal == 0)//解密收到的消息内容
            {
                Handle(reqMsg);
            }
            else
            {
                Logger.Error("消息解密出错 error code: {0}", retVal);
            }
        }
Пример #3
0
 //构造函数
 // @param sToken: 公众平台上,开发者设置的Token
 // @param sEncodingAESKey: 公众平台上,开发者设置的EncodingAESKey
 // @param sAppID: 公众帐号的appid
 public WXBizMsgCrypt(IWxMsgToken token)
 {
     m_sToken          = token.BizMsgToken;
     m_sAppID          = token.AppId;
     m_sEncodingAESKey = token.EncodingAESKey;
 }