1 public class AESHelper
2 {
3 const string AES_KEY = "43hr8fhu34b58123";
4
5 /// <summary>
6 /// AES加密
7 /// </summary>
8 /// <param name="Text">待加密字符串</param>
9 /// <returns>加密后字符串</returns>
10 public static string AESEncrypt(string Text)
11 {
12 try
13 {
14 string key = AES_KEY;
15 //分组加密算法
16 AesCryptoServiceProvider aes =new AesCryptoServiceProvider();
17 byte[] inputByteArray = Encoding.UTF8.GetBytes(Text);//得到需要加密的字节数组
18 //设置密钥及密钥向量
19 aes.Key = Encoding.UTF8.GetBytes(key);
20 //aes.IV = Encoding.UTF8.GetBytes(key);
21 aes.Mode = CipherMode.ECB;
22 aes.Padding = PaddingMode.PKCS7;
23 byte[] cipherBytes = null;
24 using (MemoryStream ms = new MemoryStream())
25 {
26 using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
27 {
28 cs.Write(inputByteArray, 0, inputByteArray.Length);
29 cs.FlushFinalBlock();
30 cipherBytes = ms.ToArray();//得到加密后的字节数组
31 cs.Close();
32 ms.Close();
33 }
34 }
35 return Convert.ToBase64String(cipherBytes);
36 }
37 catch { }
38 return Text;
39 }
40
41 /// <summary>
42 /// AES解密
43 /// </summary>
44 /// <param name="Text">待解密字符串</param>
45 /// <returns>解密后字符串</returns>
46 public static string AESDecrypt(string Text)
47 {
48 try
49 {
50 string key = AES_KEY;
51 byte[] cipherText = Convert.FromBase64String(Text);
52 AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
53 aes.Key = Encoding.UTF8.GetBytes(key);
54 //aes.IV = Encoding.UTF8.GetBytes(key);
55 aes.Mode = CipherMode.ECB;
56 aes.Padding = PaddingMode.PKCS7;
57 byte[] decryptBytes = new byte[cipherText.Length];
58 using (MemoryStream ms = new MemoryStream(cipherText))
59 {
60 using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read))
61 {
62 cs.Read(decryptBytes, 0, decryptBytes.Length);
63 cs.Close();
64 ms.Close();
65 }
66 }
67 return Encoding.UTF8.GetString(decryptBytes).Replace("