tags:

views:

303

answers:

3

I am trying to generate a key using the .net 2.0 PasswordDeriveBytes class.

  PasswordDeriveBytes pdb = new PasswordDeriveBytes("blahblahblah",null);
  byte[] iv = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
  byte[] key = pdb.CryptDeriveKey("TripleDES", "SHA1", 128, iv);

The code throws "CryptographicException: Invalid flags specified." when trying to execute the CryptDeriveKey method above. I'm trying to encrypt a database entry in asp.net and this is my first shot a crypto. I'd appreciate any help.

+1  A: 

TripleDES has a key length of 192 bits try:

byte[] key = pdb.CryptDeriveKey("TripleDES", "SHA1", 192, iv);

Try this link for example code

trampster
+2  A: 

According to MSDN:

"If the keySize parameter is set to 0 bits, the default key size for the specified algorithm is used."

MSDN PasswordDeriveBytes.CryptDeriveKey Method

PasswordDeriveBytes pdb = new PasswordDeriveBytes("blahblahblah",null);
byte[] iv = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
byte[] key = pdb.CryptDeriveKey("TripleDES", "SHA1", 0, iv);

Unless you have a good reason for specifying the size I'd suggest just leaving it 0.

Spencer Ruport
That did it, thanks.
Jon
A: 

You should consider using Rfc2898DeriveBytes instead of PasswordDeriveBytes.

samael