码迷,mamicode.com
首页 > 其他好文 > 详细

C# 加密解密

时间:2014-09-12 14:45:43      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   os   ar   for   文件   

  1 using System;
  2 using System.IO;
  3 using System.Security.Cryptography;
  4 using System.Text;
  5 
  6 namespace Public
  7 {
  8     /// <summary>
  9     /// 加密解密
 10     /// </summary>
 11     public class EncryptUtils
 12     {
 13         #region Sha256加密
 14         /// <summary>
 15         /// Sha256加密算法
 16         /// </summary>
 17         /// <param name="plainText"></param>
 18         /// <returns></returns>
 19         public static string Sha256(string plainText)
 20         {
 21             SHA256Managed _sha256 = new SHA256Managed();
 22             byte[] _cipherText = _sha256.ComputeHash(Encoding.Default.GetBytes(plainText));
 23             return Convert.ToBase64String(_cipherText);
 24         }
 25         #endregion
 26 
 27         #region Base64加密解密
 28         /// <summary>
 29         /// Base64加密
 30         /// </summary>
 31         /// <param name="input">需要加密的字符串</param>
 32         /// <returns></returns>
 33         public static string Base64Encrypt(string input)
 34         {
 35             return Base64Encrypt(input, new UTF8Encoding());
 36         }
 37 
 38         /// <summary>
 39         /// Base64加密
 40         /// </summary>
 41         /// <param name="input">需要加密的字符串</param>
 42         /// <param name="encode">字符编码</param>
 43         /// <returns></returns>
 44         public static string Base64Encrypt(string input, Encoding encode)
 45         {
 46             return Convert.ToBase64String(encode.GetBytes(input));
 47         }
 48 
 49         /// <summary>
 50         /// Base64解密
 51         /// </summary>
 52         /// <param name="input">需要解密的字符串</param>
 53         /// <returns></returns>
 54         public static string Base64Decrypt(string input)
 55         {
 56             return Base64Decrypt(input, new UTF8Encoding());
 57         }
 58 
 59         /// <summary>
 60         /// Base64解密
 61         /// </summary>
 62         /// <param name="input">需要解密的字符串</param>
 63         /// <param name="encode">字符的编码</param>
 64         /// <returns></returns>
 65         public static string Base64Decrypt(string input, Encoding encode)
 66         {
 67             return encode.GetString(Convert.FromBase64String(input));
 68         }
 69         #endregion
 70 
 71         #region DES加密解密
 72         /// <summary>
 73         /// DES加密
 74         /// </summary>
 75         /// <param name="data">加密数据</param>
 76         /// <returns></returns>
 77         public static string DESEncrypt(string data)
 78         {
 79             return DESEncrypt(data, "znstkxun", "xxuntian");
 80         }
 81         /// <summary>
 82         /// DES加密
 83         /// </summary>
 84         /// <param name="data">加密数据</param>
 85         /// <param name="key">8位字符的密钥字符串</param>
 86         /// <param name="iv">8位字符的初始化向量字符串</param>
 87         /// <returns></returns>
 88         public static string DESEncrypt(string data, string key, string iv)
 89         {
 90             byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(key);
 91             byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv);
 92 
 93             DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
 94             int i = cryptoProvider.KeySize;
 95             MemoryStream ms = new MemoryStream();
 96             CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
 97 
 98             StreamWriter sw = new StreamWriter(cst);
 99             sw.Write(data);
100             sw.Flush();
101             cst.FlushFinalBlock();
102             sw.Flush();
103             return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
104         }
105         /// <summary>
106         /// DES解密
107         /// </summary>
108         /// <param name="data">加密数据</param>
109         /// <returns></returns>
110         public static string DESDecrypt(string data)
111         {
112             return DESDecrypt(data, "znstkxun", "xxuntian");
113         }
114         /// <summary>
115         /// DES解密
116         /// </summary>
117         /// <param name="data">解密数据</param>
118         /// <param name="key">8位字符的密钥字符串(需要和加密时相同)</param>
119         /// <param name="iv">8位字符的初始化向量字符串(需要和加密时相同)</param>
120         /// <returns></returns>
121         public static string DESDecrypt(string data, string key, string iv)
122         {
123             byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(key);
124             byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv);
125 
126             byte[] byEnc;
127             try
128             {
129                 byEnc = Convert.FromBase64String(data);
130             }
131             catch (Exception ex)
132             {
133                 return "数据解密失败!\n 失败原因:" + ex.Message;
134             }
135 
136             DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
137             MemoryStream ms = new MemoryStream(byEnc);
138             CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
139             StreamReader sr = new StreamReader(cst);
140             return sr.ReadToEnd();
141         }
142         #endregion
143 
144         #region MD5加密
145         /// <summary>
146         /// MD5加密
147         /// </summary>
148         /// <param name="input">需要加密的字符串</param>
149         /// <returns></returns>
150         public static string MD5Encrypt(string input)
151         {
152             return MD5Encrypt(input, new UTF8Encoding());
153         }
154 
155         /// <summary>
156         /// MD5加密
157         /// </summary>
158         /// <param name="input">需要加密的字符串</param>
159         /// <param name="encode">字符的编码</param>
160         /// <returns></returns>
161         public static string MD5Encrypt(string input, Encoding encode)
162         {
163             MD5 md5 = new MD5CryptoServiceProvider();
164             byte[] t = md5.ComputeHash(encode.GetBytes(input));
165             StringBuilder sb = new StringBuilder(32);
166             for (int i = 0; i < t.Length; i++)
167                 sb.Append(t[i].ToString("x").PadLeft(2, 0));
168             return sb.ToString();
169         }
170 
171         /// <summary>
172         /// MD5对文件流加密
173         /// </summary>
174         /// <param name="sr"></param>
175         /// <returns></returns>
176         public static string MD5Encrypt(Stream stream)
177         {
178             MD5 md5serv = MD5CryptoServiceProvider.Create();
179             byte[] buffer = md5serv.ComputeHash(stream);
180             StringBuilder sb = new StringBuilder();
181             foreach (byte var in buffer)
182                 sb.Append(var.ToString("x2"));
183             return sb.ToString();
184         }
185 
186         /// <summary>
187         /// MD5加密(返回16位加密串)
188         /// </summary>
189         /// <param name="input"></param>
190         /// <param name="encode"></param>
191         /// <returns></returns>
192         public static string MD5Encrypt16(string input, Encoding encode)
193         {
194             MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
195             string result = BitConverter.ToString(md5.ComputeHash(encode.GetBytes(input)), 4, 8);
196             result = result.Replace("-", "");
197             return result;
198         }
199         #endregion
200 
201         #region 3DES 加密解密
202 
203         public static string DES3Encrypt(string data, string key)
204         {
205             TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
206 
207             DES.Key = ASCIIEncoding.ASCII.GetBytes(key);
208             DES.Mode = CipherMode.CBC;
209             DES.Padding = PaddingMode.PKCS7;
210 
211             ICryptoTransform DESEncrypt = DES.CreateEncryptor();
212 
213             byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(data);
214             return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
215         }
216 
217         public static string DES3Decrypt(string data, string key)
218         {
219             TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
220 
221             DES.Key = ASCIIEncoding.ASCII.GetBytes(key);
222             DES.Mode = CipherMode.CBC;
223             DES.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
224 
225             ICryptoTransform DESDecrypt = DES.CreateDecryptor();
226 
227             string result = "";
228             try
229             {
230                 byte[] Buffer = Convert.FromBase64String(data);
231                 result = ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
232             }
233             catch (Exception ex)
234             {
235                 result = "数据解密失败!\n 失败原因:" + ex.Message;
236             }
237             return result;
238         }
239 
240         #endregion
241     }
242 }

 

C# 加密解密

标签:des   style   blog   color   io   os   ar   for   文件   

原文地址:http://www.cnblogs.com/Xuntind/p/3968266.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!