Пример #1
0
        //验证URL
        // @param sMsgSignature: 签名串,对应URL参数的msg_signature
        // @param sTimeStamp: 时间戳,对应URL参数的timestamp
        // @param sNonce: 随机串,对应URL参数的nonce
        // @param sEchoStr: 随机串,对应URL参数的echostr
        // @param sReplyEchoStr: 解密之后的echostr,当return返回0时有效
        // @return:成功0,失败返回对应的错误码
        public WeChatEncryptionErrorCode VerifyURL(string sMsgSignature, string sTimeStamp, string sNonce, string sEchoStr, ref string sReplyEchoStr)
        {
            WeChatEncryptionErrorCode ret = 0;

            if (M_sEncodingAESKey.Length != 43)
            {
                return(WeChatEncryptionErrorCode.IllegalAesKey);
            }
            ret = VerifySignature(M_sToken, sTimeStamp, sNonce, sEchoStr, sMsgSignature);
            if (0 != ret)
            {
                return(ret);
            }
            sReplyEchoStr = "";
            string cpid = "";

            try
            {
                sReplyEchoStr = WeChatCryptography.AES_decrypt(sEchoStr, M_sEncodingAESKey, ref cpid); //m_sCorpID);
            }
            catch (Exception)
            {
                sReplyEchoStr = "";
                return(WeChatEncryptionErrorCode.DecryptAES_Error);
            }
            if (cpid != M_sCorpID)
            {
                sReplyEchoStr = "";
                return(WeChatEncryptionErrorCode.ValidateCorpid_Error);
            }
            return(0);
        }
Пример #2
0
        // 检验消息的真实性,并且获取解密后的明文
        // @param sMsgSignature: 签名串,对应URL参数的msg_signature
        // @param sTimeStamp: 时间戳,对应URL参数的timestamp
        // @param sNonce: 随机串,对应URL参数的nonce
        // @param sPostData: 密文,对应POST请求的数据
        // @param sMsg: 解密后的原文,当return返回0时有效
        // @return: 成功0,失败返回对应的错误码
        public WeChatEncryptionErrorCode DecryptMsg(string sMsgSignature, string sTimeStamp, string sNonce, string sPostData, ref string sMsg)
        {
            if (M_sEncodingAESKey.Length != 43)
            {
                return(WeChatEncryptionErrorCode.IllegalAesKey);
            }
            XmlDocument doc = new XmlDocument();
            XmlNode     root;
            string      sEncryptMsg;

            try
            {
                doc.LoadXml(sPostData);
                root        = doc.FirstChild;
                sEncryptMsg = root["Encrypt"].InnerText;
            }
            catch (Exception)
            {
                return(WeChatEncryptionErrorCode.ParseXml_Error);
            }
            //verify signature
            WeChatEncryptionErrorCode ret = 0;

            ret = VerifySignature(M_sToken, sTimeStamp, sNonce, sEncryptMsg, sMsgSignature);
            if (ret != 0)
            {
                return(ret);
            }
            //decrypt
            string cpid = "";

            try
            {
                sMsg = WeChatCryptography.AES_decrypt(sEncryptMsg, M_sEncodingAESKey, ref cpid);
            }
            catch (FormatException)
            {
                sMsg = "";
                return(WeChatEncryptionErrorCode.DecodeBase64_Error);
            }
            catch (Exception)
            {
                sMsg = "";
                return(WeChatEncryptionErrorCode.DecryptAES_Error);
            }
            return(cpid != M_sCorpID ? WeChatEncryptionErrorCode.ValidateCorpid_Error : 0);
        }