180 lines
6.1 KiB
C#
180 lines
6.1 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Security.Cryptography;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace YY.Admin.Core.Util
|
||
{
|
||
public class CryptogramUtil
|
||
{
|
||
public static readonly string CryptoType = "SM2"; // 加密类型
|
||
public static readonly string PublicKey = "0484C7466D950E120E5ECE5DD85D0C90EAA85081A3A2BD7C57AE6DC822EFCCBD66620C67B0103FC8DD280E36C3B282977B722AAEC3C56518EDCEBAFB72C5A05312"; // 公钥
|
||
public static readonly string PrivateKey = "8EDB615B1D48B8BE188FC0F18EC08A41DF50EA731FA28BF409E6552809E3A111"; // 私钥
|
||
|
||
public static readonly string SM4_key = "0123456789abcdeffedcba9876543210";
|
||
public static readonly string SM4_iv = "595298c7c6fd271f0402f804c33d3f66";
|
||
|
||
/// <summary>
|
||
/// Md5加密
|
||
/// </summary>
|
||
/// <param name="plainText"></param>
|
||
/// <returns></returns>
|
||
public static string Encrypt(string plainText)
|
||
{
|
||
if (CryptoType == CryptogramEnum.MD5.ToString())
|
||
{
|
||
return Encrypt(plainText);
|
||
}
|
||
else if (CryptoType == CryptogramEnum.SM2.ToString())
|
||
{
|
||
return SM2Encrypt(plainText);
|
||
}
|
||
else if (CryptoType == CryptogramEnum.SM4.ToString())
|
||
{
|
||
return SM4EncryptECB(plainText);
|
||
}
|
||
return plainText;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 解密
|
||
/// </summary>
|
||
/// <param name="cipherText"></param>
|
||
/// <returns></returns>
|
||
public static string Decrypt(string cipherText)
|
||
{
|
||
if (CryptoType == CryptogramEnum.SM2.ToString())
|
||
{
|
||
return SM2Decrypt(cipherText);
|
||
}
|
||
else if (CryptoType == CryptogramEnum.SM4.ToString())
|
||
{
|
||
return SM4DecryptECB(cipherText);
|
||
}
|
||
return cipherText;
|
||
}
|
||
|
||
/// <summary>
|
||
/// SM2加密
|
||
/// </summary>
|
||
/// <param name="plainText"></param>
|
||
/// <returns></returns>
|
||
public static string SM2Encrypt(string plainText)
|
||
{
|
||
return GMUtil.SM2Encrypt(PublicKey, plainText);
|
||
}
|
||
|
||
/// <summary>
|
||
/// SM2解密
|
||
/// </summary>
|
||
/// <param name="cipherText"></param>
|
||
/// <returns></returns>
|
||
public static string SM2Decrypt(string cipherText)
|
||
{
|
||
return GMUtil.SM2Decrypt(PrivateKey, cipherText);
|
||
}
|
||
|
||
/// <summary>
|
||
/// SM4加密(ECB)
|
||
/// </summary>
|
||
/// <param name="plainText"></param>
|
||
/// <returns></returns>
|
||
public static string SM4EncryptECB(string plainText)
|
||
{
|
||
return GMUtil.SM4EncryptECB(SM4_key, plainText);
|
||
}
|
||
|
||
/// <summary>
|
||
/// SM4解密(ECB)
|
||
/// </summary>
|
||
/// <param name="cipherText"></param>
|
||
/// <returns></returns>
|
||
public static string SM4DecryptECB(string cipherText)
|
||
{
|
||
return GMUtil.SM4DecryptECB(SM4_key, cipherText);
|
||
}
|
||
|
||
/// <summary>
|
||
/// SM4加密(CBC)
|
||
/// </summary>
|
||
/// <param name="plainText"></param>
|
||
/// <returns></returns>
|
||
public static string SM4EncryptCBC(string plainText)
|
||
{
|
||
return GMUtil.SM4EncryptCBC(SM4_key, SM4_iv, plainText);
|
||
}
|
||
|
||
/// <summary>
|
||
/// SM4解密(CBC)
|
||
/// </summary>
|
||
/// <param name="cipherText"></param>
|
||
/// <returns></returns>
|
||
public static string SM4DecryptCBC(string cipherText)
|
||
{
|
||
return GMUtil.SM4DecryptCBC(SM4_key, SM4_iv, cipherText);
|
||
}
|
||
/// <summary>
|
||
/// MD5 比较
|
||
/// </summary>
|
||
/// <param name="text">加密文本</param>
|
||
/// <param name="hash">MD5 字符串</param>
|
||
/// <param name="uppercase">是否输出大写加密,默认 false</param>
|
||
/// <param name="is16">是否输出 16 位</param>
|
||
/// <returns>bool</returns>
|
||
public static bool Compare(string text, string hash, bool uppercase = false, bool is16 = false)
|
||
{
|
||
return Compare(Encoding.UTF8.GetBytes(text), hash, uppercase, is16);
|
||
}
|
||
|
||
/// <summary>
|
||
/// MD5 加密
|
||
/// </summary>
|
||
/// <param name="text">加密文本</param>
|
||
/// <param name="uppercase">是否输出大写加密,默认 false</param>
|
||
/// <param name="is16">是否输出 16 位</param>
|
||
/// <returns></returns>
|
||
public static string Encrypt(string text, bool uppercase = false, bool is16 = false)
|
||
{
|
||
return Encrypt(Encoding.UTF8.GetBytes(text), uppercase, is16);
|
||
}
|
||
|
||
/// <summary>
|
||
/// MD5 加密
|
||
/// </summary>
|
||
/// <param name="bytes">字节数组</param>
|
||
/// <param name="uppercase">是否输出大写加密,默认 false</param>
|
||
/// <param name="is16">是否输出 16 位</param>
|
||
/// <returns></returns>
|
||
public static string Encrypt(byte[] bytes, bool uppercase = false, bool is16 = false)
|
||
{
|
||
var data = MD5.HashData(bytes);
|
||
|
||
var stringBuilder = new StringBuilder();
|
||
for (var i = 0; i < data.Length; i++)
|
||
{
|
||
stringBuilder.Append(data[i].ToString("x2"));
|
||
}
|
||
|
||
var md5String = stringBuilder.ToString();
|
||
var hash = !is16 ? md5String : md5String.Substring(8, 16);
|
||
return !uppercase ? hash : hash.ToUpper();
|
||
}
|
||
|
||
/// <summary>
|
||
/// MD5 比较
|
||
/// </summary>
|
||
/// <param name="bytes">字节数组</param>
|
||
/// <param name="hash">MD5 字符串</param>
|
||
/// <param name="uppercase">是否输出大写加密,默认 false</param>
|
||
/// <param name="is16">是否输出 16 位</param>
|
||
/// <returns>bool</returns>
|
||
public static bool Compare(byte[] bytes, string hash, bool uppercase = false, bool is16 = false)
|
||
{
|
||
var hashOfInput = Encrypt(bytes, uppercase, is16);
|
||
return hash.Equals(hashOfInput, StringComparison.OrdinalIgnoreCase);
|
||
}
|
||
}
|
||
}
|