Lesson 6 of 15

Feistel Network

Feistel Network

A Feistel network (Horst Feistel, IBM, 1973) is a symmetric structure used in many block ciphers including DES. Its key insight: you can build an invertible cipher from a non-invertible round function.

One Round

Split the block into two halves LL and RR. Apply the round function FF with round key KK:

L=RL' = R R=LF(R,K)R' = L \oplus F(R, K)

The structure is automatically invertible — to reverse a round: R=LR = L' L=RF(L,K)L = R' \oplus F(L', K)

Multi-round Encryption

Repeat the round with different subkeys K1,K2,,KnK_1, K_2, \ldots, K_n. More rounds → more diffusion and confusion.

Our Simulation

We use 4-bit halves (from an 8-bit block: L=byte>>4L = \text{byte} >> 4, R=byte&0xFR = \text{byte} \& 0xF) with round function:

F(R,K)=(R+K)mod256F(R, K) = (R + K) \bmod 256

Combined in the round: L=R,R=L(R+K)mod256L' = R, \quad R' = L \oplus (R + K) \bmod 256

DES

DES uses a 64-bit block with 16 Feistel rounds and 56-bit keys. It was the US federal standard from 1977 to 2001, superseded by AES.

Your Task

Implement:

  • feistel_round(L, R, K)(new_L, new_R) where new_L = R, new_R = L XOR (R+K)%256
  • feistel_encrypt(plaintext, keys) — apply rounds with each key in sequence
  • feistel_decrypt(ciphertext, keys) — apply rounds in reverse order using inverse round
Python runtime loading...
Loading...
Click "Run" to execute your code.