views:

304

answers:

3

Is there any way to calculate the largest outcome from an Rijndael encryption with a fixed array lenght?

Encryption method: RijndaelManaged

Padding: PKCS7

CipherMode: CBC

BlockSize 128

KeySize: 128

I need this as im converting a database where all string are going to be encrypted so i need to change the size of all string fields.

+1  A: 

Yes. Round up your input size to the nearest multiple of your block size (e.g. 128 / 8 = 16 bytes).

extraBytesNeeded = (16 - (inputSize % 16)) % 16;
maxSize = inputSize + extraBytesNeeded.
Jeff Moser
Careful with the second equation, not every language handles the modulo of a negative number the way you are assuming. C/C++ for example, will give you the wrong answer as (-a % b) == -(a % b) in those languages.
Naaff
Good point. I deleted the second one to avoid confusion.
Jeff Moser
A: 

Jeff's answer is almost correct, except that PKCS7 will always add padding to the message, even if the message exactly fits inside an integral number of blocks. Also, don't forget that if using a random IV that the IV has to be stored too. The corrected formula for the length of a PKCS7 padded message is:

extraBytesNeeded = (16 - (inputSize % 16)); // whole block of padding if input fits exactly
maxSize = inputSize + extraBytesNeeded + IVbytes;
Theran
+1  A: 
SwDevMan81