using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Text.RegularExpressions;
using System.Web.Security;
namespace SSCMS.CommonClass
{
/// <summary>
/// 描述:MD5加密、DES加密解密类
/// </summary>a
public class EncryptHelper
{
private static readonly string sIV = "38S62Y09F34="; //矢量
private static string sKey = "S3Y8F6S2S0T9U3D4I3O8S6L2I0C9K3LY";//默认密钥
private static SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();//构造一个对称算法
// 格式化md5 hash 字节数组所用的格式(两位小写16进制数字)
private static readonly string m_strHexFormat = "x2";
/// <summary>
/// 使用当前缺省的字符编码对字符串进行加密
/// </summary>
/// <param name="str">需要进行md5演算的字符串</param>
/// <returns>用小写字母表示的32位16进制数字字符串</returns>
public static string md5(string str)
{
return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5").ToLower();
// return (string)md5(str, false, Encoding.Default);
}
/// <summary>
/// 对字符串进行md5 hash计算
/// </summary>
/// <param name="str">需要进行md5演算的字符串</param>
/// <param name="raw_output">
/// false则返回经过格式化的加密字符串(等同于 string md5(string) )
/// true则返回原始的md5 hash 长度16 的 byte[] 数组
/// </param>
/// <returns>
/// byte[] 数组或者经过格式化的 string 字符串
/// </returns>
public static object md5(string str, bool raw_output)
{
return md5(str, raw_output, Encoding.Default);
}
/// <summary>
/// 对字符串进行md5 hash计算
/// </summary>
/// <param name="str">需要进行md5演算的字符串</param>
/// <param name="raw_output">
/// false则返回经过格式化的加密字符串(等同于 string md5(string) )
/// true则返回原始的md5 hash 长度16 的 byte[] 数组
/// </param>
/// <param name="charEncoder">
/// 用来指定对输入字符串进行编解码的 Encoding 类型,
/// 当输入字符串中包含多字节文字(比如中文)的时候
/// 必须保证进行匹配的 md5 hash 所使用的字符编码相同,
/// 否则计算出来的 md5 将不匹配。
/// </param>
/// <returns>
/// byte[] 数组或者经过格式化的 string 字符串
/// </returns>
public static object md5(string str, bool raw_output, Encoding charEncoder)
{
if (!raw_output)
return md5str(str, charEncoder);
else
return md5raw(str, charEncoder);
}
/// <summary>
/// 使用当前缺省的字符编码对字符串进行加密
/// </summary>
/// <param name="str">需要进行md5演算的字符串</param>
/// <returns>用小写字母表示的32位16进制数字字符串</returns>
protected static string md5str(string str)
{
return md5str(str, Encoding.Default);
}
/// <summary>
/// 对字符串进行md5加密
/// </summary>
/// <param name="str">需要进行md5演算的字符串</param>
/// <param name="charEncoder">
/// 指定对输入字符串进行编解码的 Encoding 类型
/// </param>
/// <returns>用小写字母表示的32位16进制数字字符串</returns>
protected static string md5str(string str, Encoding charEncoder)
{
byte[] bytesOfStr = md5raw(str, charEncoder);
int bLen = bytesOfStr.Length;
StringBuilder pwdBuilder = new StringBuilder(32);
for (int i = 0; i < bLen; i++)
{
pwdBuilder.Append(bytesOfStr[i].ToString(m_strHexFormat));
}
return pwdBuilder.ToString();
}
/// <summary>
/// 使用当前缺省的字符编码对字符串进行加密
/// </summary>
/// <param name="str">需要进行md5演算的字符串</param>
/// <returns>长度16 的 byte[] 数组</returns>
protected static byte[] md5raw(string str)
{
return md5raw(str, Encoding.Default);
}
/// <summary>
/// 对字符串进行md5加密
/// </summary>
/// <param name="str">需要进行md5演算的字符串</param>
/// <param name="charEncoder">
/// 指定对输入字符串进行编解码的 Encoding 类型
/// </param>
/// <returns>长度16 的 byte[] 数组</returns>
protected static byte[] md5raw(string str, Encoding charEncoder)
{
System.Security.Cryptography.MD5 md5 =
System.Security.Cryptography.MD5.Create();
return md5.ComputeHash(charEncoder.GetBytes(str));
}
/// <summary>
/// 对字符串进行DES加密
/// </summary>
public static string EncryptDes(string source)
{
//获取Webconfig中的EncryptKey值
string EncryptKey = ConfigHelper.GetConfigString("EncryptKey");
if (Regex.IsMatch(sKey, @"^[A-Za-z0-9]{32}$"))
{
sKey = EncryptKey;
}
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
string str = null;
mCSP.Key = Convert.FromBase64String(sKey);
mCSP.IV = Convert.FromBase64String(sIV);
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
byt = Encoding.UTF8.GetBytes(source);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
str = Convert.ToBase64String(ms.ToArray());
return str;
}
/// <summary>
/// 对字符串进行DES解密
/// </summary>
public static string DecryptDes(string source)
{
//获取Webconfig中的EncryptKey值
string EncryptKey = ConfigHelper.GetConfigString("EncryptKey");
if (Regex.IsMatch(sKey, @"^[A-Za-z0-9]{32}$"))
{
sKey = EncryptKey;
}
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
string str = null;
mCSP.Key = Convert.FromBase64String(sKey);
mCSP.IV = Convert.FromBase64String(sIV);
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
byt = Convert.FromBase64String(source);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
str = Encoding.UTF8.GetString(ms.ToArray());
return str;
}
}
}