/// <summary> /// 核心执行 过程的 验签和解密 /// </summary> /// <param name="recXml">消息内容</param> /// <param name="signature">微信加密签名</param> /// <param name="timestamp">时间戳</param> /// <param name="nonce">随机数</param> /// <returns>验证结果及相应的消息内容体 (如果加密模式,返回的是解密后的明文)</returns> private Resp <string> PrepareExecute(string recXml, string signature, string timestamp, string nonce) { if (string.IsNullOrEmpty(recXml)) { return(new Resp <string>().WithResp(RespTypes.ObjectNull, "接收的消息体为空!")); } var resCheck = WXChatHelper.CheckSignature(ApiConfig.Token, signature, timestamp, nonce); if (!resCheck.IsSuccess()) { return(new Resp <string>().WithResp(resCheck));// resCheck.ConvertToResult<string>(); } if (ApiConfig.SecurityType == WXSecurityType.None) { return(new Resp <string>(recXml)); } var dirs = WXChatHelper.ChangXmlToDir(recXml, out XmlDocument xmlDoc); if (dirs == null || !dirs.TryGetValue("Encrypt", out var encryStr) || string.IsNullOrEmpty(encryStr)) { return(new Resp <string>().WithResp(RespTypes.ObjectNull, "加密消息为空")); } var recMsgXml = Cryptography.WXAesDecrypt(encryStr, ApiConfig.EncodingAesKey); return(new Resp <string>(recMsgXml)); }
/// <summary> /// 核心执行 过程的 验签和解密 /// </summary> /// <returns>验证结果及相应的消息内容体 (如果加密模式,返回的是解密后的明文)</returns> private static StrResp Prepare(WXChatConfig appConfig, string recXml, string signature, string msg_signature, string timestamp, string nonce) { var isEncryptMsg = appConfig.SecurityType == WXSecurityType.Safe; if (!isEncryptMsg) { var resCheck = WXChatHelper.CheckSignature(appConfig.Token, signature, timestamp, nonce, String.Empty); return(!resCheck.IsSuccess() ? new StrResp().WithResp(resCheck) : new StrResp(recXml)); } if (string.IsNullOrEmpty(msg_signature)) { return(new StrResp().WithResp(RespTypes.ParaError, "msg_signature 消息体验证签名参数为空!")); } var xmlDoc = WXChatHelper.GetXmlDocment(recXml); var encryStr = xmlDoc?.FirstChild["Encrypt"]?.InnerText; if (string.IsNullOrEmpty(encryStr)) { return(new StrResp().WithResp(RespTypes.ObjectNull, "安全接口的加密字段为空!")); } var cryptMsgCheck = WXChatHelper.CheckSignature(appConfig.Token, msg_signature, timestamp, nonce, encryStr); if (!cryptMsgCheck.IsSuccess()) { return(new StrResp().WithResp(cryptMsgCheck)); } var recMsgXml = Cryptography.AESDecrypt(encryStr, appConfig.EncodingAesKey); return(new StrResp(recMsgXml)); }
/// <summary> /// 服务器验证 /// </summary> /// <param name="signature"></param> /// <param name="timestamp"></param> /// <param name="nonce"></param> /// <param name="echostr"></param> /// <returns></returns> public Resp <string> CheckServerValid(string signature, string timestamp, string nonce, string echostr) { var checkSignRes = WXChatHelper.CheckSignature(ApiConfig.Token, signature, timestamp, nonce); var resultRes = new Resp <string>().WithResp(checkSignRes);// checkSignRes.ConvertToResult<string>(); resultRes.data = resultRes.IsSuccess() ? echostr : string.Empty; return(resultRes); }