Skip to content
CryptographyInternals

How Does AES Encryption Work? A Step-by-Step Example

How AES encryption works, step by step: we follow one real 16-byte block through key expansion and all 10 rounds of AES-128, with every hex value shown.

Published on 10 min read

AES encryption works by putting 16 bytes of plaintext into a 4×4 grid of bytes called the state, mixing a secret key into it, and then running that grid through 10 rounds (for a 128-bit key) of four simple operations: SubBytes swaps every byte through a fixed non-linear table, ShiftRows rotates the rows, MixColumns blends the four bytes of each column together, and AddRoundKey XORs in a fresh round key derived from your key. None of these steps is secret or complicated on its own. Repeated ten times, they turn readable bytes into a block that looks like random noise to anyone who does not hold the key, yet can be reversed exactly by anyone who does.

That is the one-paragraph answer. The rest of this post makes it concrete: we take one real block, follow every byte through AES-128 and print the actual hex values at each stage, all computed with the same implementation that drives the animations on this site.

The block and the key we will follow

We encrypt the message Attack at dawn!! with the key my secret key 42. Both are exactly 16 characters of ASCII, so in UTF-8 they are exactly 16 bytes, one AES block and one AES-128 key. If you have scrolled the home page journey, you have already seen this pair.

plaintext  "Attack at dawn!!"  = 41 74 74 61 63 6b 20 61 74 20 64 61 77 6e 21 21
key        "my secret key 42"  = 6d 79 20 73 65 63 72 65 74 20 6b 65 79 20 34 32

AES does not read those bytes as a line. It fills the state column by column: the first four bytes go down column 0, the next four down column 1, and so on. Here is the plaintext as a state, and the key arranged the same way:

plaintext state        key (round key 0)
41 63 74 77            6d 65 74 79
74 6b 20 6e            79 63 20 20
74 20 64 21            20 72 6b 34
61 61 61 21            73 65 65 32

Read the left grid down the first column and you get 41 74 74 61, which is A t t a. This column-major layout matters, because two of the four operations work on rows and one works on columns.

Key expansion: one key becomes eleven

AES-128 needs 11 round keys of 16 bytes each: one before the rounds start and one at the end of each of the 10 rounds. The key schedule produces them from your key, four bytes (one "word") at a time. The first four words, w0 to w3, are the key itself. Every following word is the word just before it XORed with the word four positions back, except that every fourth word first goes through a twist.

Here is that twist for w4, the first word of round key 1:

StepBytes
Take w379 20 34 32
RotWord (rotate left one byte)20 34 32 79
SubWord (S-box on each byte)b7 18 23 b6
XOR round constant Rcon = 01b6 18 23 b6
XOR w0 = 6d 79 20 73db 61 03 c5 = w4

The round constant doubles at each round (01, 02, 04, ...) in GF(2^8), so no two round keys are built the same way, and the S-box puts non-linearity into the schedule itself. Running the recipe to the end gives:

round key  0  6d7920736563726574206b6579203432
round key  1  db6103c5be0271a0ca221ac5b3022ef7
round key  2  ae506ba810521a08da7000cd69722e3a
round key  3  ea61eb51fa33f1592043f1944931dfae
round key  4  25ff0f6adfccfe33ff8f0fa7b6bed009
round key  5  9b8f0e244443f017bbccffb00d722fb9
round key  6  fb9a58f3bfd9a8e404155754096778ed
round key  7  3e260df281ffa51685eaf2428c8d8aaf
round key  8  e358749662a7d180e74d23c26bc0a96d
round key  9  428b48e9202c9969c761baabaca113c6
round key 10  46f6fc7866da6511a1bbdfba0d1acc7c

Notice that round key 1 already shares nothing visible with the text my secret key 42.

Round 0: the key goes in first

Before any round, AES XORs the plaintext state with round key 0. Byte by byte: 41 ⊕ 6d = 2c, 74 ⊕ 79 = 0d, and so on.

after initial AddRoundKey
2c 06 00 0e
0d 08 00 4e
54 52 0f 15
12 04 04 13

Look at the zeros. t ⊕ t = 00, and our plaintext and key happen to share the letter t and a space at the same positions. On its own, XOR with a key hides nothing about structure. That is why AES does not stop here.

Round 1, one operation at a time

SubBytes: the only non-linear step

Every byte is replaced by its entry in the S-box, a fixed table of 256 values. The first byte 2c becomes 71. The three 00 bytes all become 63, since the S-box maps a given input to the same output every time.

after SubBytes
71 6f 63 ab
d7 30 63 2f
20 00 76 59
c9 f2 f2 7d

The S-box is built from inversion in GF(2^8) followed by an affine transform, chosen to be as far from any linear function as possible. Everything else in AES is linear; this table is where the cipher gets its confusion.

ShiftRows: bytes change columns

Row 0 stays put, row 1 rotates left by one, row 2 by two, row 3 by three:

after ShiftRows
71 6f 63 ab
30 63 2f d7
76 59 20 00
7d c9 f2 f2

No byte changes value. What changes is the neighborhood: each column now holds one byte from each of the four original columns. Column 0 is now 71 30 76 7d.

MixColumns: every byte touches its column

Each column is treated as a vector and multiplied by a fixed matrix in GF(2^8):

| 2 3 1 1 |
| 1 2 3 1 |
| 1 1 2 3 |
| 3 1 1 2 |

For the top byte of column 0 that means 2·71 ⊕ 3·30 ⊕ 76 ⊕ 7d. In GF(2^8), 2·71 = e2 and 3·30 = 50, so the result is e2 ⊕ 50 ⊕ 76 ⊕ 7d = b9. The full column 71 30 76 7d becomes b9 f6 2a 2f, and the whole state becomes:

after MixColumns
b9 eb 65 dd
f6 8b af ec
2a fe 01 71
2f 02 55 ce

The matrix is chosen so that changing a single input byte changes all four output bytes of the column. Combined with ShiftRows, this is what lets one byte reach the whole block within two rounds.

AddRoundKey: round key 1 goes in

Finally the state is XORed with round key 1:

round key 1            after round 1
db be ca b3            62 55 af 6e
61 02 22 02            97 89 8d ee
03 71 1a 2e            29 8f 1b 5f
c5 a0 c5 f7            ea a2 90 39

That is one complete round. The repeating zeros and 63s are gone; nothing in the state hints at the letters of Attack at dawn!! anymore.

Rounds 2 to 10, and the ciphertext

Rounds 2 through 9 repeat exactly the same four operations, each with its own round key. Round 10 does SubBytes, ShiftRows and AddRoundKey but skips MixColumns. Here is the state at the end of each round, written as 16 bytes in column order:

round  0  2c0d54120608520400000f040e4e1513
round  1  629729ea55898fa2af8d1b906eee5f39
round  2  ae5715615cd9ac90cf974ebfdc137fbb
round  3  a37441b3d0f0a61891b814f52aa123e5
round  4  9d3a16abc06319339e594dcf08eede5c
round  5  9848a84a12311dd0519d0c5fa8b4a9b2
round  6  ec67676ed114e39e83e804930b39f1c3
round  7  7024b4ef758be34bfebd00ec835879e5
round  8  bc0fae92a4d03b550a1d3d8e2dbd18e2
round  9  a7ffa03aa795e4259f396b7f66b14913
round 10  1adc83053ac85e917a733f853e0ca5ae

So the final ciphertext is:

1a 3a 7a 3e
dc c8 73 0c
83 5e 3f a5
05 91 85 ae

AES-128("Attack at dawn!!", key "my secret key 42") = 1adc83053ac85e917a733f853e0ca5ae

Why does the last round skip MixColumns? Because it would add nothing. MixColumns is linear, so placed right before the final AddRoundKey, an attacker could simply undo it. Leaving it out also makes decryption mirror encryption cleanly: decryption runs the inverse operations (InvShiftRows, InvSubBytes, InvMixColumns) in reverse order, with the round keys used from last to first.

If you want to see these 41 states move instead of reading them, scroll through this exact block on our AES page: the bytes animate through every operation while the round keys fall into the state.

Check it yourself

A worked example is worth little if you cannot reproduce it. Two quick checks:

  • Your own runtime. In Node.js, encrypting the block with aes-128-ecb and padding disabled (ECB with a single block is just the raw AES block function) returns 1adc83053ac85e917a733f853e0ca5ae, the same value as above.
  • The official test vector. FIPS-197, the standard that defines AES, gives an example in Appendix C.1: key 000102030405060708090a0b0c0d0e0f, plaintext 00112233445566778899aabbccddeeff, ciphertext 69c4e0d86a7b0430d8cdb78070b4c55a. The implementation behind this post and the site's animations reproduces it.

Why each step is there

Claude Shannon described the two properties a good cipher needs: confusion (the relationship between key and ciphertext should be as complex as possible) and diffusion (each input bit should influence many output bits). AES assigns them to separate steps:

StepJobWithout it
SubBytesConfusion: the only non-linear operationThe cipher is a linear system, solvable with algebra
ShiftRowsDiffusion across columnsThe four columns would be encrypted independently, like four 32-bit ciphers
MixColumnsDiffusion within a columnA changed byte would stay a changed byte
AddRoundKeyBrings in the keyEverything else is public, so anyone could invert it

The neat way to see diffusion at work is the avalanche effect. We flipped the lowest bit of the first plaintext byte (A = 41 becomes @ = 40, so the message reads @ttack at dawn!!), encrypted both versions with the same key, and counted differing bits in the state after each round. Then we did the same with one bit of the key (my secret key 42 becomes ly secret key 42).

After round012345678910
Plaintext bit flipped114676267666479635259
Key bit flipped116645857636870667258

Out of 128 bits, the difference goes from 1 to roughly 64 in two rounds, and stays there. Half the bits differing is exactly what you would expect between two unrelated random blocks, so the two ciphertexts (315797d713c5404b35711579dfc89980 for the plaintext flip, 3d46de852b64324f32a68bb726792be4 for the key flip) tell an observer nothing about how close their inputs were.

The breakdown inside round 2 shows who does the work. For the plaintext flip, 15 bits differed after SubBytes and ShiftRows, then 67 after MixColumns. SubBytes scrambles a single byte; MixColumns is the step that spreads it.

Diffusion is complete after two rounds, yet AES-128 uses ten. The rest is security margin. Reduced versions of AES are broken up to 7 rounds, and the best known attack on the full cipher, the biclique attack, costs about 2^126.1 operations, barely better than trying all 2^128 keys. To get a feel for how large that number is, watch your browser brute-force small keys live and see the timeline explode as the key grows.

What AES does not decide for you

Everything above encrypts exactly one 16-byte block. Real messages are longer, and how you chain blocks together is a separate choice called the mode of operation. This is where most real-world mistakes happen:

  • ECB (Electronic Codebook) encrypts every block independently with the same key. Identical plaintext blocks give identical ciphertext blocks, so patterns in the data leak straight through. Do not use it for real data.
  • GCM (Galois/Counter Mode) turns AES into a stream of keystream blocks driven by a counter and adds an authentication tag, so any tampering with the ciphertext is detected on decryption. It is one of the main cipher suites of TLS 1.3. Its one hard rule: never reuse a nonce with the same key. A repeated nonce is catastrophic.

Two more practical points:

  • AES-192 and AES-256 use the same round function with longer keys and more rounds (12 and 14). The block stays 128 bits.
  • Implementation leaks matter. Table-based software AES can leak timing through CPU caches. Modern processors ship AES-NI instructions that run in constant time, and every serious library uses them.

The example in this post is for learning. The code behind it keeps every intermediate state so the site can animate it, and it is not constant-time. For real data, use your platform's vetted library (WebCrypto, OpenSSL, libsodium or your language's standard crypto module) with an authenticated mode such as AES-GCM. Never roll your own.

Follow the block yourself

You now know every number in this block's journey: 16 bytes in, one XOR with the key, ten rounds of substitute, shift, mix and add, and 1adc83053ac85e917a733f853e0ca5ae out. To watch it rather than read it, open the interactive AES page, type your own message and key, and flip a single bit to watch the avalanche chart climb to about 64 in two rounds. When you are done, the rest of the modern cryptography section applies the same treatment to SHA-256, Diffie-Hellman and RSA.

Related articles

A Diffie-Hellman key exchange example with real numbers: p = 23, g = 5, secrets 6 and 15, shared key 2. See how it works and why Eve can't follow at 2048 bits.
At 10^21 guesses per second, brute-forcing AES-256 takes about 1.8 × 10^48 years. See the math, AES-128 vs AES-256, quantum, and what really breaks AES.
How SHA-256 works, step by step: we hash "Hello, world!" by hand, from padding and message schedule to one real round and the final 256-bit digest.