【愚公系列】2022年10月 .Net Core使用cpolar内网穿透功能实现钉钉回调事件的监听
【摘要】 前言 1.cpolar简介cpolar是一款拥有远程控制和内网穿透功能的软件。而且还可以监控端口的HTTP请求,利用实时的cpolar Web UI开发者工具,让您调试代码更容易。您可以监听所有隧道上的HTTP消息包,分析消息包的结构内容,找出问题点。还可以单击重放(Replay)按钮,重新发送该HTTP信令请求。 2.cpolar功能1、个人使用场景:远程桌面访问公司电脑远程方面家中电脑...
前言
1.cpolar简介
cpolar是一款拥有远程控制和内网穿透功能的软件。而且还可以监控端口的HTTP请求,利用实时的cpolar Web UI开发者工具,让您调试代码更容易。您可以监听所有隧道上的HTTP消息包,分析消息包的结构内容,找出问题点。还可以单击重放(Replay)按钮,重新发送该HTTP信令请求。
2.cpolar功能
1、个人使用场景:
- 远程桌面访问公司电脑
- 远程方面家中电脑
- 搭建私人Web站点
2、中小企业远程办公场景:
- 远程桌面(个人或公司使用)
- 远程访问文件共享服务器(中小企业使用)
- 远程访问公司内网财务系统、进销存系统、ERP系统(小中企业使用)
3、游戏玩家使用场景:
- 搭建个人游戏私服
4、群晖NAS(个人网络存储服务器)用户使用
- 远程访问家中的NAS文件服务器Web管理界面
- 远程文件共享服务
- 远程用手机看视频(通过手机或PC远程访问家中的视频文件库,观看小姐姐)
- 为啥要用远程,因为NAS服务器通常也是BT迅雷下载服务器,小姐姐或电影都存在这上面。
5、开发人员使用场景:
- 搭建Web站点,用于测试,用于给客户演示场景
- 联调公网API服务,例如远程调试支付宝接口
- 联调开发微信公众号,小程序接口
- 远程访问数据库
- 搭建私有git源代码仓库
- 搭建私有CI服务器
- 搭建私有SVN源代码服务器
- 远程SSH服务器家中的服务器,或者公司的服务器
- 远程使用vs code编辑代码
6、批量商业使用场景:
- 开发智能终端的用户,希望在他们开发的每个终端上,都安装cpolar,用于可以随时ssh远程访问的能力。例如,初期100台设备上安装(试生产),成功后,再部署到1-4万台设备上。
- 企业用户,希望在公司的每台电脑上,都配置远程桌面功能,访问疫情期间,远程办公方便。
- 有一个店家,有5个店,他是老板,但他并不希望每次都跑过去查看电脑上的财务数据。可以安装cpolar到这5个店的电脑中,他只要在家中,远程访问即可。
- 私有云服务,企业希望打造一套自己的内网穿透系统,独立搭建一套私有云服务。
一、无公网IP异地远程连接内网群晖NAS【内网穿透】
1.1 注册cpolar账号
官网链接:https://i.cpolar.com/m/4VfC
进入cpolar官网,我们先点击右上角的免费注册
,使用邮箱注册一个账号,我们后面会需要用到。
1.2 下载cpolar客户端进行内网穿透
下载完一路安装就好了,安装完成进入安装的文件夹执行注册,注册的命令如下:
cpolar authtoken <YOUR_AUTH_TOKEN>
cpolar http 5000
执行成功界面如下
访问网址:https://458ed76c.vip.cpolar.cn/swagger/index.html
内网swagger映射成功
1.3 进行钉钉回调事件的监听测试
进入钉钉后台管理注册事件订阅
进行保存后端断点收到钉钉发过来信息,支持内网应用程序和钉钉已经进行互通了
1.4 进行钉钉回调事件的监听的源码解析如下
1.4.1 控制器代码
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NSwag.Annotations;
using System.Collections;
using YG.Monomer.Framework.Controllers.Other_Manage.DingDing;
using YG.Monomer.Framework.Filters.格式化返回结果;
namespace YG.Monomer.Framework.Controllers.Other_Manage
{
[Route("/Other_Manage/[controller]/[action]")]
[OpenApiTag("第三方回调-钉钉事件监听")]
public class DDCallBackController : BaseApiController
{
private readonly IConfiguration Configuration;
/// <summary>
/// 日志注入
/// </summary>
/// <param name="loggerFactory"></param>
public DDCallBackController(IConfiguration configuration)
{
Configuration = configuration;
}
[HttpGet]
[AllowAnonymous]
[NoFormatResponse]
public string ReadConfig()
{
return Configuration["DingDing:Token"];
}
/// <summary>
/// 钉钉注册的回调地址
/// </summary>
/// <param name="signature"></param>
/// <param name="timestamp"></param>
/// <param name="nonce"></param>
/// <param name="body"></param>
/// <returns></returns>
[HttpPost]
[AllowAnonymous]
[NoFormatResponse]
public dynamic CallBack(string signature, string timestamp, string nonce, PostBody body)
{
//接收encrypt参数
string encryptStr = body.encrypt.Replace("{\"encrypt\":\"", "").Replace("\"}", "");
//注册时填写的token、aes_key、suitekey
string token = Configuration["DingDing:Token"];
string aes_key = Configuration["DingDing:AesKey"];
string suitekey = Configuration["DingDing:Suitekey"];
#region 验证回调的url
DingTalkCrypt dingTalk = new DingTalkCrypt(token, aes_key, suitekey);
string sEchoStr = "";
dingTalk.VerifyURL(signature, timestamp, nonce, encryptStr, ref sEchoStr);
#endregion
#region 解密接受信息,进行事件处理
string plainText = "";
dingTalk.DecryptMsg(signature, timestamp, nonce, encryptStr, ref plainText);
Hashtable tb = (Hashtable)JsonConvert.DeserializeObject(plainText, typeof(Hashtable))!;
string eventType = tb["EventType"].ToString()!;
//string processCode = tb["processCode"] == null ? null : tb["processCode"].ToString();//任务码
string res = "success";
switch (eventType)
{
case "bpms_instance_change"://审批实例改变,执行代码
#region 审批实例改变,执行代码
//if (processCode == Configuration["PurProcessCode"])
//{
// _purdemitem.AddPurDem(_dDHelper.GetProcessInstance(tb["processInstanceId"].ToString()));
//}
#endregion
break;
default:
break;
}
timestamp = Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds).ToString();
string encrypt = "";
string signature2 = "";
dingTalk = new DingTalkCrypt(token, aes_key, suitekey);
//返回成功信息
dingTalk.EncryptMsg(res, timestamp, nonce, ref encrypt, ref signature2);
Hashtable jsonMap = new Hashtable
{
{"msg_signature", signature2},
{"encrypt", encrypt},
{"timeStamp", timestamp},
{"nonce", nonce}
};
return JsonConvert.SerializeObject(jsonMap);
#endregion
}
}
/// <summary>
/// 钉钉解密消息体
/// </summary>
public class PostBody
{
/// <summary>
/// 密文
/// </summary>
public string? encrypt { get; set; }
}
}
1.4.2 相关加解密代码
AESHepler.cs
using System.Net;
using System.Security.Cryptography;
using System.Text;
namespace YG.Monomer.Framework.Controllers.Other_Manage.DingDing
{
/// <summary>
/// 钉钉加解密库
/// </summary>
public class AESHepler
{
/// <summary>
/// 加密
/// </summary>
/// <param name="input">原文</param>
/// <param name="aesKey">AES密钥</param>
/// <param name="corpid">企业CorpId</param>
/// <returns></returns>
public static string AESEncrypt(string input, string aesKey, string corpid)
{
byte[] Key;
Key = Convert.FromBase64String(aesKey + "=");
byte[] Iv = new byte[16];
Array.Copy(Key, Iv, 16);
string Randcode = CreateRandCode(16);
byte[] bRand = Encoding.UTF8.GetBytes(Randcode);
byte[] bCorpid = Encoding.UTF8.GetBytes(corpid);
byte[] btmpMsg = Encoding.UTF8.GetBytes(input);
byte[] bMsgLen = BitConverter.GetBytes(HostToNetworkOrder(btmpMsg.Length));
byte[] bMsg = new byte[bRand.Length + bMsgLen.Length + bCorpid.Length + btmpMsg.Length];
Array.Copy(bRand, bMsg, bRand.Length);
Array.Copy(bMsgLen, 0, bMsg, bRand.Length, bMsgLen.Length);
Array.Copy(btmpMsg, 0, bMsg, bRand.Length + bMsgLen.Length, btmpMsg.Length);
Array.Copy(bCorpid, 0, bMsg, bRand.Length + bMsgLen.Length + btmpMsg.Length, bCorpid.Length);
return AESEncrypt(bMsg, Key, Iv);
}
/// <summary>
/// 解密
/// </summary>
/// <param name="input">密文</param>
/// <param name="aesKey">AES密钥</param>
/// <param name="corpId"></param>
/// <returns></returns>
public static string AESDecrypt(string input, string aesKey, ref string corpId)
{
byte[] Key;
Key = Convert.FromBase64String(aesKey + "=");
byte[] Iv = new byte[16];
Array.Copy(Key, Iv, 16);
byte[] btmpMsg = null;
using (var aesAlg = Aes.Create())
{
aesAlg.KeySize = 256;
aesAlg.BlockSize = 128;
aesAlg.Padding = PaddingMode.None;
aesAlg.Mode = CipherMode.CBC;
using (var decryptor = aesAlg.CreateDecryptor(Key, Iv))
{
using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
{
byte[] xXml = Convert.FromBase64String(input);
byte[] msg = new byte[xXml.Length + 32 - xXml.Length % 32];
Array.Copy(xXml, msg, xXml.Length);
cs.Write(xXml, 0, xXml.Length);
}
btmpMsg = decode2(ms.ToArray());
}
}
}
int len = BitConverter.ToInt32(btmpMsg, 16);
len = IPAddress.NetworkToHostOrder(len);
byte[] bMsg = new byte[len];
byte[] bCorpid = new byte[btmpMsg.Length - 20 - len];
Array.Copy(btmpMsg, 20, bMsg, 0, len);
Array.Copy(btmpMsg, 20 + len, bCorpid, 0, btmpMsg.Length - 20 - len);
string oriMsg = Encoding.UTF8.GetString(bMsg);
corpId = Encoding.UTF8.GetString(bCorpid);
return oriMsg;
}
/// <summary>
/// 加密算法
/// </summary>
/// <param name="input">原文</param>
/// <param name="key">密钥</param>
/// <param name="iv">IV偏移量</param>
/// <returns></returns>
public static string AESEncrypt(byte[] input, byte[] key, byte[] iv)
{
using (var aesAlg = Aes.Create())
{
aesAlg.KeySize = 256;
aesAlg.BlockSize = 128;
aesAlg.Padding = PaddingMode.None;
aesAlg.Mode = CipherMode.CBC;
byte[] msg = new byte[input.Length + 32 - input.Length % 32];
Array.Copy(input, msg, input.Length);
byte[] pad = KCS7Encoder(input.Length);
Array.Copy(pad, 0, msg, input.Length, pad.Length);
byte[] xBuff = null;
using (var encryptor = aesAlg.CreateEncryptor(key, iv))
{
using (var msEncrypt = new MemoryStream())
{
using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
cs.Write(msg, 0, msg.Length);
}
xBuff = ms.ToArray();
}
return Convert.ToBase64String(xBuff);
}
}
}
}
private static byte[] decode2(byte[] decrypted)
{
int pad = (int)decrypted[decrypted.Length - 1];
if (pad < 1 || pad > 32)
{
pad = 0;
}
byte[] res = new byte[decrypted.Length - pad];
Array.Copy(decrypted, 0, res, 0, decrypted.Length - pad);
return res;
}
/// <summary>
///
/// </summary>
/// <param name="codeLen"></param>
/// <returns></returns>
public static string CreateRandCode(int codeLen)
{
string codeSerial = "2,3,4,5,6,7,a,c,d,e,f,h,i,j,k,m,n,p,r,s,t,A,C,D,E,F,G,H,J,K,M,N,P,Q,R,S,U,V,W,X,Y,Z";
if (codeLen == 0)
{
codeLen = 16;
}
string[] arr = codeSerial.Split(',');
string code = "";
int randValue = -1;
Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < codeLen; i++)
{
randValue = rand.Next(0, arr.Length - 1);
code += arr[randValue];
}
return code;
}
/// <summary>
///
/// </summary>
/// <param name="inval"></param>
/// <returns></returns>
public static UInt32 HostToNetworkOrder(UInt32 inval)
{
UInt32 outval = 0;
for (int i = 0; i < 4; i++)
outval = (outval << 8) + ((inval >> (i * 8)) & 255);
return outval;
}
/// <summary>
///
/// </summary>
/// <param name="inval"></param>
/// <returns></returns>
public static Int32 HostToNetworkOrder(Int32 inval)
{
Int32 outval = 0;
for (int i = 0; i < 4; i++)
outval = (outval << 8) + ((inval >> (i * 8)) & 255);
return outval;
}
private static byte[] KCS7Encoder(int text_length)
{
int block_size = 32;
// 计算需要填充的位数
int amount_to_pad = block_size - (text_length % block_size);
if (amount_to_pad == 0)
{
amount_to_pad = block_size;
}
// 获得补位所用的字符
char pad_chr = chr(amount_to_pad);
string tmp = "";
for (int index = 0; index < amount_to_pad; index++)
{
tmp += pad_chr;
}
return Encoding.UTF8.GetBytes(tmp);
}
static char chr(int a)
{
byte target = (byte)(a & 0xFF);
return (char)target;
}
}
}
Cryptography.cs
using System.Text;
namespace YG.Monomer.Framework.Controllers.Other_Manage.DingDing
{
/// <summary>
///
/// </summary>
public class Cryptography
{
/// <summary>
///
/// </summary>
/// <param name="inval"></param>
/// <returns></returns>
public static UInt32 HostToNetworkOrder(UInt32 inval)
{
UInt32 outval = 0;
for (int i = 0; i < 4; i++)
outval = (outval << 8) + ((inval >> (i * 8)) & 255);
return outval;
}
/// <summary>
///
/// </summary>
/// <param name="inval"></param>
/// <returns></returns>
public static Int32 HostToNetworkOrder(Int32 inval)
{
Int32 outval = 0;
for (int i = 0; i < 4; i++)
outval = (outval << 8) + ((inval >> (i * 8)) & 255);
return outval;
}
/// <summary>
/// 解密方法
/// </summary>
/// <param name="Input">密文</param>
/// <param name="EncodingAESKey"></param>
/// <param name="corpid"></param>
/// <returns></returns>
public static string AES_decrypt(String Input, string EncodingAESKey, ref string corpid)
{
//扩展改造成Core版AES解密算法
return AESHepler.AESDecrypt(Input, EncodingAESKey, ref corpid);
}
/// <summary>
/// 加密方法
/// </summary>
/// <param name="Input"></param>
/// <param name="EncodingAESKey"></param>
/// <param name="corpid"></param>
/// <returns></returns>
public static String AES_encrypt(String Input, string EncodingAESKey, string corpid)
{
byte[] Key;
Key = Convert.FromBase64String(EncodingAESKey + "=");
byte[] Iv = new byte[16];
Array.Copy(Key, Iv, 16);
string Randcode = CreateRandCode(16);
byte[] bRand = Encoding.UTF8.GetBytes(Randcode);
byte[] bCorpid = Encoding.UTF8.GetBytes(corpid);
byte[] btmpMsg = Encoding.UTF8.GetBytes(Input);
byte[] bMsgLen = BitConverter.GetBytes(HostToNetworkOrder(btmpMsg.Length));
byte[] bMsg = new byte[bRand.Length + bMsgLen.Length + bCorpid.Length + btmpMsg.Length];
Array.Copy(bRand, bMsg, bRand.Length);
Array.Copy(bMsgLen, 0, bMsg, bRand.Length, bMsgLen.Length);
Array.Copy(btmpMsg, 0, bMsg, bRand.Length + bMsgLen.Length, btmpMsg.Length);
Array.Copy(bCorpid, 0, bMsg, bRand.Length + bMsgLen.Length + btmpMsg.Length, bCorpid.Length);
return AES_encrypt(bMsg, Iv, Key);
}
private static string CreateRandCode(int codeLen)
{
string codeSerial = "2,3,4,5,6,7,a,c,d,e,f,h,i,j,k,m,n,p,r,s,t,A,C,D,E,F,G,H,J,K,M,N,P,Q,R,S,U,V,W,X,Y,Z";
if (codeLen == 0)
{
codeLen = 16;
}
string[] arr = codeSerial.Split(',');
string code = "";
int randValue = -1;
Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < codeLen; i++)
{
randValue = rand.Next(0, arr.Length - 1);
code += arr[randValue];
}
return code;
}
private static String AES_encrypt(byte[] Input, byte[] Iv, byte[] Key)
{
//扩展改造成Core版AES加密算法
return AESHepler.AESEncrypt(Input, Key, Iv);
}
private static byte[] KCS7Encoder(int text_length)
{
int block_size = 32;
// 计算需要填充的位数
int amount_to_pad = block_size - (text_length % block_size);
if (amount_to_pad == 0)
{
amount_to_pad = block_size;
}
// 获得补位所用的字符
char pad_chr = chr(amount_to_pad);
string tmp = "";
for (int index = 0; index < amount_to_pad; index++)
{
tmp += pad_chr;
}
return Encoding.UTF8.GetBytes(tmp);
}
/**
* 将数字转化成ASCII码对应的字符,用于对明文进行补码
*
* @param a 需要转化的数字
* @return 转化得到的字符
*/
static char chr(int a)
{
byte target = (byte)(a & 0xFF);
return (char)target;
}
private static byte[] decode2(byte[] decrypted)
{
int pad = (int)decrypted[decrypted.Length - 1];
if (pad < 1 || pad > 32)
{
pad = 0;
}
byte[] res = new byte[decrypted.Length - pad];
Array.Copy(decrypted, 0, res, 0, decrypted.Length - pad);
return res;
}
}
}
DingTalkCrypt.cs
using System.Collections;
using System.Security.Cryptography;
using System.Text;
namespace YG.Monomer.Framework.Controllers.Other_Manage.DingDing
{
/// <summary>
///
/// </summary>
public class DingTalkCrypt
{
private string m_sEncodingAESKey;
private string m_sToken;
private string m_sSuiteKey;
/**ask getPaddingBytes key固定长度**/
private static int AES_ENCODE_KEY_LENGTH = 43;
/**加密随机字符串字节长度**/
//private static int RANDOM_LENGTH = 16;
enum DingTalkCryptErrorCode
{
/**成功**/
SUCCESS = 0,
/**加密明文文本非法**/
ENCRYPTION_PLAINTEXT_ILLEGAL = 900001,
/**加密时间戳参数非法**/
ENCRYPTION_TIMESTAMP_ILLEGAL = 900002,
/**加密随机字符串参数非法**/
ENCRYPTION_NONCE_ILLEGAL = 900003,
/**不合法的aeskey**/
AES_KEY_ILLEGAL = 900004,
/**签名不匹配**/
SIGNATURE_NOT_MATCH = 900005,
/**计算签名错误**/
COMPUTE_SIGNATURE_ERROR = 900006,
/**计算加密文字错误**/
COMPUTE_ENCRYPT_TEXT_ERROR = 900007,
/**计算解密文字错误**/
COMPUTE_DECRYPT_TEXT_ERROR = 900008,
/**计算解密文字长度不匹配**/
COMPUTE_DECRYPT_TEXT_LENGTH_ERROR = 900009,
/**计算解密文字suiteKey不匹配**/
COMPUTE_DECRYPT_TEXT_SuiteKey_ERROR = 900010,
};
/// <summary>
/// 构造函数
/// </summary>
/// <param name="token">钉钉开放平台上,开发者设置的token</param>
/// <param name="encodingAesKey">钉钉开放台上,开发者设置的EncodingAESKey</param>
/// <param name="suiteKey">钉钉开放平台上,开发者设置的suiteKey</param>
public DingTalkCrypt(string token, string encodingAesKey, string suiteKey)
{
m_sToken = token;
m_sSuiteKey = suiteKey;
m_sEncodingAESKey = encodingAesKey;
}
/// <summary>
/// 将消息加密,返回加密后字符串
/// </summary>
/// <param name="sReplyMsg">传递的消息体明文</param>
/// <param name="sTimeStamp">时间戳</param>
/// <param name="sNonce">随机字符串</param>
/// <param name="sEncryptMsg">加密后的消息信息</param>
/// <param name="signature"></param>
/// <returns>成功0,失败返回对应的错误码</returns>
public int EncryptMsg(string sReplyMsg, string sTimeStamp, string sNonce, ref string sEncryptMsg, ref string signature)
{
if (string.IsNullOrEmpty(sReplyMsg))
return (int)DingTalkCryptErrorCode.ENCRYPTION_PLAINTEXT_ILLEGAL;
if (string.IsNullOrEmpty(sTimeStamp))
return (int)DingTalkCryptErrorCode.ENCRYPTION_TIMESTAMP_ILLEGAL;
if (string.IsNullOrEmpty(sNonce))
return (int)DingTalkCryptErrorCode.ENCRYPTION_NONCE_ILLEGAL;
if (m_sEncodingAESKey.Length != AES_ENCODE_KEY_LENGTH)
return (int)DingTalkCryptErrorCode.AES_KEY_ILLEGAL;
string raw = "";
try
{
raw = Cryptography.AES_encrypt(sReplyMsg, m_sEncodingAESKey, m_sSuiteKey);
}
catch (Exception)
{
return (int)DingTalkCryptErrorCode.AES_KEY_ILLEGAL;
}
string msgSigature = "";
int ret = GenerateSignature(m_sToken, sTimeStamp, sNonce, raw, ref msgSigature);
sEncryptMsg = raw;
signature = msgSigature;
return ret;
}
/// <summary>
/// 密文解密
/// </summary>
/// <param name="sMsgSignature">签名串</param>
/// <param name="sTimeStamp">时间戳</param>
/// <param name="sNonce">随机串</param>
/// <param name="sPostData">密文</param>
/// <param name="sMsg">解密后的原文,当return返回0时有效</param>
/// <returns>成功0,失败返回对应的错误码</returns>
public int DecryptMsg(string sMsgSignature, string sTimeStamp, string sNonce, string sPostData, ref string sMsg)
{
if (m_sEncodingAESKey.Length != AES_ENCODE_KEY_LENGTH)
return (int)DingTalkCryptErrorCode.AES_KEY_ILLEGAL;
string sEncryptMsg = sPostData;
int ret = VerifySignature(m_sToken, sTimeStamp, sNonce, sEncryptMsg, sMsgSignature);
string cpid = "";
try
{
sMsg = Cryptography.AES_decrypt(sEncryptMsg, m_sEncodingAESKey, ref cpid);
}
catch (FormatException)
{
sMsg = "";
return (int)DingTalkCryptErrorCode.COMPUTE_DECRYPT_TEXT_SuiteKey_ERROR;
}
catch (Exception)
{
sMsg = "";
return (int)DingTalkCryptErrorCode.COMPUTE_DECRYPT_TEXT_SuiteKey_ERROR;
}
if (cpid != m_sSuiteKey)
return (int)DingTalkCryptErrorCode.COMPUTE_DECRYPT_TEXT_SuiteKey_ERROR;
return ret;
}
/// <summary>
/// 生成签名
/// </summary>
/// <param name="sToken"></param>
/// <param name="sTimeStamp"></param>
/// <param name="sNonce"></param>
/// <param name="sMsgEncrypt"></param>
/// <param name="sMsgSignature"></param>
/// <returns></returns>
public static int GenerateSignature(string sToken, string sTimeStamp, string sNonce, string sMsgEncrypt, ref string sMsgSignature)
{
ArrayList AL = new ArrayList();
AL.Add(sToken);
AL.Add(sTimeStamp);
AL.Add(sNonce);
AL.Add(sMsgEncrypt);
AL.Sort(new DictionarySort());
string raw = "";
for (int i = 0; i < AL.Count; ++i)
{
raw += AL[i];
}
SHA1 sha;
ASCIIEncoding enc;
string hash = "";
try
{
sha = SHA1.Create();
enc = new ASCIIEncoding();
byte[] dataToHash = enc.GetBytes(raw);
byte[] dataHashed = sha.ComputeHash(dataToHash);
hash = BitConverter.ToString(dataHashed).Replace("-", "");
hash = hash.ToLower();
}
catch (Exception)
{
return (int)DingTalkCryptErrorCode.COMPUTE_SIGNATURE_ERROR;
}
sMsgSignature = hash;
return 0;
}
/// <summary>
/// 验证签名
/// </summary>
/// <param name="sToken"></param>
/// <param name="sTimeStamp"></param>
/// <param name="sNonce"></param>
/// <param name="sMsgEncrypt"></param>
/// <param name="sSigture"></param>
/// <returns></returns>
private static int VerifySignature(string sToken, string sTimeStamp, string sNonce, string sMsgEncrypt, string sSigture)
{
string hash = "";
int ret = 0;
ret = GenerateSignature(sToken, sTimeStamp, sNonce, sMsgEncrypt, ref hash);
if (ret != 0)
return ret;
if (hash == sSigture)
return 0;
else
{
return (int)DingTalkCryptErrorCode.SIGNATURE_NOT_MATCH;
}
}
/// <summary>
/// 验证URL
/// </summary>
/// <param name="sMsgSignature">签名串,对应URL参数的msg_signature</param>
/// <param name="sTimeStamp">时间戳,对应URL参数的timestamp</param>
/// <param name="sNonce">随机串,对应URL参数的nonce</param>
/// <param name="sEchoStr">经过加密的消息体,对应URL参数的encrypt</param>
/// <param name="sReplyEchoStr"></param>
/// <returns></returns>
public int VerifyURL(string sMsgSignature, string sTimeStamp, string sNonce, string sEchoStr, ref string sReplyEchoStr)
{
int ret = 0;
if (m_sEncodingAESKey.Length != 43)
{
return (int)DingTalkCryptErrorCode.AES_KEY_ILLEGAL;
}
ret = VerifySignature(m_sToken, sTimeStamp, sNonce, sEchoStr, sMsgSignature);
sReplyEchoStr = "";
string cpid = "";
try
{
sReplyEchoStr = Cryptography.AES_decrypt(sEchoStr, m_sEncodingAESKey, ref cpid); //m_sCorpID);
}
catch (Exception)
{
sReplyEchoStr = "";
return (int)DingTalkCryptErrorCode.COMPUTE_SIGNATURE_ERROR;
}
if (cpid != m_sSuiteKey)
{
sReplyEchoStr = "";
return (int)DingTalkCryptErrorCode.COMPUTE_DECRYPT_TEXT_SuiteKey_ERROR;
}
return ret;
}
/// <summary>
/// 字典排序
/// </summary>
public class DictionarySort : System.Collections.IComparer
{
/// <summary>
///
/// </summary>
/// <param name="oLeft"></param>
/// <param name="oRight"></param>
/// <returns></returns>
public int Compare(object oLeft, object oRight)
{
string sLeft = oLeft as string;
string sRight = oRight as string;
int iLeftLength = sLeft.Length;
int iRightLength = sRight.Length;
int index = 0;
while (index < iLeftLength && index < iRightLength)
{
if (sLeft[index] < sRight[index])
return -1;
else if (sLeft[index] > sRight[index])
return 1;
else
index++;
}
return iLeftLength - iRightLength;
}
}
}
}
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)