加密
MD5
不可逆加密,加密后可以是16位,也可以是32位,任何语言的MD5加密后结果相同。
private static string MD5(string str)
{
var md5 = System.Security.Cryptography.MD5.Create();
//计算字符串的散列值
var bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
var stringBuilder = new StringBuilder();
foreach (var b in bytes)
{
stringBuilder.Append(b.ToString("x2"));
}
return stringBuilder.ToString();
}
DES
可逆对称加密,优点:速度快。缺点:密钥管理不方便,要求共享密钥。特点:不同写法对应的加密值不同。
/// <summary>
/// DES for encryption and decryption
/// </summary>
public static class DesHelper
{
/// <summary>
/// DES encryption
/// </summary>
/// <param name="pToEncrypt"></param>
/// <returns></returns>
public static string DesEncrypt(string pToEncrypt)
{
var sKey = GetKey();
var des = new DESCryptoServiceProvider();
var inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
des.Key = Encoding.ASCII.GetBytes(sKey);
des.IV = Encoding.ASCII.GetBytes(sKey);
var ms = new MemoryStream();
var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
var ret = new StringBuilder();
foreach (var b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
}
/// <summary>
/// DES decryption
/// </summary>
/// <param name="pToDecrypt"></param>
/// <returns></returns>
public static string DesDecrypt(string pToDecrypt)
{
var sKey = GetKey();
var des = new DESCryptoServiceProvider();
var inputByteArray = new byte[pToDecrypt.Length / 2];
for (var x = 0; x < pToDecrypt.Length / 2; x++)
{
var i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
des.Key = Encoding.ASCII.GetBytes(sKey);
des.IV = Encoding.ASCII.GetBytes(sKey);
var ms = new MemoryStream();
var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
}
/// <summary>
/// get Key
/// </summary>
/// <returns></returns>
private static string GetKey()
{
return "str";
}
}
RSA
可逆非对称加密,需要两个key,公钥和私钥,每次产生的公钥和私钥不一致,加密的结果就不一致
优点:密钥方便管理,缺点:速度慢
特性:需要通过代码生成一对公钥(加密key)和私钥(解密key,公钥私钥只与是否公开有关:
A:如果公开公钥(加密key):只有我接受加密信息才有用,因为只有我有私钥(解密key),能解密。
B:如果公开私钥(解密key):用于签名,表示加密数据一定是我发的,因为只有公钥(加密key)。
public static class RsaEncrypt
{
/// <summary>
/// 生成公钥和私钥
/// </summary>
/// <returns></returns>
public static KeyValuePair<string, string> GetKeyPair()
{
var rsa = new RSACryptoServiceProvider();
var publicKey = rsa.ToXmlString(false);
var privateKey = rsa.ToXmlString(true);
return new KeyValuePair<string, string>(publicKey, privateKey);
}
/// <summary>
/// 加密:内容+公钥,事先需要生成公钥和私钥
/// </summary>
/// <param name="content">加密内容</param>
/// <param name="publicKey">公钥</param>
/// <returns></returns>
public static string Encrypt(string content, string publicKey)
{
var rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(publicKey);
var unicodeEncoding = new UnicodeEncoding();
var dataToEncrypt = unicodeEncoding.GetBytes(content);
var resultBytes = rsa.Encrypt(dataToEncrypt, false);
return Convert.ToBase64String(resultBytes);
}
/// <summary>
/// 解密-内容+私钥
/// </summary>
/// <param name="content"></param>
/// <param name="privateKey"></param>
/// <returns></returns>
public static string Decrypt(string content, string privateKey)
{
var dataToDecrypt = Convert.FromBase64String(content);
var rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(privateKey);
var resultBytes = rsa.Decrypt(dataToDecrypt, false);
var byteConverter = new UnicodeEncoding();
return byteConverter.GetString(resultBytes);
}
}
//调用
static void Main()
{
//方式一:先生成公钥和私钥,然后加密和解密
//KeyValuePair是一个键值对, myKey.Key 代表的公钥;myKey.Value 代表的私钥
var myKey = RsaEncrypt.GetKeyPair();
var msg1 = "mr1";
//加密
var result1 = RsaEncrypt.Encrypt(msg1, myKey.Key);
//解密
var result2 = RsaEncrypt.Decrypt(result1, myKey.Value);
Console.WriteLine(result1);
Console.WriteLine(result2);
Console.Read();
}