Ho do i calculate the size of a AES256 encrypted file/buffer.
I do malloc of (n + AES_BLOCK_SIZE -1) bytes (where n is the unencrypted buffer size).
But will the size of the encrypted buffer always be that size? can it also be “smaller”?
Any idea how i pre-calculate the exact size?
Thanks
It depends on the padding you are using. The most common padding scheme (as it is reversible and contains a slight integrity check) is PKCS#5-padding: This appends a number of bytes such that the final size is a multiple of the block size, and at least one byte is appended. (Each appended byte than has the same value as the number of bytes appended.)
I.e. at most one full block (16 bytes for AES) will be appended.
n + AES_BLOCK_SIZEis always enough (and in some cases just enough), but you can calculate it more precise asn + AES_BLOCK_SIZE - (n % AES_BLOCK_SIZE).Note that there are some modes of operation which don’t need padding at all, like CTR, CFB and OFB mode. Also note that you often want to transmit the initialization vector (another full block), too.