110 lines
4.2 KiB
C#
110 lines
4.2 KiB
C#
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace AllBase
|
|
{
|
|
public static class SignHelper
|
|
{
|
|
public static string GetSign()
|
|
{
|
|
string url = BaseClass.LoginApiUrl + "?obj=littlecow/sign&act=get_sign";
|
|
string sign = GetHtml.GetUtf8(url, BaseClass.ChromeUserAgent);
|
|
if (!string.IsNullOrEmpty(sign))
|
|
{
|
|
Encoding utf8 = Encoding.UTF8;
|
|
Encoding ascii = Encoding.ASCII;
|
|
|
|
string input = sign;
|
|
sign = ascii.GetString(Encoding.Convert(utf8, ascii, utf8.GetBytes(input)));
|
|
sign = sign.Trim('?');
|
|
}
|
|
return sign;
|
|
}
|
|
|
|
public static string DESDecrypt(string decrypt, string key = "")
|
|
{
|
|
if (string.IsNullOrEmpty(key))
|
|
{
|
|
key = "20190813";
|
|
}
|
|
try
|
|
{
|
|
DESCryptoServiceProvider oDESCryptoServiceProvider = new DESCryptoServiceProvider();
|
|
|
|
byte[] inputByteArray = new byte[decrypt.Length / 2];
|
|
for (int x = 0; x < decrypt.Length / 2; x++)
|
|
{
|
|
int i = (Convert.ToInt32(decrypt.Substring(x * 2, 2), 16));
|
|
inputByteArray[x] = (byte)i;
|
|
}
|
|
oDESCryptoServiceProvider.Key = ASCIIEncoding.ASCII.GetBytes(key);
|
|
oDESCryptoServiceProvider.IV = ASCIIEncoding.ASCII.GetBytes(key);
|
|
MemoryStream oMemoryStream = new MemoryStream();
|
|
CryptoStream oCryptoStream = new CryptoStream(oMemoryStream, oDESCryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Write);
|
|
oCryptoStream.Write(inputByteArray, 0, inputByteArray.Length);
|
|
oCryptoStream.FlushFinalBlock();
|
|
return Encoding.Default.GetString(oMemoryStream.ToArray());
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
LogSave.Add(ex.ToString());
|
|
return string.Empty;
|
|
}
|
|
}
|
|
|
|
public static Dictionary<string, object> BuildSign(Dictionary<string, object> dict, string clientSecret)
|
|
{
|
|
var result = new Dictionary<string, object>();
|
|
// 去除空值并排序
|
|
dict = dict.Where(d => d.Value != null)
|
|
.OrderBy(d => d.Key)
|
|
.ToDictionary((d) => d.Key, (d) => d.Value);
|
|
// 拼接
|
|
string signString = "";
|
|
// 反射处理非基本类型字段的json转换
|
|
string[] types = { "String", "DateTime", "Int64", "Boolean", "Float", "Double", "Long", "Int32" };
|
|
foreach (var item in dict.Keys.ToArray())
|
|
{
|
|
if (!types.Contains(dict[item]?.GetType().Name))
|
|
{
|
|
dict[item] = JsonConvert.SerializeObject(dict[item]);
|
|
}
|
|
object value = null;
|
|
dict.TryGetValue(item, out value);
|
|
// 布尔值大写造成的签名错误
|
|
if (value.ToString().ToLower().Equals("false")) value = "false";
|
|
if (value.ToString().ToLower().Equals("true")) value = "true";
|
|
signString += item + value.ToString();
|
|
result.Add(item, value.ToString());
|
|
}
|
|
signString = clientSecret + signString + clientSecret;
|
|
//Console.WriteLine("拼接内容:" + signString);
|
|
// MD5加密
|
|
using (var md5 = MD5.Create())
|
|
{
|
|
signString = GetMd5Hash(md5, signString).ToUpper();
|
|
}
|
|
//Console.WriteLine("签名:" + signString);
|
|
result["sign"] = signString;
|
|
return result;
|
|
}
|
|
|
|
static string GetMd5Hash(MD5 md5Hash, string input)
|
|
{
|
|
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
|
|
StringBuilder sBuilder = new StringBuilder();
|
|
for (int i = 0; i < data.Length; i++)
|
|
{
|
|
sBuilder.Append(data[i].ToString("x2"));
|
|
}
|
|
return sBuilder.ToString();
|
|
}
|
|
|
|
}
|
|
}
|