Symmetric · 02

Block Ciphers and AES

A block cipher chops the plaintext into fixed-size pieces and scrambles each piece with the key. AES is the block cipher that won the world. The math behind it is dense, but the structure is surprisingly visual once you see it.

01

What a Block Cipher Actually Does

A block cipher is a function that takes a fixed-size chunk of plaintext and a key, and produces a fixed-size chunk of ciphertext of the same length. The chunk is called a block. For AES the block size is always 128 bits (16 bytes), regardless of the key length.

Plaintext is rarely a tidy 16 bytes long. Real data, files, passwords, packet payloads, comes in arbitrary lengths. The block cipher does not care. The data gets cut into 16-byte chunks. Each chunk is encrypted separately. The chunks are then reassembled in order to form the ciphertext.

Plaintext divided into 128-bit blocks A long plaintext message is shown being divided into four 16-byte blocks. The last block has a partial fill, leaving room for padding. Plaintext to Blocks "The quick brown fox jumps over the lazy dog and rests." arbitrary-length plaintext BLOCK 1 "The quick brown " 16 bytes BLOCK 2 "fox jumps over t" 16 bytes BLOCK 3 "he lazy dog and " 16 bytes BLOCK 4 "rests." + pad 6 bytes + 10 padding
Figure 2.1 Plaintext is sliced into 16-byte blocks. The last block, if short, is filled out with padding bytes covered on the next page.
AES VariantKey SizeBlock SizeRounds
AES-128128 bits (16 B)128 bits (16 B)10
AES-192192 bits (24 B)128 bits (16 B)12
AES-256256 bits (32 B)128 bits (16 B)14

The block size never changes. Only the key length and the number of rounds scale up. AES-256 is the recommended default for new systems.

02

A Brief History of AES

In the late 1990s, the U.S. government realized DES, the workhorse symmetric cipher of the previous quarter-century, was finished. Its 56-bit key was small enough that a custom $250,000 machine called Deep Crack could brute force it in days. NIST ran a five-year open competition. Cryptographers from around the world submitted candidate algorithms. Each candidate was published, attacked, and benchmarked in public.

In October 2000, NIST selected Rijndael, a cipher designed by Belgian cryptographers Vincent Rijmen and Joan Daemen. It was standardized as the Advanced Encryption Standard (AES) in November 2001 as FIPS Publication 197. It has been the dominant symmetric cipher ever since, embedded in TLS, BitLocker, FileVault, signal, WPA2/WPA3, IPSec, SSH, and on and on.

Two key takeaways from the AES selection process:

03

The State Matrix

Before AES operates on a block, it loads the 16 bytes into a 4-by-4 grid called the state. The state is the working canvas. Every operation in AES reads from the state, transforms it, and writes back to the state. After all the rounds complete, the state is read out as the ciphertext block.

The byte order is column-major: bytes 0, 1, 2, 3 fill the first column top to bottom, then bytes 4 through 7 fill the second column, and so on.

The AES state matrix A linear sequence of 16 bytes is loaded column-by-column into a 4 by 4 grid. From Linear Block to 4x4 State 16-byte block (linear order) b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 load column-by-column b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 r0 r1 r2 r3 c0 c1 c2 c3 Each cell is one byte. The grid is the "state" for the entire encryption.
Figure 2.2 The state matrix. Bytes 0-3 form column 0 top to bottom, bytes 4-7 form column 1, and so on.
04

The Round Structure

AES encrypts a block by running it through a sequence of identical rounds. Each round shuffles, substitutes, and mixes the bytes of the state, then XORs in a round-specific key. The number of rounds depends on the key size.

The first thing AES does to a fresh block is XOR it with the original key. That step is called the initial AddRoundKey. Then it runs Nr−1 main rounds, where Nr is 10, 12, or 14. Then it runs one final round that omits MixColumns (this asymmetry is mathematically necessary to make decryption invertible).

AES round structure A flowchart shows the AES round structure: an initial AddRoundKey, then Nr-1 main rounds containing SubBytes, ShiftRows, MixColumns, and AddRoundKey, then a final round without MixColumns. PLAINTEXT BLOCK AddRoundKey (with K0) whitening REPEAT Nr-1 TIMES (rounds 1 to Nr-1) SubBytes non-linear substitution ShiftRows permutation MixColumns linear mixing FINAL ROUND (Nr) SubBytes · ShiftRows · AddRoundKey (no MixColumns) CIPHERTEXT BLOCK + AddRoundKey at the bottom of each loop iteration
Figure 2.3 AES round structure. AES-128 runs 10 rounds, AES-192 runs 12, AES-256 runs 14. The final round drops MixColumns.

Each round operates on the entire state matrix simultaneously. The four operations are explained next.

05

SubBytes · Substitution Through the S-Box

SubBytes is a non-linear byte-by-byte substitution. Every one of the 16 bytes in the state is looked up in a fixed table called the S-box, and replaced with the corresponding output byte. The S-box is a 256-entry lookup: any input byte from 0x00 to 0xFF maps to exactly one output byte.

The S-box is designed to be highly non-linear. Small changes to the input byte produce wildly different output bytes. This is the source of AES's resistance to linear and differential cryptanalysis, which are the two most powerful general attacks against block ciphers.

SubBytes operation Each byte in the 4 by 4 state is replaced with its S-box substitution. SubBytes: Every Byte, Looked Up in the S-Box STATE (before) 19 a0 9a e9 3d f4 c6 f8 e3 e2 8d 48 be 2b 2a 08 S-Box 256-entry lookup STATE (after) d4 e0 b8 1e 27 bf b4 41 11 98 5d 52 ae f1 e5 30 Same positions. Each byte replaced individually. Non-linear, hard to invert without the table.
Figure 2.4 SubBytes. Every byte is looked up in the same S-box. The substitution destroys any easy mathematical relationship between input and output.
06

ShiftRows · Permuting the Rows

ShiftRows is a positional shuffle. The bytes do not change value. They just move to new positions. The first row stays put. The second row shifts left by one position (with wraparound). The third row shifts left by two. The fourth row shifts left by three.

The point of ShiftRows is to spread the bytes of each column across other columns. SubBytes only operates on bytes in isolation. Without ShiftRows, a column would stay self-contained for the entire encryption. ShiftRows breaks that isolation and gives the next operation, MixColumns, fresh material to mix together.

ShiftRows operation Each row of the state is cyclically left-shifted by 0, 1, 2, and 3 positions. ShiftRows: Cyclic Left Shifts BEFORE A0 A1 A2 A3 B0 B1 B2 B3 C0 C1 C2 C3 D0 D1 D2 D3 no shift shift 1 shift 2 shift 3 AFTER A0 A1 A2 A3 B1 B2 B3 B0 C2 C3 C0 C1 D3 D0 D1 D2 Bytes do not change. Only positions do. Each row wraps around when it shifts off the left edge.
Figure 2.5 ShiftRows. Row 0 stays put. Row 1 shifts left by 1. Row 2 by 2. Row 3 by 3. The shifts wrap around.
07

MixColumns · Diffusing Within Each Column

MixColumns is the diffusion step. It takes each of the four columns and treats it as a four-byte vector. The vector is multiplied by a fixed 4-by-4 matrix in a finite field called GF(2⁸). The result replaces the original column.

The exact math is dense (it uses operations from finite-field arithmetic, not regular multiplication), but the effect is what matters: every byte of the output column depends on every byte of the input column. Change one byte before MixColumns, and all four bytes of that column change after. Combined with ShiftRows from the previous step, a single bit flip in the plaintext block propagates to every byte of the state within two rounds. That is the avalanche effect that makes the cipher strong.

MixColumns operation Each column of the state is replaced by the result of multiplying it by a fixed matrix in GF(2^8). MixColumns: Each Column Mixed With Itself column in s0 s1 s2 s3 multiply by fixed matrix in GF(2^8) [ 02030101 01020301 01010203 03010102 ] × same in s0 s1 s2 s3 = column out s'0 s'1 s'2 s'3 Each output byte is a finite-field combination of all four input bytes. Repeat the operation for each of the four columns. Final round omits this step.
Figure 2.6 MixColumns. Each column becomes a mathematical blend of itself. Diffusion is achieved here.
08

AddRoundKey · Folding the Key In

AddRoundKey is where the key actually enters the picture. The current state is XORed byte-for-byte with the round key. XOR is its own inverse, which means the same operation undoes itself during decryption when the same key bytes are reapplied.

Each round uses a different round key, derived from the original key by the key schedule. The key schedule for AES-128 expands the 16-byte master key into 11 round keys (one for the initial AddRoundKey, plus one per round). For AES-256, it produces 15 round keys. Each round key is generated by a deterministic procedure that involves rotations, S-box substitutions, and XORs with round constants.

AddRoundKey operation The current state is XORed with the round key to produce the next state. AddRoundKey: XOR Byte-for-Byte STATE d4 e0 b8 1e bf b4 41 27 5d 52 11 98 30 ae f1 e5 ROUND KEY a0 fa fe 17 88 54 2c b1 23 a3 39 39 2a 6c 76 05 = NEW STATE 74 1a 46 09 37 e0 6d 96 7e f1 28 a1 1a c2 87 e0 Self-inverse: XORing with the same key bytes again restores the original.
Figure 2.7 AddRoundKey. The simplest of the four operations, but the only one that actually mixes the secret key into the state.
09

Why It Holds Up

The four operations were chosen to satisfy two classical cryptographic design principles, identified by Claude Shannon in 1949:

Run those four operations 10, 12, or 14 times with a properly derived key schedule, and the resulting ciphertext is, as far as decades of public analysis can tell, indistinguishable from random data without the key. The fastest known attack on AES-128 still requires roughly 2¹²⁶ operations, which is barely better than brute force, and the gap is purely theoretical. For all practical purposes, AES is not breakable by attacking the algorithm. Attackers who beat AES-encrypted systems do so by attacking the key (capturing it, guessing it, exfiltrating it from memory), not the cipher.

Takeaway

AES is not magic. It is the result of choosing four simple operations that satisfy confusion and diffusion, then iterating them enough times for the avalanche to dominate. Every operation is reversible. The same machinery, run in reverse with the same key schedule, undoes the encryption exactly.

10

What AES Cannot Do Alone

AES is a block cipher. It encrypts one 16-byte block at a time. It does not, by itself, tell you:

A block cipher is one tool, not a complete cryptographic system. Treat AES as the engine. The chassis, transmission, and brakes that turn the engine into a working vehicle are everything else in this track.