本博文列出了.NET下常用的对称加密算法,并将它们制作成小DEMO,希望能对大家有所帮助。
公共代码
[csharp]
static byte[] CreateKey(int num)
{
byte[] result = new byte[num];
Random rand = new Random();
for (int i = 0; i < num; i++)
{
result[i] = (Byte)rand.Next(1, 256);
}
return result;
}
DES
[csharp]
/// <summary>
/// DES加密算法必须使用Base64的Byte对象
/// </summary>
/// <param name="data">待加密的字符数据</param>
/// <param name="key">密匙,长度必须为64位(byte[8]))</param>
/// <param name="iv">iv向量,长度必须为64位(byte[8])</param>
/// <returns>加密后的字符</returns>
static string EnDES(string data, byte[] key, byte[] iv)
{
DES des = DES.Create();
//这行代码很重要,需要根据不同的字符串选择不同的转换格式
byte[] tmp = Encoding.Unicode.GetBytes(data);
Byte[] encryptoData;
ICryptoTransform encryptor = des.CreateEncryptor(key, iv);
using (MemoryStream memoryStream = new MemoryStream())
{
using (var cs = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter writer = new StreamWriter(cs))
{
writer.Write(data);
writer.Flush();
}
}
encryptoData = memoryStream.ToArray();
}
des.Clear();
return Convert.ToBase64String(encryptoData);
}
/// <summary>
/// DES解密算法
/// </summary>
/// <param name="data">待加密的字符数据</param>
/// <param name="key">密匙,长度必须为64位(byte[8]))</param>
/// <param name="iv">iv向量,长度必须为64位(byte[8])</param>
/// <returns>加密后的字符</returns>
static string DeDes(string data, Byte[] key, Byte[] iv)
{
string resultData = string.Empty;
//这行代码很重要
Byte[] tmpData = Convert.FromBase64String(data);//转换的格式挺重要
DES des = DES.Create();
ICryptoTransform decryptor = des.CreateDecryptor(key, iv);
using (var memoryStream = new MemoryStream(tmpData))
{
using (var cs = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
StreamReader reader = new StreamReader(cs);
resultData = reader.ReadLine();
}
}
return resultData;
}
调用代码
[csharp]
//DES对称加密
Console.WriteLine("DES对称加密");
Byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
Byte[] key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
string inputDES_1 = inputString();
string enData = EnDES(inputDES_1, key, iv);<p>
Console.WriteLine("加密后的数据:{0}", enData);
Console.WriteLine("解密后的数据:{0}", DeDes(enData, key, iv));</p>
TripleDES(3DES)
[csharp]
/// <summary>
/// TripleDES加密
/// </summary>
/// <param name="data">待加密的字符数据</param>
/// <param name="key">密匙,长度可以为:128位(byte[16]),192位(byte[24])</param>
/// <param name="iv">iv向量,长度必须为64位(byte[8])</param>
/// <returns>加密后的字符</returns>
static string EnTripleDES(string data, Byte[] key, Byte[] iv)
{
Byte[] tmp = null;
Byte[] tmpData = Encoding.Unicode.GetBytes(data);
TripleDES tripleDes = TripleDES.Create();
ICryptoTransform encryptor = tripleDes.CreateEncryptor(key, iv);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
StreamWriter writer = new StreamWriter(cs);
writer.Write(data);
writer.Flush();//这句很重要,在对流操作结束后必须用这句话强制将缓冲区中的数据全部写入到目标对象中
}
tmp = ms.ToArray();
}
return Convert.ToBase64String(tmp);
}
/// <summary>
/// TripleDES解密
/// </summary>
/// <param name="data">待加密的字符数据</param>
/// <param name="key">密匙,长度可以为:128位(byte[16]),192位(byte[24])</param>
/// <param name="iv">iv向量,长度必须为64位(byte[8])</param>
/// <returns>加密后的字符</returns>
static string DeTripleDES(string data, Byte[] key, Byte[] iv)
{
Byte[] tmp = Convert.FromBase64String(data);
string result = string.Empty;
TripleDES tripleDES = TripleDES.Create();
ICryptoTransform decryptor = tripleDES.CreateDecryptor(key, iv);
using (MemoryStream ms = new MemoryStream(tmp))
{
using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
{
StreamReader reader = new StreamReader(cs);
result = reader.ReadLine();
}
}
tripleDES.Clear();
return result;
}
调用代码
[csharp]
TripleDES对称加密
Console.WriteLine("TripleDES对称加密");
Byte[] iv = CreateKey(8);
Byte[] key = CreateKey(32);
string inputDES_1 = inputString();
string enData = EnTripleDES(inputDES_1, key, iv);
Console.WriteLine("加密后的数据:{0}", enData);
Console.WriteLine("解密后的数据:{0}", DeTripleDES(enData, key, iv)); <
AES
[csharp]
/// <summary>
/// AES加密
/// </summary>
/// <param name="data">待加密的字符数据</param>
/// <param name="key">密匙,长度可以为:128位(byte[16]),192位(byte[24]),256位(byte[32])</param>
/// <param name="iv">iv向量,长度必须为128位(byte[16])</param>
/// <returns>加密后的字符</returns>
static string EnAES(string data, byte[] key, byte[] iv)
{
Aes aes = Aes.Create();
byte[] tmp = null;
ICryptoTransform encryptor = aes.CreateEncryptor(key, iv);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
StreamWriter writer = new StreamWriter(cs);
writer.Write(data);
writer.Flush();
writer.Close();
}
tmp = ms.ToArray();
}
return Convert.ToBase64String(tmp);
}
/// <summary>
/// AES解密
/// </summary>
/// <param name="data">待加密的字符数据</param>
/// <param name="key">密匙,长度可以为:128位(byte[16]),192位(byte[24]),256位(byte[32])</param>
/// <param name="iv">iv向量,长度必须为128位(byte[16])</param>
/// <returns>加密后的字符</returns>
static string DeAES(string data, byte[] key, byte[] iv)
{
string result = string.Empty;
Aes aes = Aes.Create();
ICryptoTransform decryptor = aes.CreateDecryptor(key, iv);
using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(data)))
{
using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
{
StreamReader reader = new StreamReader(cs);
result = reader.ReadLine();
reader.Close();
}
}
aes.Clear();
return result;
}
调用代码
[csharp]
<p> //AES对称加密
Console.WriteLine("AES对称加密");
Byte[] iv = CreateKey(16);
Byte[] key = CreateKey(24);
string inputDES_1 = inputString();</p><p> string enData =EnAES(inputDES_1, key, iv);</p><p>
Console.WriteLine("加密后的数据:{0}", enData);
Console.WriteLine("解密后的数据:{0}", DeAES(enData, key, iv));</p>
Rijndael
[csharp]
/// <summary>
/// Rijndael加密
/// </summary>
/// <param name="data">需要加密的字符数据</param>
/// <param name="key">密匙,长度可以为:64位(byte[8]),128位(byte[16]),192位(byte[24]),256位(byte[32])</param>
/// <param name="iv">iv向量,长度为128(byte[16])</param>
/// <returns>加密后的字符</returns>
static string EnRijndael(string data, byte[] key, byte[] iv)
{
Rijndael rijndael = Rijndael.Create();
byte[] tmp = null;
ICryptoTransform encryptor = rijndael.CreateEncryptor(key, iv);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
StreamWriter writer = new StreamWriter(cs);
writer.Write(data);
writer.Flush();
}
tmp = ms.ToArray();
}
return Convert.ToBase64String(tmp);
}
/// <summary>
/// Rijndael解密
/// </summary>
/// <param name="data">需要加密的字符数据</param>
/// <param name="key">密匙,长度可以为:64位(byte[8]),128位(byte[16]),192位(byte[24]),256位(byte[32])</param>
/// <param name="iv">iv向量,长度为128(byte[16])</param>
/// <returns>解密后的字符</returns>
static string DeRijndael(string data, byte[] key, byte[] iv)
{
string result = string.Empty;
Rijndael rijndael = Rijndael.Create();
ICryptoTransform decryptor = rijndael.CreateDecryptor(key, iv);
using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(data)))
{
using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
{
StreamReader reader = new StreamReader(cs);
result = reader.ReadLine();
reader.Close();
}
}
return result;
}
调用代码
[csharp]
//Rijndael对称加密
Console.WriteLine("Rijndael对称加密");
Byte[] iv = CreateKey(16);
Byte[] key = CreateKey(32);
string inputDES_1 = inputString();
string enData = EnRijndael(inputDES_1, key, iv);
Console.WriteLine("加密后的数据:{0}", enData);
Console.WriteLine("解密后的数据:{0}", DeRijndael(enData, key, iv));