using Org.BouncyCastle.Utilities.Encoders; namespace Admin.NET.Core; public class CryptogramUtil { public static readonly string CryptoType = App.GetConfig("Cryptogram:CryptoType"); // 加密类型 public static readonly string PublicKey = App.GetConfig("Cryptogram:PublicKey"); // 公钥 public static readonly string PrivateKey = App.GetConfig("Cryptogram:PrivateKey"); // 私钥 /// /// 加密 /// /// /// public static string Encrypt(string plainText) { if (CryptoType == CryptogramEnum.MD5.ToString()) { return MD5Encryption.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) { 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); } }