using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace AESDecryptDemo { public class AES { public static string AESEncryptASCII(string plainStr) { StringBuilder sb = new StringBuilder(); byte[] bKey = Encoding.UTF8.GetBytes(GetKey()); byte[] bIV = Encoding.UTF8.GetBytes(GetIV()); byte[] byteArray = Encoding.UTF8.GetBytes(plainStr); string encrypt = null; Rijndael aes = Rijndael.Create(); using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write)) { cStream.Write(byteArray, 0, byteArray.Length); cStream.FlushFinalBlock(); encrypt = Encode(Convert.ToBase64String(mStream.ToArray())); } } aes.Clear(); return encrypt; } public static string AESEncrypt(string plainStr) { StringBuilder sb = new StringBuilder(); byte[] bKey = Encoding.UTF8.GetBytes(GetKey()); byte[] bIV = Encoding.UTF8.GetBytes(GetIV()); byte[] byteArray = Encoding.UTF8.GetBytes(plainStr); string encrypt = null; Rijndael aes = Rijndael.Create(); using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write)) { cStream.Write(byteArray, 0, byteArray.Length); cStream.FlushFinalBlock(); encrypt = Convert.ToBase64String(mStream.ToArray()); } } aes.Clear(); return encrypt; } public static string AESDecryptASCII(string encryptStr) { encryptStr = Decode(encryptStr); byte[] bKey = Encoding.UTF8.GetBytes(GetKey()); byte[] bIV = Encoding.UTF8.GetBytes(GetIV()); byte[] byteArray = Convert.FromBase64String(encryptStr); string decrypt = null; Rijndael aes = Rijndael.Create(); using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write)) { cStream.Write(byteArray, 0, byteArray.Length); cStream.FlushFinalBlock(); decrypt = Encoding.UTF8.GetString(mStream.ToArray()); } } aes.Clear(); return decrypt; } public static string AESDecrypt(string encryptStr) { byte[] bKey = Encoding.UTF8.GetBytes(GetKey()); byte[] bIV = Encoding.UTF8.GetBytes(GetIV()); byte[] byteArray = Convert.FromBase64String(encryptStr); string decrypt = null; Rijndael aes = Rijndael.Create(); using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write)) { cStream.Write(byteArray, 0, byteArray.Length); cStream.FlushFinalBlock(); decrypt = Encoding.UTF8.GetString(mStream.ToArray()); } } aes.Clear(); return decrypt; } public static string Encode(string strEncode) { string strReturn = "";// 存储转换后的编码 foreach (short shortx in strEncode.ToCharArray()) { strReturn += shortx.ToString("X"); } return strReturn; } public static string Decode(string strDecode) { string sResult = ""; for (int i = 0; i < strDecode.Length / 2; i++) { sResult += (char)short.Parse(strDecode.Substring(i * 2, 2), global::System.Globalization.NumberStyles.HexNumber); } return sResult; } private static string GetKey() { return @")O[NB]6,YF}+efcaj{+oESb9d8>Z'e9M"; } /// <summary> /// 获取向量 /// </summary> private static string GetIV() { return @"L+~f4,Ir)b$=pkf"; } } }