using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace TEAMModelOS.SDK { public class AESHelper { public static string Decryptor(string TextToDecrypt,string strKey,string strIV) { byte[] EncryptedBytes = Convert.FromBase64String(TextToDecrypt); //Setup the AES provider for decrypting. Aes aesProvider = Aes.Create(); //aesProvider.Key = System.Text.Encoding.ASCII.GetBytes(strKey); //aesProvider.IV = System.Text.Encoding.ASCII.GetBytes(strIV); aesProvider.BlockSize = 128; aesProvider.KeySize = 256; //My key and iv that i have used in openssl aesProvider.Key = System.Text.Encoding.UTF8.GetBytes(strKey); aesProvider.IV = System.Text.Encoding.UTF8.GetBytes(strIV); aesProvider.Padding = PaddingMode.PKCS7; aesProvider.Mode = CipherMode.CBC; ICryptoTransform cryptoTransform = aesProvider.CreateDecryptor(aesProvider.Key, aesProvider.IV); byte[] DecryptedBytes = cryptoTransform.TransformFinalBlock(EncryptedBytes, 0, EncryptedBytes.Length); return System.Text.Encoding.UTF8.GetString(DecryptedBytes); } public static string Encryptor(string TextToEncrypt, string strKey, string strIV) { //Turn the plaintext into a byte array. byte[] PlainTextBytes = System.Text.ASCIIEncoding.UTF8.GetBytes(TextToEncrypt); //Setup the AES providor for our purposes. Aes aesProvider = Aes.Create(); aesProvider.BlockSize = 128; aesProvider.KeySize = 256; //My key and iv that i have used in openssl aesProvider.Key = System.Text.Encoding.UTF8.GetBytes(strKey); aesProvider.IV = System.Text.Encoding.UTF8.GetBytes(strIV); aesProvider.Padding = PaddingMode.PKCS7; aesProvider.Mode = CipherMode.CBC; ICryptoTransform cryptoTransform = aesProvider.CreateEncryptor(aesProvider.Key, aesProvider.IV); byte[] EncryptedBytes = cryptoTransform.TransformFinalBlock(PlainTextBytes, 0, PlainTextBytes.Length); return Convert.ToBase64String(EncryptedBytes); } /// /// 有密码的加密 /// /// 加密字符 /// 加密的密码 /// public static string AESEncrypt(string toEncrypt, string key) { byte[] keyArray = hexStringTobyte(key); byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt); RijndaelManaged rDel = new RijndaelManaged(); rDel.KeySize = 128; rDel.BlockSize = 128; rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateEncryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return byteToHexString(resultArray); } /// /// 解密 /// /// 解密字符 /// 加密的密码 /// public static string AESDecrypt(string toDecrypt, string key) { if (string.IsNullOrWhiteSpace(toDecrypt)) { return ""; } else { byte[] keyArray = hexStringTobyte(key); byte[] dest = hexStringTobyte(toDecrypt); RijndaelManaged rDel = new(); rDel.KeySize = 128; rDel.BlockSize = 128; rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateDecryptor(); try { byte[] resultArray = cTransform.TransformFinalBlock(dest, 0, dest.Length); return UTF8Encoding.UTF8.GetString(resultArray); } catch (Exception ex) { return toDecrypt; } } } private static byte[] hexStringTobyte(String s) { if (string.IsNullOrEmpty(s)) return null; s = s.ToLower(); int byteArrayLength = s.Length / 2; byte[] b = new byte[byteArrayLength]; for (int i = 0; i < byteArrayLength; i++) { byte b0 = Convert.ToByte(s.Substring(i * 2, 2), 16); b[i] = b0; } return b; } public static string byteToHexString(byte[] t) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < t.Length; i++) { sb.Append(t[i].ToString("x").PadLeft(2, '0')); } return sb.ToString().ToUpper(); } } }