///
/// 字符串MD5加密
///
/// 来源
/// 长度(默认是32位)
///
public static string Encrypt(string source, int length = 32)
{
if (string.IsNullOrEmpty(source)) return string.Empty;
HashAlgorithm provider = CryptoConfig.CreateFromName("MD5") as HashAlgorithm;
//这里需要注意的是编码
byte[] bytes = Encoding.UTF8.GetBytes(source);
byte[] hashValue = provider.ComputeHash(bytes);
StringBuilder sb = new StringBuilder();
switch (length)
{
case 16:
for (int i = 4; i < 12; i++)
{
sb.Append(hashValue[i].ToString("x2"));
}
break;
case 32:
for (int i = 0; i < 16; i++)
{
sb.Append(hashValue[i].ToString("x2"));
}
break;
default:
for (int i = 0; i < hashValue.Length; i++)
{
sb.Append(hashValue[i].ToString("x2"));
}
break;
}
return sb.ToString();
}
///
/// 根据stream加密文件--(获取文件摘要)
///
///
///
public static string EncryptFile(Stream stream)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] bytes = md5.ComputeHash(stream);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
sb.Append(bytes[i].ToString("x2"));
}
return sb.ToString();
}
MD5加密的一些特点:
1.不可逆加密 ,加密后密文是无法j解密出原文的;
2. 加密的内容差别跟小但是加密出来的密文差别却很大;
3. 原文相同加密出来的密文是一样的
4. 文件的大小不同,加密后的密文长度都是length长度的;文件内容有所改变时,产生的密文结果是非常大的(文件名称的改变,不会影响密文改变)
MD5的几点用途:
1.密码的保存,防止明文泄露
由于密文是可见的,所以要求密码原文不能过于简单,或者加盐(加前缀/后缀/MD5两遍等)
2.做数字签名,防止抵赖
将内容加密,由比较权威的第三方去保障,从而证明这个文件就是你做的来,不能抵赖
3.防止篡改
将内容的密文,交给使用者,使用者他拿到文件后进行MD5校验,查看内容是否被篡改
public static class Constant
{
public static string DesKey = "beizhuledexiaopangxie";
}
private static byte[] _rgbKey = ASCIIEncoding.ASCII.GetBytes(Constant.DesKey.Substring(0, 8));
private static byte[] _rgbIV = ASCIIEncoding.ASCII.GetBytes(Constant.DesKey.Substring(0, 8));
///
/// DES加密
///
/// 待加密的原文
///
public static string Encrypt(string scource)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
using (MemoryStream memStream = new MemoryStream())
{
CryptoStream cryptoStream = new CryptoStream(memStream, provider.CreateEncryptor(_rgbKey, _rgbIV), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cryptoStream);
sw.Write(scource);
sw.Flush();
cryptoStream.FlushFinalBlock();
memStream.Flush();
return Convert.ToBase64String(memStream.GetBuffer(), 0, (int)memStream.Length);
}
}
///
/// DES解密
///
/// 待解密的密文
///
public static string Decrypt(string scource)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
byte[] buffer = Convert.FromBase64String(scource);
using (MemoryStream memStream = new MemoryStream())
{
CryptoStream cryptoStream = new CryptoStream(memStream, provider.CreateDecryptor(_rgbKey, _rgbIV), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cryptoStream);
cryptoStream.Write(buffer,0,buffer.Length);
cryptoStream.FlushFinalBlock();
return ASCIIEncoding.UTF8.GetString(memStream.ToArray());
}
}
DES加密的一些特点:
1.对称可逆加密:加密和解密的key是一个,加密后可以解密回原文;
2.加密速度相比较RSA加密速度快,但是密钥安全行不能保证
///
/// 获取加密解密KEY
///
///
public static KeyValuePair<string, string> KeyValuePair()
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
var publicKey = rsa.ToXmlString(false);
var privateKey = rsa.ToXmlString(true);
return new KeyValuePair<string, string>(publicKey, privateKey);
}
///
/// RSA加密
///
/// 来源
/// 加密key
///
public static string Encrypt(string scource, string key)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(key);
UnicodeEncoding unicodeEncoding = new UnicodeEncoding();
byte[] DataToEncrypt = unicodeEncoding.GetBytes(scource);
byte[] resultBytes = rsa.Encrypt(DataToEncrypt, false);
return Convert.ToBase64String(resultBytes);
}
///
/// RSA解密
///
/// 来源
/// 解密key
///
public static string Decrypt(string scource, string key)
{
byte[] DataToEncrypt = Convert.FromBase64String(scource);
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(key);
byte[] resultBytes = rsa.Decrypt(DataToEncrypt, false);
UnicodeEncoding unicodeEncoding = new UnicodeEncoding();
return unicodeEncoding.GetString(resultBytes);
}
RSA加密的一些特点:
1.非对称可逆加密:加密和解密的key不是一个而是一对,加密后可以解密回原文;
2.加密速度相比较DES加密速度慢,但是密钥安全性好
3.加密密钥作为私钥 ,解密密钥作为公钥,能够保证不可抵赖(可以做数字签名)
4.解密密钥作为私钥,加密key作为公钥,能保证数据安全
static void Main(string[] args)
{
#region MD5
{
string testMD5 = "被煮了的小螃蟹";
Console.WriteLine("MD5加密:" + MD5Encrypt.Encrypty(testMD5));
}
#endregion
#region DES
{
string desStr = "被煮了的小螃蟹";
Console.WriteLine("DES--Encrypt加密:" + DesEncrypt.Encrypt(desStr));
Console.WriteLine("DES--Decrypt解密:" + DesEncrypt.Decrypt(DesEncrypt.Encrypt(desStr)));
}
#endregion
#region RSA
{
string desStr = "被煮了的小螃蟹";
KeyValuePair<string, string> keyValuePair = RsaEncrypt.KeyValuePair();
string str = RsaEncrypt.Encrypt(desStr, keyValuePair.Key);
Console.WriteLine("RSA--Encrypt加密:" + str);
Console.WriteLine("RSA--Decrypt解密:" + RsaEncrypt.Decrypt(str, keyValuePair.Value));
}
#endregion
Console.ReadLine();
}