5

So when I say something like:

TripleDES tripledes = TripleDES.Create();
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password, plain);
tripledes.Key = pdb.GetBytes(16);
tripledes.IV = pdb.GetBytes(16);

I get an error. The error used to be on the key, but its been fixed (I think - unless you spot something wrong). However, the error occurs when I set the IV:

tripledes.IV = pdb.GetBytes(16);

It says that its not a valid initialization vector.

How do I fix it?

Alper
  • 1
  • 12
  • 39
  • 78

1 Answers1

8

The block size for TripleDES is 64 bits. You are trying to set 128 bits.

This should work:

tripledes.IV = pdb.GetBytes(8);
Gonzalo
  • 20,805
  • 3
  • 75
  • 78
  • 1
    For reference you can check your key size with ValidKeySize (http://msdn.microsoft.com/en-us/library/system.security.cryptography.symmetricalgorithm.validkeysize) – Paul Alexander Jun 15 '11 at 21:02
  • Thanks, this seems to work for me. -- Just for future reference, how exactly do you determine the block size for an algorithm? – Alper Jun 15 '11 at 21:08
  • 2
    The BlockSize property (inherited from the SymmetricAlgorithm base class) tells you the current block size in bits for the algorithm you're using. Divide that by 8 for the number of bytes. – The Moof Jun 15 '11 at 21:39