Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Block Ciphers

Every cipher in the book so far has worked on one small piece of the message at a time. Caesar, affine and Vigenère change one letter at a time. The LFSR ciphers change one bit at a time. Change one letter or one bit of the plaintext, and exactly one letter or bit of the ciphertext changes.

Most encryption used today works differently. A block cipher takes a whole group of bits, called a block, and scrambles the block as a unit. Change one bit of the plaintext, and about half of the bits in the ciphertext block change. This chapter shows how block ciphers do that, from a 4-bit toy you can run by hand up to the Advanced Encryption Standard (AES), which protects most of the internet.

This page covers block ciphers in general: confusion, diffusion, rounds and Feistel networks.

In This Chapter

Stream Ciphers and Block Ciphers

All of the ciphers in this chapter are symmetric: the same key encrypts and decrypts. Symmetric ciphers come in two families.

In this course we also call the classical ciphers that work one letter at a time, like Caesar and Vigenère, stream ciphers. That’s a course convention. Many books save the name “stream cipher” for ciphers that combine the plaintext with a keystream of bits.

CipherBlock size
BabyBlock (this chapter)4 bits
Simplified AES (this chapter)16 bits
DES (1977)64 bits
AES (2001)128 bits

A message rarely fills a whole number of blocks. The sender adds extra bits, called padding, to fill the last block, and the padding must be easy for the receiver to recognize and remove. One common rule for byte-based ciphers, called PKCS #7 padding, adds nn bytes that each have the value nn. For example, a message that is 3 bytes short of a full block gets three bytes of 03 added to the end.

Confusion and Diffusion

In 1949 Claude Shannon published “Communication Theory of Secrecy Systems,” one of the first mathematical studies of cryptography. He named two properties that a strong cipher needs.

Substitution provides confusion. You’ve used it since the Caesar cipher.

None of the ciphers you’ve seen so far do this.

The goal for diffusion is this: change one symbol of the plaintext, and about half of the ciphertext symbols should change. A strong cipher needs both confusion and diffusion.

A Vigenère cipher has no diffusion

Encrypt TOLL with the keyword MATH, then change the first letter to P and encrypt again.

FOES
BOES

One plaintext letter changed, and only the matching ciphertext letter changed. Each ciphertext letter has a hidden relationship with its plaintext letter, so there’s confusion, but there’s no diffusion.

An XOR stream cipher has no diffusion

The same thing happens with bits. Encrypt 01010000 with the keystream 11011100, then flip the first plaintext bit and encrypt again.

10001100
00001100
ciphertext bits changed: 1 of 8

A block cipher has diffusion

Here is the same experiment with an 8-bit block cipher. The plaintext 00101011 encrypts to 10111001. Changing the third bit gives the plaintext 00001011, which encrypts to 01101100.

ciphertext bits changed: 5 of 8

One plaintext bit changed, and 5 of the 8 ciphertext bits changed. That’s close to half, which is the goal. When a small change in the input causes a large change in the output, people often call it the avalanche effect.

Why Diffusion Matters

Without diffusion, an attacker who knows what a message looks like can change it without knowing the key.

Suppose a bank sends the message PAY $100 with an XOR stream cipher. The keystream below comes from the 8-bit LFSR lfsr8 from the LFSR page. The attacker sees only the ciphertext. They know the message format, though, so they XOR the ciphertext with the difference between PAY $100 and PAY $900.

difference: 0000000000000000000000000000000000000000000010000000000000000000
the receiver decrypts: PAY $900

The characters 1 (00110001) and 9 (00111001) differ in one bit, so the attacker flipped a single ciphertext bit. The receiver decrypts PAY $900, and nothing looks wrong.

A block cipher with good diffusion stops this kind of precise change. Flipping one ciphertext bit changes about half the bits of the decrypted block, so the attacker can’t predict what the receiver will see. On the next pages you’ll measure this: BabyBlock changes about 2 of its 4 bits on average, and AES changes about 64 of its 128 bits. Diffusion alone doesn’t prove that a message wasn’t changed, though. The last section of the BabyBlock page describes modes that add a tag to catch tampering.

The Structure of a Block Cipher

Most block ciphers turn a block of plaintext into a block of ciphertext of the same size by repeating a step called the round function several times. Each repetition is a round. Each round usually has:

  • a substitution stage, such as an S-box (a lookup table), which provides confusion, and

  • a permutation or mixing stage, which moves or combines bits and provides diffusion.

Each round also uses a round key. The round keys are all made from the one secret key by a rule called the key schedule, so that every round is different.

There are two common designs. A substitution-permutation network puts the whole block through every step in every round. Simplified AES and AES work this way. A Feistel network changes only half of the block in each round. BabyBlock and DES work this way.

Feistel Networks

The Feistel network is named after Horst Feistel, who designed the Lucifer cipher at IBM in the early 1970s. Lucifer became the starting point for DES. Many other ciphers use the same plan, including the Soviet GOST cipher, Blowfish and Twofish.

Here is one round of a Feistel network.

The diagram shows the same round. The old right half is copied across to become the new left half, and the XOR result becomes the new right half. That’s the swap.

Then repeat for the next round with a new round key. For nn rounds with round keys k0,k1,…,kn−1k_0, k_1, \ldots, k_{n-1}, round ii is

Li+1=RiRi+1=Li⊕f(Ri,ki)\begin{aligned} L_{i+1} &= R_i \\ R_{i+1} &= L_i \oplus f(R_i, k_i) \end{aligned}

The function ff is where each cipher puts its confusion and diffusion, and every cipher has its own ff.

Decryption runs the same network backwards

Look at what this needs. To undo a round you compute ff forwards again, on a value you already have. You never have to undo ff. So ff doesn’t need to be invertible. It can be as messy as the designer likes, and the cipher can still be decrypted. That freedom is the big advantage of a Feistel network.

Most Feistel ciphers, including DES and BabyBlock, leave out the swap at the end of the last round. Then decrypting is exactly the same algorithm as encrypting, run with the round keys in reverse order.

A Feistel network in Python

The function below runs a Feistel network on two halves stored as integers. Each round XORs ff of the right half into the left half and swaps. At the end it undoes the last swap, so that the same function also decrypts.

To show that ff doesn’t need to be invertible, here’s a round function that is very far from invertible. It works on 4-bit halves (the numbers 0 to 15), and it only ever outputs 0, 1, 4 or 9, because those are the only squares mod 16.

[9, 0, 9, 4, 1, 0, 1, 4, 9, 0, 9, 4, 1, 0, 1, 4]

Many different inputs give the same output, so there’s no way to undo ff. The network still decrypts. Encrypt the 8-bit block 10110110 with three round keys, then run the same function on the ciphertext with the round keys reversed.

ciphertext: 0011 0110
decrypted:  1011 0110

The decrypted block is the original plaintext, even though ff itself can’t be undone.

Exercise for the Reader

What’s Next

The BabyBlock cipher is a 4-bit Feistel cipher with an S-box as its function ff. That page’s first two sections, How BabyBlock Works and Decrypting BabyBlock, only use ideas from this page, so you can read them right away.

Before BabyBlock, the next page looks at the ADFGVX cipher from World War I. It isn’t a block cipher, but it was one of the first ciphers to spread each letter across the ciphertext.