using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Security;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using System.Security.Cryptography;
namespace Utils
{
///
///
/// 常用工具类——加密解密类
/// -------------------------------------------------
/// StringEncode:返回 HTML 字符串的编码结果
/// StringDecode:返回 HTML 字符串的解码结果
/// UrlEncode:返回 URL 字符串的编码结果
/// UrlDecode:返回 URL 字符串的解码结果
/// DESEncrypt:DES加密
/// DESDecrypt:DES解密
/// MD5:MD5函数
/// SHA256:SHA256函数
///
public class EncyptHelper
{
///
/// 32位Key值:
///
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 };
#region 返回 HTML 字符串的编码结果
///
/// 返回 HTML 字符串的编码结果
///
/// 字符串
/// 编码结果
public static string StringEncode(string str)
{
return HttpUtility.HtmlEncode(str);
}
#endregion
#region 返回 HTML 字符串的解码结果
///
/// 返回 HTML 字符串的解码结果
///
/// 字符串
/// 解码结果
public static string StringDecode(string str)
{
return HttpUtility.HtmlDecode(str);
}
#endregion
#region 返回 URL 字符串的编码结果
///
/// 返回 URL 字符串的编码结果
///
/// 字符串
/// 编码结果
public static string UrlEncode(string str)
{
return HttpUtility.UrlEncode(str);
}
#endregion
#region 返回 URL 字符串的解码结果
///
/// 返回 URL 字符串的解码结果
///
/// 字符串
/// 解码结果
public static string UrlDecode(string str)
{
return HttpUtility.UrlDecode(str);
}
#endregion
#region DES加密
///
/// DES加密
///
/// 待加密字串
/// 加密后的字符串
public static string DESEncrypt(string strSource)
{
return DESEncrypt(strSource, DESKey);
}
///
/// DES加密
///
/// 待加密字串
/// Key值
/// 加密后的字符串
public static string DESEncrypt(string strSource, byte[] key)
{
SymmetricAlgorithm sa = Rijndael.Create();
sa.Key = key;
sa.Mode = CipherMode.ECB;
sa.Padding = PaddingMode.Zeros;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);
byte[] byt = Encoding.Unicode.GetBytes(strSource);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
#endregion
#region DES解密
///
/// DES解密
///
/// 待解密的字串
/// 解密后的字符串
public static string DESDecrypt(string strSource)
{
return DESDecrypt(strSource, DESKey);
}
///
/// DES解密
///
/// 待解密的字串
/// 32位Key值
/// 解密后的字符串
public static string DESDecrypt(string strSource, byte[] key)
{
SymmetricAlgorithm sa = Rijndael.Create();
sa.Key = key;
sa.Mode = CipherMode.ECB;
sa.Padding = PaddingMode.Zeros;
ICryptoTransform ct = sa.CreateDecryptor();
byte[] byt = Convert.FromBase64String(strSource);
MemoryStream ms = new MemoryStream(byt);
CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs, Encoding.Unicode);
return sr.ReadToEnd();
}
#endregion
#region MD5函数
///
/// MD5函数,需引用:using System.Security.Cryptography;
///
/// 原始字符串
/// MD5结果
public static string MD5(string str)
{
byte[] b = Encoding.Default.GetBytes(str);
b = new MD5CryptoServiceProvider().ComputeHash(b);
string ret = "";
for (int i = 0; i < b.Length; i++)
ret += b[i].ToString("x").PadLeft(2, '0');
return ret;
}
#endregion
#region SHA256函数
///
/// SHA256函数
///
/// /// 原始字符串
/// SHA256结果
public static string SHA256(string str)
{
byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
SHA256Managed Sha256 = new SHA256Managed();
byte[] Result = Sha256.ComputeHash(SHA256Data);
return Convert.ToBase64String(Result); //返回长度为44字节的字符串
}
#endregion
}
}