AES加密和解密

  1 using System;
  2 using System.Security.Cryptography;
  3 using System.Text;
  4 using System.IO;
  5 
  6 namespace AES
  7 {
  8     public class AESEncryption
  9     {
 10         private static string strKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
 11         /// <summary>
 12         /// AES加密算法
 13         /// </summary>
 14         /// <param name="plainText">明文字符串</param>
 15         /// <param name="strKey">密钥</param>
 16         /// <returns>返回加密后的密文字节数组</returns>
 17         public static string AESEncrypt(string plainText)
 18         {
 19             try
 20             {
 21                 //分组加密算法
 22                 RijndaelManaged aes = new RijndaelManaged();
 23                 byte[] inputByteArray = Encoding.UTF8.GetBytes(plainText);//得到需要加密的字节数组
 24 
 25                 //设置密钥及密钥向量
 26                 aes.Key = Encoding.UTF8.GetBytes(GetMD5(strKey));
 27                 aes.IV = Encoding.UTF8.GetBytes(GetMD5(strKey).Substring(8, 16));
 28                 aes.Mode = CipherMode.CBC;
 29                 aes.Padding = PaddingMode.PKCS7;
 30 
 31                 ICryptoTransform transform = aes.CreateEncryptor();
 32                 byte[] buffer= transform.TransformFinalBlock(inputByteArray, 0, inputByteArray.Length);
 33 
 34                 //System.IO.MemoryStream ms = new System.IO.MemoryStream();
 35                 
 36                 //CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write);
 37                 
 38                 //cs.Write(inputByteArray, 0, inputByteArray.Length);
 39                 //cs.FlushFinalBlock();
 40 
 41                 StringBuilder ret = new StringBuilder();
 42                 foreach (byte b in buffer)
 43                 {
 44                     ret.AppendFormat("{0:x2}", b);
 45                 }
 46                 return ret.ToString(); 
 47             }
 48             catch
 49             {
 50                 return plainText;
 51             }
 52         }
 53 
 54         /// <summary>
 55         /// AES解密
 56         /// </summary>
 57         /// <param name="cipherText">密文字节数组</param>
 58         /// <param name="strKey">密钥</param>
 59         /// <returns>返回解密后的字符串</returns>
 60         public static string AESDecrypt(string cipherText)
 61         {
 62             try
 63             {
 64                 RijndaelManaged aes = new RijndaelManaged();
 65 
 66                 int len;
 67                 len = cipherText.Length / 2;
 68                 byte[] inputByteArray = new byte[len];
 69                 int x, i;
 70                 for (x = 0; x < len; x++)
 71                 {
 72                     i = Convert.ToInt32(cipherText.Substring(x * 2, 2), 16);
 73                     inputByteArray[x] = (byte)i;
 74                 }
 75 
 76                 //byte[] inputByteArray = Encoding.UTF8.GetBytes(cipherText);
 77                 //设置密钥及密钥向量
 78                 aes.Key = Encoding.UTF8.GetBytes(GetMD5(strKey));
 79                 aes.IV = Encoding.UTF8.GetBytes(GetMD5(strKey).Substring(8, 16));
 80                 aes.Mode = CipherMode.CBC;
 81                 aes.Padding = PaddingMode.PKCS7;
 82 
 83                 ICryptoTransform transform = aes.CreateDecryptor();
 84                 byte[] buffer = transform.TransformFinalBlock(inputByteArray, 0, inputByteArray.Length);
 85 
 86                 //System.IO.MemoryStream ms = new System.IO.MemoryStream();
 87                 //CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write);
 88                 //cs.Write(inputByteArray, 0, inputByteArray.Length);
 89                 //cs.FlushFinalBlock();
 90                 string strDecrypt = Encoding.UTF8.GetString(buffer);
 91                 return strDecrypt; 
 92 
 93             }
 94             catch
 95             {
 96                 return cipherText;
 97             }
 98         }
 99 
100         public static string GetMD5(string str)
101         {
102             StringBuilder sb = new StringBuilder();
103             foreach (byte b in System.Security.Cryptography.MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(str)))
104             {
105                 sb.Append(b.ToString("X2"));
106             }
107             return sb.ToString();
108         }
109 
110 
111     }
112 }

 

posted @ 2013-11-21 15:22  comeonfyz  阅读(564)  评论(0编辑  收藏  举报