using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Common.Helpers { /// /// AES加解密工具类 /// public class AESEncrypt { static string KeysString = "^%*7&%1)1~1`(UNIC):&*d|`> /// AES加密 /// /// 要加密的内容 /// 密钥 /// 16个字节长(128位) /// 32个字节长(256位) /// 注:一个汉字,2个字节 /// 为空时,系统默认256位进行加/解密 /// /// Base64转码后的密文 public static string Encrypt(string toEncrypt, string strKey) { if (strKey.Length != 16 && strKey.Length != 32) strKey = KeysString; byte[] keyArray = UTF8Encoding.UTF8.GetBytes(strKey); byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt); string tmp = string.Empty; RijndaelManaged rDel = new RijndaelManaged();//using System.Security.Cryptography; try { rDel.Key = keyArray; rDel.Mode = CipherMode.ECB;//using System.Security.Cryptography; rDel.Padding = PaddingMode.PKCS7;//using System.Security.Cryptography; ICryptoTransform cTransform = rDel.CreateEncryptor();//using System.Security.Cryptography; byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); tmp = Convert.ToBase64String(resultArray, 0, resultArray.Length); } catch (Exception ex) { tmp = ex.Message; } return tmp; } /// /// AES解密 /// /// 要解密的内容 /// 密钥 /// 16个字节长(128位) /// 32个字节长(256位) /// 注:一个汉字,2个字节 /// 为空时,系统默认256位进行加/解密 /// /// 解密后的明文 public static string Decrypt(string toDecrypt, string strKey) { if (strKey.Length != 16 && strKey.Length != 32) strKey = KeysString; byte[] keyArray = UTF8Encoding.UTF8.GetBytes(strKey); byte[] toEncryptArray = Convert.FromBase64String(toDecrypt); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return UTF8Encoding.UTF8.GetString(resultArray); } static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV) { // Check arguments. if (plainText == null || plainText.Length <= 0) throw new ArgumentNullException("plainText"); if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key"); if (IV == null || IV.Length <= 0) throw new ArgumentNullException("Key"); byte[] encrypted; // Create an RijndaelManaged object // with the specified key and IV. using (RijndaelManaged rijAlg = new RijndaelManaged()) { rijAlg.Key = Key; rijAlg.IV = IV; // Create a decrytor to perform the stream transform. ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV); // Create the streams used for encryption. using (MemoryStream msEncrypt = new MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) { //Write all data to the stream. swEncrypt.Write(plainText); } encrypted = msEncrypt.ToArray(); } } } // Return the encrypted bytes from the memory stream. return encrypted; } static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV) { // Check arguments. if (cipherText == null || cipherText.Length <= 0) throw new ArgumentNullException("cipherText"); if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key"); if (IV == null || IV.Length <= 0) throw new ArgumentNullException("Key"); // Declare the string used to hold // the decrypted text. string plaintext = null; // Create an RijndaelManaged object // with the specified key and IV. using (RijndaelManaged rijAlg = new RijndaelManaged()) { rijAlg.Key = Key; rijAlg.IV = IV; // Create a decrytor to perform the stream transform. ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV); // Create the streams used for decryption. using (MemoryStream msDecrypt = new MemoryStream(cipherText)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (StreamReader srDecrypt = new StreamReader(csDecrypt)) { // Read the decrypted bytes from the decrypting stream // and place them in a string. plaintext = srDecrypt.ReadToEnd(); } } } } return plaintext; } } }