ASP.NET AES、DES加密

原:http://xylw.iteye.com/blog/1121714

首先,引入

using System.Security.Cryptography;

  1 /// <summary>   
  2     /// AES、DES加解密工具类   
  3     /// </summary>   
  4     public class EncryptUtil   
  5     {   
  6         private static readonly string AESKEY = "GHYU80DV3465QSFG";//16或者32位   
  7   
  8         //private static readonly string DESKEY = ConfigurationManager.AppSettings["DESKEY"].Trim();   
  9         private static readonly string DESKEY = "PORG56JK";//8位   
 10   
 11         /// <summary>   
 12         /// AES加密   
 13         /// </summary>   
 14         /// <param name="data">要加密的字符串</param>   
 15         /// <returns>加密后的字符串</returns>   
 16         public static string EncryptAES(string data)   
 17         {   
 18             try  
 19             {   
 20                 RijndaelManaged aes = new RijndaelManaged();   
 21                 byte[] bData = UTF8Encoding.UTF8.GetBytes(data);   
 22                 aes.Key = UTF8Encoding.UTF8.GetBytes(AESKEY);   
 23                 aes.IV = UTF8Encoding.UTF8.GetBytes(AESKEY);   
 24                 aes.Mode = CipherMode.CBC;   
 25                 aes.Padding = PaddingMode.PKCS7;   
 26   
 27                 ICryptoTransform iCryptoTransform = aes.CreateEncryptor();   
 28                 byte[] bResult = iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length);   
 29   
 30                 // return Convert.ToBase64String(bResult); //返回base64加密;   
 31                 return ByteToHex(bResult);                //返回十六进制数据;   
 32             }   
 33             catch  
 34             {   
 35                 throw;   
 36             }   
 37         }   
 38   
 39         /// <summary>   
 40         /// AES加密   
 41         /// </summary>   
 42         /// <param name="data">要加密的字符串</param>   
 43         /// <param name="sKey">密钥串(16位或32位)</param>   
 44         /// <returns>加密后的字符串</returns>   
 45         public static string EncryptAES(string data, string sKey)   
 46         {   
 47             try  
 48             {   
 49                 RijndaelManaged aes = new RijndaelManaged();   
 50                 byte[] bData = UTF8Encoding.UTF8.GetBytes(data);   
 51                 aes.Key = UTF8Encoding.UTF8.GetBytes(sKey);   
 52                 aes.IV = UTF8Encoding.UTF8.GetBytes(sKey);   
 53                 aes.Mode = CipherMode.CBC;   
 54                 aes.Padding = PaddingMode.PKCS7;   
 55   
 56                 ICryptoTransform iCryptoTransform = aes.CreateEncryptor();   
 57                 byte[] bResult = iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length);   
 58   
 59                 // return Convert.ToBase64String(bResult); //返回base64加密;   
 60                 return ByteToHex(bResult);                //返回十六进制数据;   
 61             }   
 62             catch  
 63             {   
 64                 throw;   
 65             }   
 66         }   
 67   
 68         /// <summary>   
 69         /// AES解密   
 70         /// </summary>   
 71         /// <param name="data">要解密的字符串</param>   
 72         /// <returns>解密后的字符串</returns>   
 73         public static string DecryptAES(string data)   
 74         {   
 75             try  
 76             {   
 77                 RijndaelManaged aes = new RijndaelManaged();   
 78                 //byte[] bData = Convert.FromBase64String(data); //解密base64;   
 79                 byte[] bData = HexToByte(data);                  //16进制to byte[];   
 80                 aes.Key = UTF8Encoding.UTF8.GetBytes(AESKEY);   
 81                 aes.IV = UTF8Encoding.UTF8.GetBytes(AESKEY);   
 82                 aes.Mode = CipherMode.CBC;   
 83                 aes.Padding = PaddingMode.PKCS7;   
 84   
 85                 ICryptoTransform iCryptoTransform = aes.CreateDecryptor();   
 86                 byte[] bResult = iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length);   
 87                 return Encoding.UTF8.GetString(bResult);   
 88             }   
 89             catch  
 90             {   
 91                 throw;   
 92             }   
 93         }   
 94   
 95         /// <summary>   
 96         /// AES解密   
 97         /// </summary>   
 98         /// <param name="data">要解密的字符串</param>   
 99         /// <param name="sKey">密钥串(16位或32位字符串)</param>   
100         /// <returns>解密后的字符串</returns>   
101         public static string DecryptAES(string data, string sKey)   
102         {   
103             try  
104             {   
105                 RijndaelManaged aes = new RijndaelManaged();   
106                 //byte[] bData = Convert.FromBase64String(data); //解密base64;   
107                 byte[] bData = HexToByte(data);                  //16进制to byte[];   
108                 aes.Key = UTF8Encoding.UTF8.GetBytes(sKey);   
109                 aes.IV = UTF8Encoding.UTF8.GetBytes(sKey);   
110                 aes.Mode = CipherMode.CBC;   
111                 aes.Padding = PaddingMode.PKCS7;   
112   
113                 ICryptoTransform iCryptoTransform = aes.CreateDecryptor();   
114                 byte[] bResult = iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length);   
115                 return Encoding.UTF8.GetString(bResult);   
116             }   
117             catch  
118             {   
119                 throw;   
120             }   
121         }   
122   
123         /// <summary>   
124         /// DES加密   
125         /// </summary>   
126         /// <param name="data">要加密的字符串</param>   
127         /// <returns>加密后的字符串</returns>   
128         public static string EncryptDES(string data)   
129         {   
130             DES des = new DESCryptoServiceProvider();   
131             des.Mode = CipherMode.ECB;   
132             des.Key = Encoding.UTF8.GetBytes(DESKEY);   
133             des.IV = Encoding.UTF8.GetBytes(DESKEY);   
134   
135             byte[] bytes = Encoding.UTF8.GetBytes(data);   
136             byte[] resultBytes = des.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length);   
137   
138             //return Convert.ToBase64String(resultBytes); //密文以base64返回;(可根据实际需要返回16进制数据;)   
139             return ByteToHex(resultBytes);//十六位   
140         }   
141   
142         /// <summary>   
143         /// DES加密   
144         /// </summary>   
145         /// <param name="data">要加密的字符串</param>   
146         /// <param name="key">密钥串(8位字符串)</param>   
147         /// <returns>加密后的字符串</returns>   
148         public static string EncryptDES(string data, string key)   
149         {   
150             DES des = new DESCryptoServiceProvider();   
151             des.Mode = CipherMode.ECB;   
152             des.Key = Encoding.UTF8.GetBytes(key);   
153             des.IV = Encoding.UTF8.GetBytes(key);   
154   
155             byte[] bytes = Encoding.UTF8.GetBytes(data);   
156             byte[] resultBytes = des.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length);   
157   
158             //return Convert.ToBase64String(resultBytes); //密文以base64返回;(可根据实际需要返回16进制数据;)   
159             return ByteToHex(resultBytes);//十六位   
160         }   
161   
162         /// <summary>   
163         /// DES解密   
164         /// </summary>   
165         /// <param name="data">要解密的字符串</param>   
166         /// <param name="key">密钥串(8位字符串)</param>   
167         /// <returns>解密后的字符串</returns>   
168         public static string DecryptDES(string data)   
169         {   
170             DES des = new DESCryptoServiceProvider();   
171             des.Mode = CipherMode.ECB;   
172             des.Key = Encoding.UTF8.GetBytes(DESKEY);   
173             des.IV = Encoding.UTF8.GetBytes(DESKEY);   
174   
175             //byte[] bytes = Convert.FromBase64String(data);   
176             byte[] bytes = HexToByte(data);   
177             byte[] resultBytes = des.CreateDecryptor().TransformFinalBlock(bytes, 0, bytes.Length);   
178   
179             return Encoding.UTF8.GetString(resultBytes);   
180         }   
181   
182         /// <summary>   
183         /// DES解密   
184         /// </summary>   
185         /// <param name="data">要解密的字符串</param>   
186         /// <param name="key">密钥串(8位字符串)</param>   
187         /// <returns>解密后的字符串</returns>   
188         public static string DecryptDES(string data, string key)   
189         {   
190             DES des = new DESCryptoServiceProvider();   
191             des.Mode = CipherMode.ECB;   
192             des.Key = Encoding.UTF8.GetBytes(key);   
193             des.IV = Encoding.UTF8.GetBytes(key);   
194   
195             //byte[] bytes = Convert.FromBase64String(data);   
196             byte[] bytes = HexToByte(data);   
197             byte[] resultBytes = des.CreateDecryptor().TransformFinalBlock(bytes, 0, bytes.Length);   
198   
199             return Encoding.UTF8.GetString(resultBytes);   
200         }   
201   
202         // method to convert hex string into a byte array     
203         private static byte[] HexToByte(string data)   
204         {   
205             data = data.Replace(" ", "");   
206   
207             byte[] comBuffer = new byte[data.Length / 2];   
208   
209             for (int i = 0; i < data.Length; i += 2)   
210                 comBuffer[i / 2] = (byte)Convert.ToByte(data.Substring(i, 2), 16);   
211   
212             return comBuffer;   
213         }   
214   
215         // method to convert a byte array into a hex string     
216         private static string ByteToHex(byte[] comByte)   
217         {   
218             StringBuilder builder = new StringBuilder(comByte.Length * 3);   
219   
220             foreach (byte data in comByte)   
221                 builder.Append(Convert.ToString(data, 16).PadLeft(2, '0').PadRight(3, ' '));   
222   
223             return builder.ToString().ToUpper().Replace(" ", "");   
224         }   
225     }  

你可能感兴趣的:(asp.net)