2008-9-15 ganton
今天,我对使用Rijndael和TripleDES那一个算法有些疑惑,据我所知Rijndael比TripleDES要快些,但是在.net开发过程中有些信息让我知道在2.0中TripleDES却比Rijndael快,
为了证明其正确性,我将做一个测试。测试的结果是他们的效率基本一样。
我测试的电脑,内存2G,cpu 酷睿双核E8400 3Ghz,.net 3.5。下面是测试代码:
using System;
using System.IO;
using System.Security.Cryptography;
using System.Diagnostics;
namespace AESvsTripleDES
{
class Program
{
static void Main(string[] args)
{
double rijndaelResult;
double tripleDesResult;
using (SymmetricAlgorithm algorithm = Rijndael.Create())
{
rijndaelResult = RunTest(algorithm);
}
using (SymmetricAlgorithm algorithm = TripleDES.Create())
{
tripleDesResult = RunTest(algorithm);
}
Console.WriteLine("Rijndael Result = " + rijndaelResult.ToString());
Console.WriteLine("TripleDES Result = " + tripleDesResult.ToString());
Console.Read();
}
private static double RunTest(SymmetricAlgorithm algorithm)
{
using (MemoryStream ms = new MemoryStream())
{
using (ICryptoTransform transform = algorithm.CreateEncryptor())
{
using (CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write))
{
byte[] data = new byte[8192];
Random r = new Random(255);
for (int i = 0; i < data.Length; i++)
{
data[i] = (byte)r.Next();
}
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 10000000; i += data.Length)
{
cs.Write(data, 0, data.Length);
}
sw.Stop();
return sw.ElapsedMilliseconds;
}
}
}
}
}
}
原文地址: http://devblog.antongochev.net/2008/09/15/rijndael-vs-tripledes-in-net-performance/