sana
sana

Reputation: 135

Data Encryption using AES-256-CBC mode openssl , doesnt return the same size of data which doesnt need padding?

I am trying to use openssl AES to encrypt my data i found the pretty nice example in this link ., http://saju.net.in/code/misc/openssl_aes.c.txt but the question i still could found the answer it padding the data although it perform a multiple of key size . for example it needs 16 byte as input to encrypt or any multiple of 16 i gave 1024 including the null ., and it still give me an out put of size 1040 , but as what i know AES input size = out put size , if the input is a multiple of 128 bit / 16 byte . any one tried this example before me or can give me any idea ?| thanks in Advance .

Upvotes: 1

Views: 3405

Answers (1)

Jerry Coffin
Jerry Coffin

Reputation: 490593

Most padding schemes require that some minimum amount of padding always be added. This is (at least primarily) so that on the receiving end, you can look at the last byte (or some small amount of data at the end) and know how much of the data at the end is padding, and how much is real data.

For example, a typical padding scheme puts zero bytes after the data with one byte at the end containing the number of bytes that are padding. For example, if you added 4 bytes of padding, the padding bytes (in hex) would be something like 00 00 00 04. Another common possibility puts that same value in all the padding bytes, so it would look like 04 04 04 04.

On the receiving end, the algorithm has to be ready to strip off the padding bytes. To do that, it looks at the last byte to tell it how many bytes of data to remove from the end and ignore. If there's no padding present, that's going to contain some value (whatever the last byte in the message happened to be). Since it has no way to know that no padding was added, it looks at that value, and removes that many bytes of data -- only in this case, it's removing actual data instead of padding.

Although it might be possible to devise a padding scheme that avoided adding extra data when/if the input happened to be an exact multiple of the block size, it's a lot simpler to just add at least one byte of padding to every message, so the receiver can count on always reading the last byte and finding how much of what it received is padding.

Upvotes: 4

Related Questions