Hi,
Just wondering.. why is a CryptoStream with Rijndael/AES about 4 times
slower than one with 3DES (I've tried both Rijndael and
RijndaelManaged). Shouldn't it be the other way around?
Example code below.. Rijndael takes about 2s here, TripleDES only 0.5s.
Is there something wrong with my code? I sure hope so.. 3DES already is
pretty slow and Rijndael should be a whole lot faster normally.
thanks,
Max
MemoryStream ms = new MemoryStream();
SymmetricAlgorithm algo = Rijndael.Create(); // try TripleDES.Create()
ICryptoTransform transform = algo.CreateEncryptor();
CryptoStream cs = new CryptoStream(ms,transform,CryptoStreamMode.Write);
byte[] data = new byte[4096];
DateTime t = DateTime.Now;
for (int i = 0; i < 10000000; i += data.Length) {
cs.Write(data, 0, data.Length);
}
cs.Flush();
TimeSpan dt = DateTime.Now - t;
Trace.WriteLine(dt.TotalMilliseconds);
cs.Close();
transform.Dispose();
algo.Clear();
ms.Close();
Valery Pryamikov - 17 Oct 2005 21:06 GMT
Interesting,
that only means that .Net implementation of Rijndael is not well optimized
(it's not quite AES in .Net if we will be percise due to broader range of
keys and support for 256 bit blocks while as AES is strictly 128 bits
blocks).
Brian Gladman's AES implementation beats any DES implementation that I know
by at least factor of two (as well as Tom st Denis's AES looks quite good as
well), and not even speaking of 3DES (with tripple DES applicaiton).
-Valery.
http://www.harper.no/valery
> Hi,
>
[quoted text clipped - 31 lines]
> algo.Clear();
> ms.Close();
Markus Stoeger - 17 Oct 2005 22:31 GMT
> Interesting,
> that only means that .Net implementation of Rijndael is not well
> optimized.
Looks like they did some optimizations in .net v2.. it's only
_twice as slow_ as 3des there. *eg*
I guess I'm simply expecting too much of MS.. again..
> Brian Gladman's AES implementation beats any DES implementation that I
> know by at least factor of two (as well as Tom st Denis's AES looks
> quite good as well), and not even speaking of 3DES (with tripple DES
> applicaiton).
I'll have a look at Tom's crypto lib.. always wanted to try it anyways. :)
Max
Valery Pryamikov - 18 Oct 2005 05:50 GMT
The problem may be in simple fact that 3DES is implemented in C/C++ by
Microsoft Enhanced Crytography Provider, while as Rijndael is managed C#.
And crypto-kind of tasks is more suited for C/C++ than C# (with boundary
checks and such, but block cipher encryption usually requires no memory
allocation but a lot of bit-operations).
-Valery.
http://www.harper.no/valery
>> Interesting,
>> that only means that .Net implementation of Rijndael is not well
[quoted text clipped - 13 lines]
>
> Max
Pieter Philippaerts - 19 Oct 2005 23:24 GMT
> Just wondering.. why is a CryptoStream with Rijndael/AES about 4 times
> slower than one with 3DES (I've tried both Rijndael and RijndaelManaged).
> Shouldn't it be the other way around?
As Valery said, this has everything to do with the fact that the
TripleDESCryptoServiceProvider calls into the Windows CryptoAPI (optimized C
code) and RijndaelManaged uses its own C# implementation. If you really
wan't to compare the two algorithms, you should try the
RijndaelCryptoServiceProvider from
http://www.mentalis.org/soft/projects/crypto/ This class also calls into the
Windows CryptoAPI to do the AES encryption work.
Unfortunately, there are a few constraints when using this class (due to the
fact that the AES algorithm is not supported on all Windows versions):
- the code must be running on Windows XP or higher
- the block size must be 128 bits
- the padding mode must be PKCS#7 or none
Regards,
Pieter Philippaerts