[C#] 常用工具类——加密解密类

  1 using System;
  2 using System.Configuration;
  3 using System.Collections.Generic;
  4 using System.Text;
  5 using System.Web;
  6 using System.Web.UI;
  7 using System.Web.UI.HtmlControls;
  8 using System.Web.UI.WebControls;
  9 using System.Web.UI.WebControls.WebParts;
 10 using System.Web.Security;
 11 using System.Drawing;
 12 using System.Drawing.Imaging;
 13 using System.Drawing.Drawing2D;
 14 using System.IO;
 15 using System.Security.Cryptography;
 16   
 17 namespace Utils
 18 {
 19     /// <summary>
 20     /// <para> </para>
 21     ///  常用工具类——加密解密类
 22     /// <para> -------------------------------------------------</para>
 23     /// <para> StringEncode:返回 HTML 字符串的编码结果</para>
 24     /// <para> StringDecode:返回 HTML 字符串的解码结果</para>
 25     /// <para> UrlEncode:返回 URL 字符串的编码结果</para>
 26     /// <para> UrlDecode:返回 URL 字符串的解码结果</para>
 27     /// <para> DESEncrypt:DES加密</para>
 28     /// <para> DESDecrypt:DES解密</para>
 29     /// <para> MD5:MD5函数</para>
 30     /// <para> SHA256:SHA256函数</para>
 31     /// </summary>
 32     public class EncyptHelper
 33     {
 34         /// <summary>
 35         /// 32位Key值:
 36         /// </summary>
 37         public static byte[] DESKey = new byte[] { 0x03, 0x0B, 0x13, 0x1B, 0x23, 0x2B, 0x33, 0x3B, 0x43, 0x4B, 0x9B, 0x93, 0x8B, 0x83, 0x7B, 0x73, 0x6B, 0x63, 0x5B, 0x53, 0xF3, 0xFB, 0xA3, 0xAB, 0xB3, 0xBB, 0xC3, 0xEB, 0xE3, 0xDB, 0xD3, 0xCB };
 38   
 39         #region 返回 HTML 字符串的编码结果
 40         /// <summary>
 41         /// 返回 HTML 字符串的编码结果
 42         /// </summary>
 43         /// <param name="str">字符串</param>
 44         /// <returns>编码结果</returns>
 45         public static string StringEncode(string str)
 46         {
 47             return HttpUtility.HtmlEncode(str);
 48         }
 49         #endregion
 50   
 51         #region 返回 HTML 字符串的解码结果
 52         /// <summary>
 53         /// 返回 HTML 字符串的解码结果
 54         /// </summary>
 55         /// <param name="str">字符串</param>
 56         /// <returns>解码结果</returns>
 57         public static string StringDecode(string str)
 58         {
 59             return HttpUtility.HtmlDecode(str);
 60         }
 61         #endregion
 62   
 63         #region 返回 URL 字符串的编码结果
 64         /// <summary>
 65         /// 返回 URL 字符串的编码结果
 66         /// </summary>
 67         /// <param name="str">字符串</param>
 68         /// <returns>编码结果</returns>
 69         public static string UrlEncode(string str)
 70         {
 71             return HttpUtility.UrlEncode(str);
 72         }
 73         #endregion
 74   
 75         #region 返回 URL 字符串的解码结果
 76         /// <summary>
 77         /// 返回 URL 字符串的解码结果
 78         /// </summary>
 79         /// <param name="str">字符串</param>
 80         /// <returns>解码结果</returns>
 81         public static string UrlDecode(string str)
 82         {
 83             return HttpUtility.UrlDecode(str);
 84         }
 85         #endregion
 86   
 87         #region DES加密
 88         /// <summary>
 89         /// DES加密
 90         /// </summary>
 91         /// <param name="strSource">待加密字串</param>
 92         /// <returns>加密后的字符串</returns>
 93         public static string DESEncrypt(string strSource)
 94         {
 95             return DESEncrypt(strSource, DESKey);
 96         }
 97         /// <summary>
 98         /// DES加密
 99         /// </summary>
100         /// <param name="strSource">待加密字串</param>
101         /// <param name="key">Key值</param>
102         /// <returns>加密后的字符串</returns>
103         public static string DESEncrypt(string strSource, byte[] key)
104         {
105             SymmetricAlgorithm sa = Rijndael.Create();
106             sa.Key = key;
107             sa.Mode = CipherMode.ECB;
108             sa.Padding = PaddingMode.Zeros;
109             MemoryStream ms = new MemoryStream();
110             CryptoStream cs = new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);
111             byte[] byt = Encoding.Unicode.GetBytes(strSource);
112             cs.Write(byt, 0, byt.Length);
113             cs.FlushFinalBlock();
114             cs.Close();
115             return Convert.ToBase64String(ms.ToArray());
116         }
117         #endregion
118   
119         #region DES解密
120         /// <summary>
121         /// DES解密
122         /// </summary>
123         /// <param name="strSource">待解密的字串</param>
124         /// <returns>解密后的字符串</returns>
125         public static string DESDecrypt(string strSource)
126         {
127             return DESDecrypt(strSource, DESKey);
128         }
129         /// <summary>
130         /// DES解密
131         /// </summary>
132         /// <param name="strSource">待解密的字串</param>
133         /// <param name="key">32位Key值</param>
134         /// <returns>解密后的字符串</returns>
135         public static string DESDecrypt(string strSource, byte[] key)
136         {
137             SymmetricAlgorithm sa = Rijndael.Create();
138             sa.Key = key;
139             sa.Mode = CipherMode.ECB;
140             sa.Padding = PaddingMode.Zeros;
141             ICryptoTransform ct = sa.CreateDecryptor();
142             byte[] byt = Convert.FromBase64String(strSource);
143             MemoryStream ms = new MemoryStream(byt);
144             CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Read);
145             StreamReader sr = new StreamReader(cs, Encoding.Unicode);
146             return sr.ReadToEnd();
147         }
148         #endregion        
149   
150         #region MD5函数
151         /// <summary>
152         /// MD5函数,需引用:using System.Security.Cryptography;
153         /// </summary>
154         /// <param name="str">原始字符串</param>
155         /// <returns>MD5结果</returns>
156         public static string MD5(string str)
157         {
158             byte[] b = Encoding.Default.GetBytes(str);
159             b = new MD5CryptoServiceProvider().ComputeHash(b);
160             string ret = "";
161             for (int i = 0; i < b.Length; i++)
162                 ret += b[i].ToString("x").PadLeft(2, '0');
163             return ret;
164         }
165         #endregion
166   
167         #region SHA256函数
168         /// <summary>
169         /// SHA256函数
170         /// </summary>
171         /// /// <param name="str">原始字符串</param>
172         /// <returns>SHA256结果</returns>
173         public static string SHA256(string str)
174         {
175             byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
176             SHA256Managed Sha256 = new SHA256Managed();
177             byte[] Result = Sha256.ComputeHash(SHA256Data);
178             return Convert.ToBase64String(Result);  //返回长度为44字节的字符串
179         }
180         #endregion
181     }
182 }
原文地址:https://www.cnblogs.com/BrokenIce/p/5594637.html