1


I am having trouble with this code to encrypt a string using RinjndaelManaged. I kept getting the error "Specified initialization vector (IV) does not match the block size for this algorithm" and have matched the key and IV length and tried a 32 char length for they key and every 4 bytes from 4 to 32. The code errors at the line starting with:

aes.IV = Convert.FromBase64String(myString);

The code block is as follows:

private String AES_encrypt(String Input)
    {
        var aes = new RijndaelManaged();
        aes.KeySize = 256;
        aes.BlockSize = 256;
        aes.Padding = PaddingMode.PKCS7;
        String myString = new string('J', 32);
        aes.Key = Convert.FromBase64String(myString);
        aes.IV = Convert.FromBase64String(myString);
        var encrypt = aes.CreateEncryptor(aes.Key, aes.IV);
        byte[] xBuff = null;
        using (var ms = new MemoryStream())
        {
            using (var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write))
            {
                byte[] xXml = Encoding.UTF8.GetBytes(Input);
                cs.Write(xXml, 0, xXml.Length);
            }

            xBuff = ms.ToArray();
        }

        String Output = Convert.ToBase64String(xBuff);
        return Output;
    }
}

I only used the myString length to just to quickly iterate through a bunch of options. I'm using this particular Keysize/Block/Padding and encryption scheme to work with PHP code which would decrypt this data.

joel
  • 157
  • 1
  • 8
  • Wondering why you are using Rijndael with a 256-bit block size given that AES is Rijndael with a block size of 128-bits. Generally it is better for interoperability to use the AES block size. Further there is concern that a larger block size may not be as secure, there has been much less research in larger block sizes. – zaph May 04 '16 at 20:45
  • I'll look at this block size. I think it was used to be compatible with a PHP function which is receiving output from this code. – joel May 05 '16 at 17:55

2 Answers2

2

A string of 32 'J's will produce 24 bytes, not 16 or 32. Do not try to Base64 decode it. Best to read-up on Base64.

It is not secure to use the same value for the key and IV.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • Thanks. That was my key issue. I'm not using the all Js nor the same key and IV. I should have left in my values in the example code. At the time I thought showing the length was easier this way. – joel May 05 '16 at 17:53
-1

The resulting Base64 string that you want to set as an IV is 16 bytes (128 bit) long, whereas your encryption algorithm requires a 256 bit IV.

Change your IV to a byte array that is 32 bytes long.

Base64: 'JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ'

Decoded: '$I$I$I$I$I$I$I$I' (16 bytes = 128 bits long)
silkfire
  • 24,585
  • 15
  • 82
  • 105
  • @zaph I decoded it here and it worked, try yourself: https://www.base64decode.org/ – silkfire May 04 '16 at 21:12
  • 32 bytes Base64 decoded will produce 24 bytes, not 16, read the spec, Base64 encodes each 3 bytes into 4 bytes. 'JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ' decoded to (in hex): 2492492492492492 49249249249249249249249249249249 which is 24 bytes. When viewed in ASCII 0x92 is non-printable so you are seeing '$I$I$I$I$I$I$I$I' but the actual result is 24 bytes. Try to Base64 encode '$I$I$I$I$I$I$I$I'. – zaph May 04 '16 at 21:37