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";
///
/// Md5加密
///
///
///
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;
}
///
/// 解密
///
///
///
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;
}
///
/// SM2加密
///
///
///
public static string SM2Encrypt(string plainText)
{
return GMUtil.SM2Encrypt(PublicKey, plainText);
}
///
/// SM2解密
///
///
///
public static string SM2Decrypt(string cipherText)
{
return GMUtil.SM2Decrypt(PrivateKey, cipherText);
}
///
/// SM4加密(ECB)
///
///
///
public static string SM4EncryptECB(string plainText)
{
return GMUtil.SM4EncryptECB(SM4_key, plainText);
}
///
/// SM4解密(ECB)
///
///
///
public static string SM4DecryptECB(string cipherText)
{
return GMUtil.SM4DecryptECB(SM4_key, cipherText);
}
///
/// SM4加密(CBC)
///
///
///
public static string SM4EncryptCBC(string plainText)
{
return GMUtil.SM4EncryptCBC(SM4_key, SM4_iv, plainText);
}
///
/// SM4解密(CBC)
///
///
///
public static string SM4DecryptCBC(string cipherText)
{
return GMUtil.SM4DecryptCBC(SM4_key, SM4_iv, cipherText);
}
///
/// MD5 比较
///
/// 加密文本
/// MD5 字符串
/// 是否输出大写加密,默认 false
/// 是否输出 16 位
/// bool
public static bool Compare(string text, string hash, bool uppercase = false, bool is16 = false)
{
return Compare(Encoding.UTF8.GetBytes(text), hash, uppercase, is16);
}
///
/// MD5 加密
///
/// 加密文本
/// 是否输出大写加密,默认 false
/// 是否输出 16 位
///
public static string Encrypt(string text, bool uppercase = false, bool is16 = false)
{
return Encrypt(Encoding.UTF8.GetBytes(text), uppercase, is16);
}
///
/// MD5 加密
///
/// 字节数组
/// 是否输出大写加密,默认 false
/// 是否输出 16 位
///
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();
}
///
/// MD5 比较
///
/// 字节数组
/// MD5 字符串
/// 是否输出大写加密,默认 false
/// 是否输出 16 位
/// bool
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);
}
}
}