using Org.BouncyCastle.Utilities.Encoders;
namespace Admin.NET.Core;
///
/// SM工具类
///
public class SMUtil
{
public static string PublicKey = App.GetConfig("Crypto:PublicKey"); // 公钥
public static string PrivateKey = App.GetConfig("Crypto:PrivateKey"); // 私钥
///
/// SM2加密
///
///
///
public static string SM2Encrypt(string plainText)
{
byte[] sourceData = Encoding.Default.GetBytes(plainText);
return SM2Util.Encrypt(Hex.Decode(PublicKey), sourceData);
}
///
/// SM2解密
///
///
///
public static string SM2Decrypt(string cipherText)
{
return Encoding.Default.GetString(SM2Util.Decrypt(Hex.Decode(PrivateKey), Hex.Decode(cipherText)));
}
///
/// SM4加密(ECB)
///
///
///
public static string SM4EncryptECB(string plainText)
{
var sm4 = new SM4Util();
return sm4.Encrypt_ECB(plainText);
}
///
/// SM4解密(ECB)
///
///
///
public static string SM4DecryptECB(string cipherText)
{
var sm4 = new SM4Util();
return sm4.Decrypt_ECB(cipherText);
}
///
/// SM4加密(CBC)
///
///
///
public static string SM4EncryptCBC(string plainText)
{
var sm4 = new SM4Util();
return sm4.Encrypt_CBC(plainText);
}
///
/// SM4解密(CBC)
///
///
///
public static string SM4DecryptCBC(string cipherText)
{
var sm4 = new SM4Util();
return sm4.Decrypt_CBC(cipherText);
}
public static string StrToHex(string str)
{
return Encoding.ASCII.GetString(Hex.Encode(Encoding.UTF8.GetBytes(str))).ToUpper();
}
}