.NET下的加密解密大全(2):对称加密
本博文列出了.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]
补充:综合编程 , 安全编程 ,