songjun
2024-09-04 cc908053e0b5075fbfd27350b6da4b39bca9e550
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Tencent;
 
namespace BatchService.Framework.Utility
{
    public class CorpBasicApi
    {
        /// <summary>
        /// 验证企业号签名
        /// </summary>
        /// <param name="token">企业号配置的Token</param>
        /// <param name="signature">签名内容</param>
        /// <param name="timestamp">时间戳</param>
        /// <param name="nonce">nonce参数</param>
        /// <param name="corpId">企业号ID标识</param>
        /// <param name="encodingAESKey">加密键</param>
        /// <param name="echostr">内容字符串</param>
        /// <param name="retEchostr">返回的字符串</param>
        /// <returns></returns>
        public bool CheckSignature(string token, string signature, string timestamp, string nonce, string corpId, string encodingAESKey, string echostr, ref string retEchostr)
        {
            WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token, encodingAESKey, corpId);
            int result = wxcpt.VerifyURL(signature, timestamp, nonce, echostr, ref retEchostr);
            if (result != 0)
            {
                LogHelper.Error("ERR: VerifyURL fail, ret: " + result);
                return false;
            }
 
            return true;
 
            //ret==0表示验证成功,retEchostr参数表示明文,用户需要将retEchostr作为get请求的返回参数,返回给企业号。
            // HttpUtils.SetResponse(retEchostr);
        }
 
        public bool DecodeMessage(string i_token, string i_encodingAESKey, string i_corpId,string i_msg_signature, string i_sReqTimeStamp, string i_sReqNonce, string i_sReqData,ref string o_ret)
        {
            bool t_is_deocde = false;
 
            WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(i_token, i_encodingAESKey, i_corpId);
            int t_ret = wxcpt.DecryptMsg(i_msg_signature, i_sReqTimeStamp, i_sReqNonce, i_sReqData, ref o_ret);
 
            if (t_ret != 0)
            {
                LogHelper.Error("ERR: DecryptMsg fail, ret: " + t_ret);
                return false;
            }
            //LogHelper.Info(o_ret);
 
            return true;
        }
 
        public bool EncodeMessage(string i_token, string i_encodingAESKey, string i_corpId, string i_sReqTimeStamp, string i_sReqNonce, string i_sReqData, ref string o_ret)
        {
            bool t_is_deocde = false;
 
            WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(i_token, i_encodingAESKey, i_corpId);
            int t_ret = wxcpt.EncryptMsg(i_sReqData, i_sReqTimeStamp, i_sReqNonce, ref o_ret);
 
            if (t_ret != 0)
            {
                LogHelper.Error("ERR: DecryptMsg fail, ret: " + t_ret);
                return false;
            }
 
            return t_is_deocde;
        }
    }
}