示例#1
0
        public void EncryptDecrypt()
        {
            var criptAES = new Cryptography();

            var plainTextEncrypted = criptAES.AESEncrypt(PLAIN_TEXT, KEY);

            var plainTextDecrypted = criptAES.AESDecrypt(plainTextEncrypted, KEY);

            plainTextDecrypted.Should().Be(PLAIN_TEXT);
        }
示例#2
0
        public ActionResult WeiXinLogin(string code, string rawData, string signature, string encryptedData, string iv)
        {
            JsonStateResult j = new JsonStateResult();

            if (string.IsNullOrEmpty(code) || string.IsNullOrEmpty(rawData) || string.IsNullOrEmpty(signature) || string.IsNullOrEmpty(encryptedData) || string.IsNullOrEmpty(iv))
            {
                j.Msg = "缺失参数";
                return(Json(j, JsonRequestBehavior.AllowGet));
            }
            string result     = CommonVariable.HttpGet(string.Format("https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code", CommonVariable.appid, CommonVariable.secret, code));
            string openId     = string.Empty;
            string sessionKey = string.Empty;
            Dictionary <string, string> dicResult = JsonConverter.DeserializeObject <Dictionary <string, string> >(result);

            if (!dicResult.ContainsKey("openid") || !dicResult.ContainsKey("session_key"))
            {
                return(Json(j, JsonRequestBehavior.AllowGet));
            }
            openId     = dicResult["openid"];
            sessionKey = dicResult["session_key"];
            string signature2 = (rawData + sessionKey).ToSHA1();

            if (string.Compare(signature, signature2, true) != 0)
            {
                j.Msg = "非法请求,签名校验失败";
                return(Json(j, JsonRequestBehavior.AllowGet));
            }
            string data = Cryptography.AESDecrypt(encryptedData, sessionKey, iv);

            if (string.IsNullOrEmpty(data))
            {
                j.Msg = "AES解密出错";
                return(Json(j, JsonRequestBehavior.AllowGet));
            }
            WeiXinLoginUser user = JsonConverter.DeserializeObject <WeiXinLoginUser>(data);

            if (string.IsNullOrEmpty(user.openId) || string.IsNullOrEmpty(user.avatarUrl) || string.IsNullOrEmpty(user.nickName))
            {
                j.Msg = "json反序列化出错";
                return(Json(j, JsonRequestBehavior.AllowGet));
            }
            j.Error = 0;
            j.Data  = user;
            return(Json(j, JsonRequestBehavior.AllowGet));
        }
        /// <summary>
        /// 核心执行 过程的  验签和解密
        /// </summary>
        /// <returns>验证结果及相应的消息内容体 (如果加密模式,返回的是解密后的明文)</returns>
        private static StrResp Prepare(WechatMsgConfig appConfig, WechatRequestPara reqBody)
        {
            var isEncryptMsg = appConfig.SecurityType == WechatSecurityType.Safe;

            if (!isEncryptMsg)
            {
                var resCheck =
                    WechatChatHelper.CheckSignature(appConfig.Token, reqBody.signature, reqBody.timestamp, reqBody.nonce, string.Empty);
                return(!resCheck.IsSuccess() ? new StrResp().WithResp(resCheck) : new StrResp(reqBody.body));
            }

            if (string.IsNullOrEmpty(reqBody.msg_signature))
            {
                return(new StrResp().WithResp(RespTypes.ParaError, "msg_signature 消息体验证签名参数为空!"));
            }

            var xmlDoc   = WechatChatHelper.GetXmlDocment(reqBody.body);
            var encryStr = xmlDoc?.FirstChild["Encrypt"]?.InnerText;

            if (string.IsNullOrEmpty(encryStr))
            {
                return(new StrResp().WithResp(RespTypes.OperateObjectNull, "安全接口的加密字段为空!"));
            }

            var cryptMsgCheck =
                WechatChatHelper.CheckSignature(appConfig.Token, reqBody.msg_signature, reqBody.timestamp, reqBody.nonce, encryStr);

            if (!cryptMsgCheck.IsSuccess())
            {
                return(new StrResp().WithResp(cryptMsgCheck));
            }

            var recMsgXml = Cryptography.AESDecrypt(encryStr, appConfig.EncodingAesKey);

            return(new StrResp(recMsgXml));
        }