常用的加密与解密类:
View Code
/// <summary>
/// 用于对字符串加密解密的类。
/// </summary>
public class Cryptography
{
/// <summary>
/// 3DES加密算法的种子,从Web.config中读取
/// </summary>
private static string SEED = ConfigurationSettings.AppSettings["Seed"].ToString();//加密种子
#region 构造函数
/// <summary>
/// 用于对字符串加密解密的类。.
/// </summary>
private Cryptography()
{
//构造函数
}
#endregion
#region 对输入值进行哈希运算
/// <summary>
/// 对输入值进行哈希运算。使用的算法名称:MD5、SHA 、SHA256 、SHA384 、 SHA512
/// </summary>
/// <param name="strSource">源字符串内容。</param>
public static string ComputeHash(string strSource)
{
const string Cryptography = "SHA";
return ComputeHash(strSource, Cryptography);
}
/// <summary>
/// 计算传入字符串的哈希值。
/// </summary>
/// <param name="strSource">源字符串内容。</param>
/// <param name="strCryptography">要使用的算法名称。</param>
public static string ComputeHash(string strSource, string strCryptography)
{
byte[] data = ConvertStringToByteArray(strSource);
byte[] result = ((HashAlgorithm)CryptoConfig.CreateFromName(strCryptography)).ComputeHash(data);
return Convert.ToBase64String(result);
}
#endregion
#region 3DES加密
/// <summary>
/// 对传入的字符串使用相应的密码进行加密(使用3DES加密算法)
/// </summary>
/// <param name="strSource">需要加密的源字符串。</param>
/// <param name="strPassword">密匙串。</param>
public static string Encrypt(string strSource, string strPassword)
{
byte[] hashvalue = ConvertStringToByteArray(ComputeHash(strPassword, "SHA256"));
byte[] desiv = new byte[8];
byte[] deskey = new byte[24];
int step = 0;
for (int i = 0; i < 32; i++)
{
if (i < 8)
desiv[i] = hashvalue[step];
else
deskey[i - 8] = hashvalue[step];
step += 2;
}
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
ICryptoTransform desencrypt = des.CreateEncryptor(deskey, desiv);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, desencrypt, CryptoStreamMode.Write);
byte[] plainBytes = Encoding.Unicode.GetBytes(strSource);
try
{
cs.Write(plainBytes, 0, plainBytes.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
finally
{
ms.Close();
cs.Close();
}
}
/// <summary>
/// 对传入的字符串使用相应的密码进行加密(使用3DES加密算法)
/// </summary>
/// <param name="strPassword">需要加密的源字符串。</param>
/// <returns>已加密的字符串</returns>
public static string Encrypt(string strPassword)
{
byte[] hashvalue = ConvertStringToByteArray(ComputeHash(SEED, "SHA256"));
byte[] desiv = new byte[8];
byte[] deskey = new byte[24];
int step = 0;
for (int i = 0; i < 32; i++)
{
if (i < 8)
desiv[i] = hashvalue[step];
else
deskey[i - 8] = hashvalue[step];
step += 2;
}
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
ICryptoTransform desencrypt = des.CreateEncryptor(deskey, desiv);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, desencrypt, CryptoStreamMode.Write);
byte[] plainBytes = Encoding.Unicode.GetBytes(strPassword);
try
{
cs.Write(plainBytes, 0, plainBytes.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
finally
{
ms.Close();
cs.Close();
}
}
#endregion
#region MD5加密
/// <summary>
/// 标准MD5算法
/// </summary>
/// <param name="source">要加密的字符串</param>
/// <returns></returns>
public static string MD5(string source)
{
string md5String = string.Empty;
try
{
byte[] byteCode = System.Text.Encoding.GetEncoding("GB2312").GetBytes(source.Trim());
byteCode = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(byteCode);
for (int i = 0; i < byteCode.Length; i++)
{
md5String += byteCode[i].ToString("x").PadLeft(2, '0');
}
}
catch (Exception ex)
{
throw ex;
}
return md5String;
}
#endregion
#region 解密(只能是3DES)
/// <summary>
/// 对传入的字符串使用相应的密码进行解密(3DES算法)
/// </summary>
/// <param name="strSource">需要解密的源字符串。</param>
/// <param name="strPassword">密匙串。</param>
public static string Decrypt(string strSource, string strPassword)
{
byte[] hashvalue = ConvertStringToByteArray(ComputeHash(strPassword, "SHA256"));
byte[] desiv = new byte[8];
byte[] deskey = new byte[24];
int step = 0;
for (int i = 0; i < 32; i++)
{
if (i < 8)
desiv[i] = hashvalue[step];
else
deskey[i - 8] = hashvalue[step];
step += 2;
}
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
ICryptoTransform decrypt = des.CreateDecryptor(deskey, desiv);
byte[] instream = Convert.FromBase64String(strSource);
MemoryStream ms = new MemoryStream(instream);
CryptoStream cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Read);
byte[] plainBytes = new byte[instream.Length];
try
{
cs.Read(plainBytes, 0, plainBytes.Length);
return Encoding.Unicode.GetString(plainBytes);
//string strNew = Encoding.Unicode.GetString(plainBytes);
//strNew = strNew.Substring(0, strNew.Length - 3);
//return strNew;
//return Encoding.Unicode.GetString(plainBytes);
}
catch
{
throw;
}
finally
{
ms.Close();
try
{
cs.Close();
}
catch
{
}
}
}
/// <summary>
/// 对传入的字符串使用相应的密码进行解密
/// </summary>
/// <param name="strPassword">需要解密的源字符串</param>
/// <returns>解密后的字符串</returns>
public static string Decrypt(string strPassword)
{
byte[] hashvalue = ConvertStringToByteArray(ComputeHash(SEED, "SHA256"));
byte[] desiv = new byte[8];
byte[] deskey = new byte[24];
int step = 0;
for (int i = 0; i < 32; i++)
{
if (i < 8)
desiv[i] = hashvalue[step];
else
deskey[i - 8] = hashvalue[step];
step += 2;
}
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
ICryptoTransform decrypt = des.CreateDecryptor(deskey, desiv);
byte[] instream = Convert.FromBase64String(strPassword);
MemoryStream ms = new MemoryStream(instream);
CryptoStream cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Read);
byte[] plainBytes = new byte[instream.Length];
try
{
cs.Read(plainBytes, 0, plainBytes.Length);
string strNew = Encoding.Unicode.GetString(plainBytes);
strNew = strNew.Substring(0, strNew.Length - 3);
return strNew;
}
catch
{
throw;
}
finally
{
ms.Close();
try
{
cs.Close();
}
catch
{
}
}
}
#endregion
#region 字符串转换为字节数组
/// <summary>
/// 把字符串转换为字节数组
/// </summary>
public static Byte[] ConvertStringToByteArray(string s)
{
return (new UnicodeEncoding()).GetBytes(s);
}
#endregion
}