1
$\begingroup$

I am writing a program in python to encrypt files. This program takes subsequent 1024*1024 (1 mebibyte) chunks of data from a file and encrypts it using AES-128. This is performed in a loop until all the data in the file is encrypted. The issue is that the size of each 1 mebibyte of data is increased when it gets encrypted.

What I need is a way to determine the new chunk size for each 1 mebibyte after its gets encrypted .

$\endgroup$
0

1 Answer 1

1
$\begingroup$

Yes, you can determine the amount that your file will grow, i.e. how much the ciphertext will expand compared to the plaintext. However, that's less determined by the block cipher than that it is determined by the block cipher mode of operation.

Very often CBC mode encryption is the default mode of operation on low level cryptographic API's. In that case - because your chunk of data is a multiple of the AES block size (16 bytes) - the ciphertext expansion is one full block of 16 bytes. If the IV is prefixed before the ciphertext then it will grow with another 16 bytes. The calculation for CBC padding size is N - (L % N) where N is the block size and L is the plaintext chunk size, both in bytes.

Other modes such as counter (CTR) mode don't use padding, but they may still store an IV or nonce with the ciphertext. Authenticated ciphertext such as AES-GCM may also store an authentication tag to provide message integrity and authenticity.

Of course, if you are using a high level API or protocol then all bets are off; you should take a look into the protocol to find out how much the files get expanded.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.