# 1. 常见对称加密算法

## 1.1 DES 和 DESede 算法

DES算法和DESede算法（又称三重DES算法） 统称DES系列算法。DES全称为Data Encryption Standard，即数据加密标准，是一种使用密钥加密的块算法。而DESede就是针对同一块数据做三次DES加密。这里就不对原理做过多的介绍了，来看看.net core里如何实现DES加/解密吧。

``````namespace Utils.Security
{
public class DesHelper
{

}
}
``````

``````using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Utils.Security
{
public static class DesHelper
{
static DesHelper()
{
DesHandler =  DES.Create("DES");
DesHandler.Key = Convert.FromBase64String("L1yzjGB2sI4=");
DesHandler.IV = Convert.FromBase64String("uEcGI4JSAuY=");
}

private static DES DesHandler { get; }

/// <summary>
/// 加密字符
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static string Encrypt(string source)
{
try
{
using (var memStream = new MemoryStream())
using (var cryptStream = new CryptoStream(memStream, DesHandler.CreateEncryptor(DesHandler.Key, DesHandler.IV),
CryptoStreamMode.Write))
{
var bytes = Encoding.UTF8.GetBytes(source);
cryptStream.Write(bytes, 0, bytes.Length);
cryptStream.FlushFinalBlock();

return Convert.ToBase64String(memStream.ToArray());
}
}
catch (Exception e)
{
Console.WriteLine(e);
return null;
}
}

/// <summary>
/// 解密
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static string Decrypt(string source)
{
try
{
using (var mStream = new MemoryStream(Convert.FromBase64String(source)))
using (var cryptoStream =
{
}
}
catch (Exception e)
{
Console.WriteLine(e);
return null;
}
}
}
}
``````

## 1.2 AES 加密算法

``````namespace Utils.Security
{
public static class AesHelper
{

}
}
``````

``````using System;
using System.IO;
using System.Security.Cryptography;

namespace Utils.Security
{
public static class AesHelper
{
static AesHelper()
{
AesHandler = Aes.Create();
AesHandler.Key = Convert.FromBase64String("lB2BxrJdI4UUjK3KEZyQ0obuSgavB1SYJuAFq9oVw0Y=");
AesHandler.IV = Convert.FromBase64String("6lra6ceX26Fazwj1R4PCOg==");
}

private static Aes AesHandler { get; }

public static string Encrypt(string source)
{
using (var mem = new MemoryStream())
using (var stream = new CryptoStream(mem, AesHandler.CreateEncryptor(AesHandler.Key, AesHandler.IV),
CryptoStreamMode.Write))
{
using (var writer = new StreamWriter(stream))
{
writer.Write(source);
}
return Convert.ToBase64String(mem.ToArray());
}

}

public static string Decrypt(string source)
{
var data = Convert.FromBase64String(source);
using (var mem = new MemoryStream(data))
using (var crypto = new CryptoStream(mem, AesHandler.CreateDecryptor(AesHandler.Key, AesHandler.IV),
{
}
}
}
}
``````

# 2. 常见非对称加密算法

## 2.1 RSA算法

RSA算法是标准的非对称加密算法，名字来源是三位发明者的姓氏首字母。RSA公开密钥密码体制是一种使用不同的加密密钥与解密密钥，“由已知加密密钥推导出解密密钥在计算上是不可行的”密码体制 。其安全性取决于密钥的长度，1024位的密钥几乎不可能被破解。

``````namespace Utils.Security
{
public static class RsaHelper
{

}
}
``````

``````using System;
using System.Security.Cryptography;

namespace Utils.Security
{
public static class RsaHelper
{
public static RSAParameters PublicKey { get; private set; }
public static RSAParameters PrivateKey { get; private set; }

static RsaHelper()
{

}

public static void InitWindows()
{
var parameters = new CspParameters()
{
KeyContainerName = "RSAHELPER" // 默认的RSA保存密钥的容器名称
};
var handle = new RSACryptoServiceProvider(parameters);
PublicKey = handle.ExportParameters(false);
PrivateKey = handle.ExportParameters(true);
}

public static void ExportKeyPair(string publicKeyXmlString, string privateKeyXmlString)
{
var handle  = new RSACryptoServiceProvider();
handle.FromXmlString(privateKeyXmlString);
PrivateKey = handle.ExportParameters(true);
handle.FromXmlString(publicKeyXmlString);
PublicKey = handle.ExportParameters(false);
}
public static byte[] Encrypt(byte[] dataToEncrypt)
{
try
{
byte[] encryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.ImportParameters(PublicKey);
encryptedData = RSA.Encrypt(dataToEncrypt, true);
}

return encryptedData;
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
return null;
}
}

public static byte[] Decrypt(byte[] dataToDecrypt)
{
try
{
byte[] decryptedData;
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(PrivateKey);
decryptedData = rsa.Decrypt(dataToDecrypt, true);
}
return decryptedData;
}
catch (CryptographicException e)
{
Console.WriteLine(e.ToString());
return null;
}
}
}
}
``````

# 3. 信息摘要算法

## 3.1 MD5算法

``````namespace Utils.Security
{
public static class Md5Helper
{

}
}
``````

``````using System.Security.Cryptography;
using System.Text;

namespace Utils.Security
{
public static class Md5Helper
{
private static MD5 Hanlder { get; } = new MD5CryptoServiceProvider();

public static string GetMd5Str(string source)
{
var data = Encoding.UTF8.GetBytes(source);
var security = Hanlder.ComputeHash(data);
var sb = new StringBuilder();
foreach (var b in security)
{
sb.Append(b.ToString("X2"));
}

return sb.ToString();
}
}
}
``````

file