简体   繁体   中英

Why does Java crypto AES pad my plaintext message which is exactly 16bytes?

For example, if a message is 16-byte long: String message = "AAAAAAAAAAAAAAAA" byte[] plaintext = message.getBytes();//plaintext.length = 16

the output cyphtertext would be 32-byte long, which indicates there are 2 blocks here, and the second block has been padded.

Whereas, if a message is 15-byte long or shorter: String message = "AAAAAAAAAAAAAAA" byte[] plaintext = message.getBytes();//plaintext.length = 15

the output cyphertext would also be 16-byte long.

Since AES's block is 16-byte long, why 16-byte plaintext message would be divided into two blocks and be padded?

Thanks!

It actually depends on the encryption mode . Some modes do not require padding at all, but the modes that require padding do require that messages of a multiple of the block length include an additional block full of padding bytes, otherwise it becomes ambiguous if the data is padded or not.

Since some messages will be padded, the algorithm will always look at the last bytes and interpret them as padding information. So all messages must be padded, even those that are of a suitable length in the first place.

For example, if your padding scheme adds bytes which values is the number of missing bytes to fill the block, and your message ends with 01 or 02 02 , 03 03 03 , etc. You wouldn't know if it's padding or data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM