views:

1062

answers:

2

Guys, I'm working on a encryption functionality based on classes inherited from SymmetricAlgorithm such as TripleDes, DES, etc.

Basically there're two options to generate consistent key and IV for my algorithm class, PasswordDeriveBytes and Rfc2898DeriveBytes, both inherit from DeriveBytes abstract class.

The PasswordDeriveBytes.GetBytes() method is marked as obsolete in .NET framework while Rfc2898DeriveBytes.GetBytes() is recommended, as it matches the PBKDF2 standard. However, based on my testing, calling the same GetBytes() method in Rfc2898DeriveBytes class is almost 15 times slower than that in PasswordDeriveBytes class, which leads to unexpected CPU usage (always higher than 50%).

Here're some testing data:
.iterations: 100, .Algorithm type: DES, .Original Text: "I'm a test key, encrypt me please"
.time - PasswordDeriveBytes: 99ms, - Rfc2898DeriveBytes: 1,373ms

Based on the testing, the bad performance of Rfc2898DeriveBytes is not acceptable in production environment.

Has anyone noticed this problem before? Any solution I can still use a standard one without hitting the performance? Any risk to use an obsolete method (could be removed in future version)?

Thanks guys!

[Edit]Probably I found where the problem is... The default iteration count number for PasswordDeriveBytes is 100, while for Rfc2898DeriveBytes is 1000. After I changed them to the same number as 1000, executing Rfc2898DeriveBytes is only double time.

+2  A: 

This blogpost talks about the differences between the two: http://blogs.msdn.com/shawnfa/archive/2004/04/14/generating-a-key-from-a-password.aspx

Yannick M.
Thx a billion, that's really really helpful! :)
tshao
+6  A: 

They aren't the same thing.

Rfc2898DeriveBytes is an implementation of PBKDF2. PasswordDeriveBytes is an implementation of PBKDF1. PBKDF2 generates a different output, using a different method, and a much larger number of rounds than PBKDF1.

Password hashing functions, such as these, which are used for key derivation are supposed to be slow. That's the point - it makes them much more difficult to crack.

The two functions are not compatible, and PasswordDeriveBytes is not nearly as secure.

BlackAura
Thanks BlackAura. I can understand the PBKDF2 implementation ought to be slow, but isn't there any best practice to use the Rfc2989DeriveBytes class, such as how to cache/reuse the same key/IV? Running a method multiple times as slow as that in production environment is not acceptable. :P
tshao
Generally, you'd only want to use these functions to generate a key from a password. Usually for something like a password-protected archive, or similar.To encrpyt an archive, you'd generate a random IV, and generate a key from the password and the IV. You store the IV (but never the key). You can then re-use the key for every file in the archive, as long as each file is encrypted with a different IV (also stored in the archive).The only other use for these functions is password hashing. You'd do this once, when a user logs in.For any other use, there's probably a better way.
BlackAura