public class EncryptAndDecrypt
{
#region MD5 加密【不可逆加密】
#region MD5 使用
/// <summary>
/// 前缀
/// </summary>
private static readonly string Prefix = "kiam";
/// <summary>
/// 后缀
/// </summary>
private static readonly string Suffix = "kiam";
/// <summary>
/// 给输入字符串 添加 指定的前缀和后缀【防止被破解】
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
private static string AddFix(string input)
{
return Prefix + input + Suffix;
}
#endregion
/// <summary>
/// 使用MD5加密 得到32位长度的字符
/// </summary>
/// <param name="input"> 待加密的字符串</param>
/// <returns></returns>
public static string EncryptMD5(string input)
{
// MD5 md5 =new MD5CryptoServiceProvider();
MD5 md = MD5.Create();
//给输入字符串 添加 指定的前缀和后缀【防止被破解】
input = AddFix(input);
//按照 UTF-8的格式,把字符串 变成 字节数组
byte[] bytes = Encoding.UTF8.GetBytes(input);
// 是MD5中的 ComputeHash 方法,把字节数据转换成 固定长度的 字节数组
byte[] enBytes = md.ComputeHash(bytes);
//通过BitConverter 把得到的 字节数据,转换成 字符串
string result = BitConverter.ToString(enBytes);
md.Dispose();
return result.Replace("-", "");
}
/// <summary>
/// 使用MD5加密 得到16位长度的字符
/// </summary>
/// <param name="input">待加密的字符串</param>
/// <returns></returns>
public static string EncryptMD5Byte16(string input)
{
// MD5 md5 =new MD5CryptoServiceProvider();
MD5 md = MD5.Create();
//给输入字符串 添加 指定的前缀和后缀【防止被破解】
input = AddFix(input);
byte[] bytes = Encoding.UTF8.GetBytes(input);
byte[] enBytes = md.ComputeHash(bytes);
string result = BitConverter.ToString(enBytes, 4, 8);
md.Dispose();
return result.Replace("-", "");
}
#endregion
#region SHA1加密【不可逆加密,没有解密】
/// <summary>
/// SHA1加密
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string EncryptBySHA1(string input)
{
SHA1 sha = new SHA1CryptoServiceProvider();
input = AddFix(input);
byte[] bytes = Encoding.Unicode.GetBytes(input);
byte[] result = sha.ComputeHash(bytes);
string output = BitConverter.ToString(result);
return output.Replace("-", "");
}
#endregion
#region Base64编码
#region 编码
/// <summary>
/// 给字符串编码
/// </summary>
/// <param name="input">待编码的字符串</param>
/// <returns></returns>
public static string Base64Encoder(string input)
{
byte[] bytes = Encoding.UTF8.GetBytes(input);
return Convert.ToBase64String(bytes);
}
#endregion
#region 解码
/// <summary>
/// 解码
/// </summary>
/// <param name="input">待解码的 base64 字符串</param>
/// <returns></returns>
public static string Base64Decoder(string input)
{
byte[] bytes = Convert.FromBase64String(input); ;
return Encoding.UTF8.GetString(bytes);
}
#endregion
#endregion
#region 对称加密【DES】
/// <summary>
/// 密钥
/// </summary>
private static readonly string _Key = "干翻反攻倒算发光法第三方噶水电费";
/// <summary>
/// 偏移量
/// </summary>
private static readonly string _Iv = "Java";
/// <summary>
/// DES 加密
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string EncryptString(string input)
{
//设置密钥[密钥的长度必须不大于8]
string sKey = Base64Encoder(_Key);
sKey = sKey.Length > 8 ? sKey.Substring(0, 8) : sKey;
//设置偏离量
string sIv = Base64Encoder(_Iv);
byte[] data = Encoding.UTF8.GetBytes(input);
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
//设置密钥
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
//设置偏移量
des.IV = ASCIIEncoding.ASCII.GetBytes(sIv);
ICryptoTransform desencrypt = des.CreateEncryptor();
byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
return Convert.ToBase64String(result);
}
}
/// <summary>
/// DES解密
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string DecryptString(string input)
{
//设置密钥
string sKey = Base64Encoder(_Key);
sKey = sKey.Length > 8 ? sKey.Substring(0, 8) : sKey;
//设置偏离量
string sIv = Base64Encoder(_Iv);
try
{
byte[] inputByteArray = Convert.FromBase64String(input);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sIv);
ICryptoTransform desencrypt = des.CreateDecryptor();
byte[] result = desencrypt.TransformFinalBlock(inputByteArray, 0, inputByteArray.Length);
return Encoding.UTF8.GetString(result);
}
catch
{
return input;
}
}
#endregion
}