Alex Rivera | Logout

Specified key is not a valid size for this algorithm

Asked 2010-05-27T07:27:23.137
70

I have with this code:

RijndaelManaged rijndaelCipher = new RijndaelManaged();
              
// Set key and IV
rijndaelCipher.Key = Convert.FromBase64String("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678912");
rijndaelCipher.IV = Convert.FromBase64String("1234567890123456789012345678901234567890123456789012345678901234");

I get this exception thrown:

Specified key is not a valid size for this algorithm.

Specified initialization vector (IV) does not match the block size for this algorithm.

What's wrong with this strings ? Can I count at some examples strings from you?

Edit
Report

2 Answers

111

The string "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678912" when base64-decoded yields 48 bytes (384 bits). RijndaelManaged supports 128, 192 and 256 bit keys.

A valid 128-bit key is new byte[]{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F } or if you need to get it from base64 : Convert.FromBase64String("AAECAwQFBgcICQoLDA0ODw==").

The default blocksize is 128 bits, so the same byte-array will work as the IV.

answered 2010-05-27T08:32:47.593
2

The RijndaelManaged algorithm supports key lengths of 128, 192, or 256 bits. Is your key one of these sizes?

answered 2010-05-27T07:32:08.153

Your Answer