Skip to main content
Confusion, Diffusion, Feistel Networks & S-Boxes
Lesson 4 of 19

Confusion, Diffusion, Feistel Networks & S-Boxes

Understand Shannon's confusion and diffusion principles, trace how Feistel networks invert any round function, and inspect S-box properties in Python.

Intermediate 26 min Cryptography

Learning objectives

  • Explain Shannon's confusion and diffusion principles and why classical ciphers fail them
  • Trace encryption and decryption through a multi-round Feistel network
  • Explain why a Feistel round function does not need to be invertible
  • Evaluate S-box nonlinearity and avalanche behaviour in Python

Recommended first: Introduction to Cryptography & Kerckhoffs's PrincipleMath Foundations: Modular Arithmetic, GCD & CongruencesClassical Ciphers: Caesar, Vigenere, XOR & Frequency Analysis

#Why this matters

Classical ciphers such as Caesar, Vigenere and repeating-key XOR break easily because each ciphertext symbol depends on a single plaintext symbol and a single key byte through a simple rule. Letter frequencies and repeating patterns leak straight through.

Modern symmetric block ciphers solve this by iterating two principles introduced by Claude Shannon in 1949: confusion and diffusion. Understanding how Feistel networks and S-boxes build those properties lets you audit custom cryptography and solve kriptografiya block-cipher challenges.

#Confusion and diffusion

Shannon defined two complementary properties for strong encryption:

  • Confusion obscures the algebraic link between key and ciphertext. Changing one key bit should change the ciphertext unpredictably.
  • Diffusion spreads every plaintext bit across many ciphertext bits. Flipping one input bit should flip roughly half of the output bits, known as the avalanche effect.
Principle What it hides Building block Failure mode if missing
Confusion Key-to-ciphertext link Nonlinear S-boxes Linear algebra solves for key bits
Diffusion Plaintext patterns Bit mixing, swaps Plaintext changes stay local

Confusion without diffusion preserves duplicate patterns across blocks. Diffusion without confusion leaves a linear system of bit equations that known plaintext-ciphertext pairs quickly break. Strong block ciphers alternate both across multiple rounds.

#How a Feistel network works

Block ciphers commonly follow one of two layouts:

  • Substitution-Permutation Network (SPN): Transforms the full block in parallel (used in AES, NIST FIPS 197). Every layer must be invertible for decryption.
  • Feistel network: Splits an n-bit block into left L and right R halves of n/2 bits. Made famous by DES (now deprecated due to its 56-bit key), this structure still powers Camellia, Blowfish, Twofish and NIST format-preserving encryption (FF1/FF3-1).

In round i with round function F and subkey K_i, encryption computes L_{i+1} = R_i and R_{i+1} = L_i XOR F(R_i, K_i).

One Feistel encryption roundOne Feistel encryption roundRightHalfRoundFunctionLeftHalfpass R_i and subkey K_ireturn F(R_i, K_i)R_{i+1} = L_i XOR F(R_i, K_i)swap so L_{i+1} = R_i

Notice the key property: F does not need to be invertible. To undo round i from (L_{i+1}, R_{i+1}), the receiver already holds R_i unmodified inside L_{i+1}. Computing F(L_{i+1}, K_i) reproduces the exact mask F(R_i, K_i), and XORing it with R_{i+1} cancels the mask (X XOR Y XOR Y = X) to recover L_i:

text
R_i = L_{i+1}
L_i = R_{i+1} XOR F(L_{i+1}, K_i)

Decryption uses the identical F function with subkeys in reverse order, even when F discards bits or is many-to-one.

#S-boxes and nonlinearity

Inside F, a substitution box (S-box) is a lookup table mapping m input bits to n output bits. Its main job is nonlinearity: breaking rules like S(a XOR b) = S(a) XOR S(b).

If an S-box is linear or affine (such as S(x) = x XOR 0x5), multiple rounds of XORs and swaps collapse into one linear bit-matrix equation, allowing recovery of the key via Gaussian elimination. Good S-boxes target three criteria:

  1. Nonlinearity: Distance from all affine boolean functions, resisting linear cryptanalysis.
  2. Low differential uniformity: Input differences spread evenly across output differences, resisting differential cryptanalysis.
  3. Bijectivity (SPN only): SPN S-boxes must be one-to-one so they can be inverted; Feistel S-boxes need not be.

#Tracing a toy Feistel cipher in Python

This script runs an 8-bit, 4-round Feistel cipher over 4-bit nibbles using a 4-bit S-box:

python
SBOX = [0xE, 0x4, 0xD, 0x1, 0x2, 0xF, 0xB, 0x8,
        0x3, 0xA, 0x6, 0xC, 0x5, 0x9, 0x0, 0x7]

def round_f(right, subkey):
    return SBOX[right ^ subkey]

def feistel_encrypt(block, subkeys):
    left, right = (block >> 4) & 0xF, block & 0xF
    for idx, k in enumerate(subkeys, 1):
        left, right = right, left ^ round_f(right, k)
        print(f"enc round {idx}: L=0x{left:x} R=0x{right:x}")
    return (left << 4) | right

def feistel_decrypt(block, subkeys):
    left, right = (block >> 4) & 0xF, block & 0xF
    for k in reversed(subkeys):
        left, right = right ^ round_f(left, k), left
    return (left << 4) | right

keys = [0x3, 0xA, 0x7, 0xC]
ct = feistel_encrypt(0x59, keys)
ct2 = feistel_encrypt(0x58, keys)
diff = ct ^ ct2
print(f"0x59 -> 0x{ct:02x}, dec -> 0x{feistel_decrypt(ct, keys):02x}")
print(f"0x58 -> 0x{ct2:02x}, diff=0x{diff:02x} ({diff:08b}), flipped={diff.bit_count()}")
Running the toy cipher and checking a 1-bit flip
$ python3 feistel_demo.py
enc round 1: L=0x9 R=0x3
enc round 2: L=0x3 R=0x3
enc round 3: L=0x3 R=0x1
enc round 4: L=0x1 R=0xa
0x59 -> 0x1a, dec -> 0x59
0x58 -> 0x96, diff=0x8c (10001100), flipped=3

Flipping one bit (0x59 to 0x58) changes both ciphertext halves (0x1a to 0x96).

#Common mistakes

  • Linear or affine S-boxes: Bitwise XORs, shifts and constants alone collapse across rounds.
  • Too few rounds: One or two rounds expose halves directly to differential analysis; real ciphers use 16 or more rounds depending on F.
  • Static subkeys: Reusing one subkey in every round invites slide attacks.
  • Custom production crypto: Use audited standards such as AES-GCM or ChaCha20-Poly1305 instead of homegrown ciphers.

#Summary

  • Confusion hides the key-ciphertext link via nonlinear S-boxes; diffusion spreads bit changes across the block.
  • Feistel networks XOR one half with F(other_half, K_i), so decryption only reverses subkey order and never inverts F.
  • S-boxes supply nonlinearity and differential resistance, without which multi-round ciphers reduce to linear equations.
  • Prototype toy ciphers in Python to study mechanics, and rely on vetted standards in production.

Knowledge check

Sign in to take the test, complete lessons and save your progress. Log in

Practice: test your knowledge