One Block at a Time
AES works on 16 bytes at a time, so a message is cut into blocks and the last block is padded; ECB and CBC always add 1 to 16 padding bytes.
The question
AES takes exactly 16 bytes in and gives exactly 16 bytes out. A message is rarely 16 bytes long. How does a cipher that only handles one block turn a message of 20 bytes into something that can be encrypted, and how long is the result?
Two things have to happen. The message is cut into blocks of 16 bytes, and the last block, which is usually short, is filled up. This lesson runs real AES-128 through OpenSSL and counts the bytes. The key is test material, written in the command; the modes themselves come in the next lessons, and the round function is in one-aes-block.
Work it by hand
The message is meet at the old mill, 20 bytes. The first 16 bytes, meet at the old , are a full block. What is left is mill, 4 bytes.
The fill rule is PKCS#7 padding, as RFC 5652 section 6.3 states it for a block size k: the input is padded at the trailing end with k - (lth mod k) bytes, all having the value k - (lth mod k), where lth is the length of the input. Here k is 16 and lth is 20, so 20 mod 16 = 4, and 16 - 4 = 12 bytes are added, each with the value 12, which is 0c in hex.
The last block is mill followed by twelve 0c bytes. The padded message has 32 bytes: two blocks. SP 800-38A leaves padding out of its scope and discusses one method in its appendix A; the rule above is the one the command uses.
Run it
The command encrypts the 20-byte message in the data box with AES-128 in ECB mode (-aes-128-ecb). -K gives the 16-byte key as 32 hex digits, -nosalt asks for no salt header (with -K OpenSSL writes none either), -in names the message and -out the file that is read back as the answer.
The picture shows the message, the 32 bytes that were written and their count. The ciphertext looks random; nothing in it shows the padding.

Public test material, generated for this lesson. Never use it for anything real.
# Encrypts in/message.bin with AES-128 in ECB mode, result in out.bin.
# The key is public test material.
openssl enc -aes-128-ecb -K 2b7e151628aed2a6abf7158809cf4f3c -nosalt \
-in in/message.bin -out out.bin
{
"answer": "0fd0176af6818627c25a3802309a12538f33ec3e55bfd175017b89efb7ddaa62",
"file": "out.bin",
"bytes": 32
}You can change the message to any whole number of bytes, written as hex pairs, up to 1024 bytes; the empty message is accepted and also gives a ciphertext. A key with too few hex digits is filled up with zero bytes, one with too many is cut, and one with a character that is not a hex digit makes OpenSSL stop; the last section says more about the short key.
Your turn
The program below is meant to return how many bytes AES-128 writes for a plaintext of n bytes, in ECB or in CBC mode, with the padding of RFC 5652 section 6.3 and no header. Decide whether it does, and find the candidates that do. Check runs each candidate on the data shown and on eight others.

let blocks = (n + 15) / 16;
let size = blocks * 16;
let answer = size;
{
"answer": 48
}A plaintext of n bytes is encrypted with AES-128 in ECB or CBC mode (mode is ecb or cbc), padded as in RFC 5652 section 6.3 with block size 16: k - (n mod k) bytes, each with that value, are appended. Return the length of the ciphertext in bytes. OpenSSL writes no header. Check runs each candidate on the data shown and on eight others.
Read the result carefully
The program counts bytes; it does not run AES. The count is the length of what OpenSSL writes with -nosalt, so the file holds the encrypted blocks and nothing else.
OpenSSL fills a key or an IV that is shorter than the cipher needs with zero bytes, prints a warning and still exits with code 0, so a short key in a command is easy to miss. The key here is test material. This lesson does not show how to make a key or what an IV is.
Recap
- AES handles 16 bytes at a time; a message is cut into blocks.
- PKCS#7 adds k - (lth mod k) bytes, each with that value; with k = 16 that is 1 to 16 bytes.
- The demo message of 20 bytes becomes 32 bytes: one full block and one padded block.
- The padding is inside the ciphertext, so the ciphertext is longer than the message.