I am writing a server in C and I would like to encode file with aes.
As I know the encoding block size should be equal to the AES key length, so I need to complement last block with zeros to the required size. The problem is in decoding: how to distinguish the file contents and the complementing zeros? Well, I planned to use Base64 encoding for that purpose, but isn’t it too slow for large files? May be I should send file size before sending encoded blocks?
A common scheme is PKCS#5 padding. Basically, fill the padding (of the plaintext) with bytes equal to the padding length. Then, after decryption, look at the last byte to see how many to drop. Confirming that the dropped bytes are the same provides a quick sanity check. Some examples, in hex:
Side note: If you’re implementing the encryption yourself, read up on modes of operation as well. If you aren’t, then whatever library you’re using should be able to handle the padding.