using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;//安全性,密码
namespace Common
{
public class Dencrypt
{
private static byte[] MakeMD5(byte[] kb)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] keyHash = md5.ComputeHash(kb);//给秘钥散列
md5.Clear();//释放资源
return keyHash;
}
/// <summary>
/// 根据秘钥加密
/// </summary>
/// <param name="original">明文</param>
/// <param name="key">秘钥</param>
/// <returns></returns>
public static string Encrypt(string original, string key)
{
byte[] buff = Encoding.Unicode.GetBytes(original);//unicode:编码 把明文转换成二进制数组。
byte[] kb = Encoding.Unicode.GetBytes(key);
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();//使用TripleDES算法来加密
des.Key = MakeMD5(kb);//给秘钥散列
des.Mode = CipherMode.ECB;//设置算法的模式
byte[] enByte = des.CreateEncryptor().TransformFinalBlock(buff, 0, buff.Length);//加密。(明文,从第几个开始正文,长度)
return Convert.ToBase64String(enByte);//将byte[]类型 转化为string
}
public static string Encrypt(string original)//必须得有秘钥 才能加密
{
return Encrypt(original, "默认秘钥");
}
public static string Decrypt(string enByte, string key)
{
byte[] buff = Encoding.Unicode.GetBytes(enByte);//unicode:编码 把明文转换成二进制数组。
byte[] kb = Encoding.Unicode.GetBytes(key);
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();//使用TripleDES算法来加密
des.Key = MakeMD5(kb);//给秘钥散列
des.Mode = CipherMode.ECB;//设置算法的模式
byte[] original = des.CreateDecryptor().TransformFinalBlock(buff, 0, buff.Length);
return Convert.ToBase64String(original);//将byte[]类型 转化为string
}
public static string Decrypt(string enByte)
{
return Decrypt(enByte, "默认秘钥");
}
}
}