public static class MyEncryption
{
#region Md5加密
/// <summary>
/// 使用MD5加密
/// </summary>
/// <param name="str">需要加密的数据。</param>
/// <param name="kind">加密类型:1-普通加密;2-密码加密</param>
/// <returns></returns>
public static string EncryptMD5(this string source, int kind = 2)
{
string str = "";
byte[] data = Encoding.Unicode.GetBytes(source.ToCharArray());
try
{
if (kind == 1)//1代表加密Byte[]数组
{
byte[] result = MakeMD5(data);
string sResut = Encoding.Unicode.GetString(result);
str = sResut;
}
else
{
//作为密码方式加密
str = FormsAuthentication.HashPasswordForStoringInConfigFile(source, "MD5");
}
return str;
}
catch (Exception e)
{
LogException.WriteLog(e, "加密出错");
return null;
}
}
#endregion
#region base64加密解密
/// <summary>
/// 转换为Base64编码字符
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static string EncryptBase64(this string source)
{
if (string.IsNullOrEmpty(source))
{
LogException.WriteLog(null, "");
return null;
}
else
{
try
{
//将字符串转换成UTF-8编码的字节数组
byte[] buffer = Encoding.UTF8.GetBytes(source);
//将UTF-8编码的字节数组转换成Base64编码的字符串
string result = Convert.ToBase64String(buffer);
return result;
}
catch (Exception e)
{
LogException.WriteLog(e, "加密出错");
return null;
}
}
}
/// <summary>
/// 将Base64编码字符转换为字符
/// </summary>
/// <param name="result"></param>
/// <returns></returns>
public static string DecryptBase64(this string result)
{
if (string.IsNullOrEmpty(result))
{
LogException.WriteLog(null, "加密出错");
return null;
}
else
{
try
{
//将字符串转换成Base64编码的字节数组
byte[] buffer = Convert.FromBase64String(result);
//将字节数组转换成UTF-8编码的字符串
string source = Encoding.UTF8.GetString(buffer);
return source;
}
catch (Exception e)
{
LogException.WriteLog(e, "加密出错");
return null;
}
}
}
#endregion
#region DES加密解密
/// <summary>
/// DES加密
/// </summary>
/// <param name="source">加密数据</param>
/// <param name="key">8位字符的密钥字符串</param>
/// <param name="iv">8位字符的初始化向量字符串</param>
/// <returns></returns>
public static string EncryptDES(this string source, string key, string iv)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(key);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv);
try
{
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
int i = cryptoProvider.KeySize;
MemoryStream ms = new MemoryStream();
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cst);
sw.Write(source);
sw.Flush();
cst.FlushFinalBlock();
sw.Flush();
return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
}
catch (Exception e)
{
LogException.WriteLog(e, "加密出错");
return null;
}
}
/// <summary>
/// DES解密
/// </summary>
/// <param name="result">解密数据</param>
/// <param name="key">8位字符的密钥字符串(需要和加密时相同)</param>
/// <param name="iv">8位字符的初始化向量字符串(需要和加密时相同)</param>
/// <returns></returns>
public static string DecryptDES(this string result, string key, string iv)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(key);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv);
byte[] byEnc;
try
{
byEnc = Convert.FromBase64String(result);
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(byEnc);
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cst);
return sr.ReadToEnd();
}
catch (Exception e)
{
LogException.WriteLog(e, "加密出错");
return null;
}
}
/// <summary>
/// DES加密
/// </summary>
/// <param name="source">解密数据</param>
/// <param name="iv">8位字符的初始化向量字符串</param>
/// <returns></returns>
public static string EncryptDES(this string source, string iv)
{
return EncryptDES(source, "MHAFU365", iv);
}
/// <summary>
/// DES解密
/// </summary>
/// <param name="result">解密数据</param>
/// <param name="iv">8位字符的初始化向量字符串</param>
/// <returns></returns>
public static string DecryptDES(this string result, string iv)
{
return DecryptDES(result, "MHAFU365", iv);
}
#endregion
#region TripleDESC加密解密
#region 字符
/// <summary>
/// 使用给定密钥加密
/// </summary>
/// <param name="source">原始文字</param>
/// <param name="key">密钥</param>
/// <param name="encoding">字符编码方案</param>
/// <returns>密文</returns>
public static string EncryptTripleDESC(this string source, string key)
{
byte[] buff = System.Text.Encoding.Default.GetBytes(source);
byte[] kb = System.Text.Encoding.Default.GetBytes(key);
return Convert.ToBase64String(EncryptTripleDESC(buff, kb));
}
/// <summary>
/// 使用给定密钥解密
/// </summary>
/// <param name="result">密文</param>
/// <param name="key">密钥</param>
/// <param name="encoding">字符编码方案</param>
/// <returns>明文</returns>
public static string DecryptTripleDESC(this string result, string key, Encoding encoding)
{
byte[] buff = Convert.FromBase64String(result);
byte[] kb = System.Text.Encoding.Default.GetBytes(key);
return encoding.GetString(DecryptTripleDESC(buff, kb));
}
/// <summary>
/// 使用缺省密钥字符串加密
/// </summary>
/// <param name="source">明文</param>
/// <returns>密文</returns>
public static string EncryptTripleDESC(this string source)
{
return EncryptTripleDESC(source, "HAFU365");
}
/// <summary>
/// 使用缺省密钥解密
/// </summary>
/// <param name="result">密文</param>
/// <returns>明文</returns>
public static string DecryptTripleDESC(this string result)
{
return DecryptTripleDESC(result, "HAFU365", System.Text.Encoding.Default);
}
/// <summary>
/// 使用给定密钥加密
/// </summary>
/// <param name="source">原始数据</param>
/// <returns>密文</returns>
public static byte[] EncryptTripleDESC(this byte[] source)
{
byte[] key = System.Text.Encoding.Default.GetBytes("HAFU@365");
return EncryptTripleDESC(source, key);
}
/// <summary>
/// 使用缺省密钥解密数据
/// </summary>
/// <param name="result">密文</param>
/// <returns>明文</returns>
public static byte[] DecryptTripleDESC(this byte[] result)
{
byte[] key = System.Text.Encoding.Default.GetBytes("HAFU@365");
return DecryptTripleDESC(result, key);
}
#endregion
#region 字节数组
/// <summary>
/// 使用给定密钥加密
/// </summary>
/// <param name="source">明文</param>
/// <param name="key">密钥</param>
/// <returns>密文</returns>
public static byte[] EncryptTripleDESC(this byte[] source, byte[] key)
{
try
{
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Key = MakeMD5(key);
des.Mode = CipherMode.ECB;
return des.CreateEncryptor().TransformFinalBlock(source, 0, source.Length);
}
catch (Exception e)
{
LogException.WriteLog(e, "加密出错");
return null;
}
}
/// <summary>
/// 使用给定密钥解密数据
/// </summary>
/// <param name="result">密文</param>
/// <param name="key">密钥</param>
/// <returns>明文</returns>
public static byte[] DecryptTripleDESC(this byte[] result, byte[] key)
{
try
{
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Key = MakeMD5(key);
des.Mode = CipherMode.ECB;
return des.CreateDecryptor().TransformFinalBlock(result, 0, result.Length);
}
catch (Exception e)
{
LogException.WriteLog(e, "加密出错");
return null;
}
}
#endregion
#endregion
#region 生成MD5摘要
/// <summary>
/// 生成MD5摘要
/// </summary>
/// <param name="original">数据源</param>
/// <returns>摘要</returns>
private static byte[] MakeMD5(byte[] original)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
byte[] keyhash = hashmd5.ComputeHash(original);
hashmd5 = null;
return keyhash;
}
#endregion