120 lines
4.4 KiB
C#
120 lines
4.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
|
|
namespace SA.Core.Util
|
|
{
|
|
public static class SecurityHelper
|
|
{
|
|
/// <summary>
|
|
/// 加密
|
|
/// </summary>
|
|
/// <param name="ciphertext"></param>
|
|
/// <param name="accessKey"></param>
|
|
/// <returns></returns>
|
|
public static string EncyptData(string ciphertext, string accessKey)
|
|
{
|
|
SymmetricAlgorithm des = DES.Create();
|
|
|
|
Encoding utf = new UTF8Encoding();
|
|
byte[] key = utf.GetBytes(accessKey);
|
|
byte[] iv = { 0x75, 0x70, 0x63, 0x68, 0x69, 0x6e, 0x61, 0x31 };
|
|
ICryptoTransform encryptor = des.CreateEncryptor(key, iv);
|
|
byte[] data = utf.GetBytes(ciphertext);
|
|
byte[] encData = encryptor.TransformFinalBlock(data, 0, data.Length);
|
|
return Convert.ToBase64String(encData);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 解密
|
|
/// </summary>
|
|
/// <param name="cryptograph"></param>
|
|
/// <param name="accessKey"></param>
|
|
/// <returns></returns>
|
|
public static string DecyptData(string? cryptograph, string? accessKey)
|
|
{
|
|
if (string.IsNullOrEmpty(cryptograph) || string.IsNullOrEmpty(accessKey))
|
|
{
|
|
return "";
|
|
}
|
|
|
|
SymmetricAlgorithm des = DES.Create();
|
|
|
|
Encoding utf = new UTF8Encoding();
|
|
byte[] key = utf.GetBytes(accessKey);
|
|
byte[] iv = { 0x75, 0x70, 0x63, 0x68, 0x69, 0x6e, 0x61, 0x31 };
|
|
ICryptoTransform decryptor = des.CreateDecryptor(key, iv);
|
|
byte[] encData = Convert.FromBase64String(cryptograph);
|
|
byte[] data = decryptor.TransformFinalBlock(encData, 0, encData.Length);
|
|
return utf.GetString(data);
|
|
}
|
|
|
|
public static string SignData(string ciphertext, string accessKey)
|
|
{
|
|
Encoding utf = new UTF8Encoding();
|
|
HMACMD5 hmac = new(utf.GetBytes(accessKey));
|
|
byte[] hashValue = hmac.ComputeHash(utf.GetBytes(ciphertext));
|
|
return Convert.ToBase64String(hashValue);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加密
|
|
/// </summary>
|
|
/// <param name="ciphertext"></param>
|
|
/// <param name="accessKey"></param>
|
|
/// <param name="iv"></param>
|
|
/// <returns></returns>
|
|
public static string EncyptDataNew(string ciphertext, string accessKey, string iv)
|
|
{
|
|
SymmetricAlgorithm des = DES.Create();
|
|
|
|
Encoding utf = new UTF8Encoding();
|
|
byte[] key = utf.GetBytes(accessKey);
|
|
byte[] ivbt = utf.GetBytes(iv);
|
|
ICryptoTransform encryptor = des.CreateEncryptor(key, ivbt);
|
|
byte[] data = utf.GetBytes(ciphertext);
|
|
byte[] encData = encryptor.TransformFinalBlock(data, 0, data.Length);
|
|
return Convert.ToBase64String(encData);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 解密
|
|
/// </summary>
|
|
/// <param name="cryptograph"></param>
|
|
/// <param name="accessKey"></param>
|
|
/// <param name="iv"></param>
|
|
/// <returns></returns>
|
|
public static string DecyptDataNew(string cryptograph, string accessKey, string iv)
|
|
{
|
|
SymmetricAlgorithm des = DES.Create();
|
|
|
|
Encoding utf = new UTF8Encoding();
|
|
byte[] key = utf.GetBytes(accessKey);
|
|
byte[] ivbt = utf.GetBytes(iv);
|
|
ICryptoTransform decryptor = des.CreateDecryptor(key, ivbt);
|
|
byte[] encData = Convert.FromBase64String(cryptograph);
|
|
byte[] data = decryptor.TransformFinalBlock(encData, 0, encData.Length);
|
|
return utf.GetString(data);
|
|
}
|
|
|
|
public static string CreateSignEncodingStr(string json, string clientid, string accessKey)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(clientid))
|
|
{
|
|
clientid = "UPWEBSITE";
|
|
}
|
|
string key = "content={0}&clientid=" + clientid + "&sign={1}";
|
|
string jiami = EncyptData(json, accessKey);
|
|
string jiami1 = HttpUtility.UrlEncode(jiami, Encoding.UTF8);
|
|
string jiasuo = SignData(jiami, accessKey);
|
|
string jiasuo1 = HttpUtility.UrlEncode(jiasuo, Encoding.UTF8);
|
|
key = string.Format(key, jiami1, jiasuo1);
|
|
return key;
|
|
}
|
|
}
|
|
}
|