Block Ciphers

Work in progress. This note is still being written and incomplete.

A block cipher operates on a fixed-length block of plaintext mm of nn bits, mapping it to a ciphertext block cc of the same length under a secret key kk, both drawn from {0,1}n\{0,1\}^n.

Large messages are divided into blocks and encrypted per block, under a mode of operation.

ciek(mi),midk(ci)c_i \leftarrow e_k(m_i), \quad m_i \leftarrow d_k(c_i)

For a fixed key kk, the encryption function eke_k is a bijection on {0,1}n\{0,1\}^n, so it is invertible via dkd_k. Per Kerckhoffs’s principle, ee and dd are public. Only kk is secret.

A block cipher’s key design objective is to behave like a family of pseudorandom permutations (PRP) indexed by kk.

A block cipher encrypts a set of bits or characters at a time, whereas a stream cipher encrypts a single bit or character at a time.

Iterated Block Cipher Design

Round Design

An iterated block cipher builds eke_k by applying a simple keyed transformation, the round function, to the block rr times in succession.

  • Round
    One application of the round function. A single round is weak; security comes from repetition, and increasing rr raises security up to a stable level.
  • Round key kik_i
    The key used in round ii, derived from kk by the key schedule.
  • Key schedule
    The algorithm that expands kk into the round keys k1,...,krk_1,...,k_r.

Examples:

  • DES
    16 rounds, Feistel structure, 48-bit round keys from a 56-bit key.
  • AES
    10, 12, or 14 rounds for 128, 192, or 256-bit keys, SP-network structure.
  • Triple DES
    DES run three times with two or three independent keys.

Feistel Cipher

Named after Horst Feistel. Splits the plaintext block into halves (L0,R0)(L_0, R_0) and iterates a round function FF, keyed by kik_i, over rr rounds, swapping the final halves to produce the ciphertext.

Each encryption round:

LiRi1,RiLi1F(ki,Ri1)L_i \leftarrow R_{i-1}, \quad R_i \leftarrow L_{i-1} \oplus F(k_i, R_{i-1})

Each decryption round, using the round keys in reverse order:

Ri1Li,Li1RiF(ki,Li)R_{i-1} \leftarrow L_i, \quad L_{i-1} \leftarrow R_i \oplus F(k_i, L_i)

The round operation is invertible independent of FF, so the same hardware or software can perform both encryption and decryption by reversing the order of round keys.

Design parameters of a Feistel cipher:

  • Block length nn.
  • Number of rounds rr.
  • Definition of the round function FF.
  • Key schedule generating each round key kik_i from kk.
  • Length of the secret key kk.

You can find an implementation of a generic Feistel cipher in sahithyandev/ciphers.

SP-Network

Proposed by Claude Shannon in 1945: a series of linked permutation and substitution operations. AES, the successor to DES, is based on this design, rather than the Feistel structure. Its round operations are visualized in the interactive guide to AES.

  • Diffusion
    Permutations disperse the statistical characteristics of each input bit among all output bits.
  • Confusion
    Substitutions create a complex relationship between the input and key with the output, providing the cipher’s non-linearity.

Modes of Operation

A mode of operation is a recommended standard for using a block cipher to build a scheme for arbitrary-length messages. Plaintext mm is divided into tt blocks m1,...,mtm_1,...,m_t of the nn-bit block length the underlying cipher specifies, padding the last block if needed.

ECB, CBC, OFB, and CFB were standardized together in FIPS 81 (1980). CTR was proposed by Diffie and Hellman in 1979 but only standardized in 2001 (NIST SP 800-38A), adding a mode built for parallel encryption of data blocks.

  • ECB, for short secrets such as session keys.
  • CBC, for bulk data encryption.
  • OFB, for real-time streamed content.
  • CFB, for streamed content.
  • CTR, for data that must be encrypted or decrypted in parallel.

The same key is used for all blocks, in all the modes.

ECB Mode

Aka. Electronic Code Book. The simplest mode, where each block is encrypted independently.

ciek(mi),midk(ci)c_i \leftarrow e_k(m_i), \quad m_i \leftarrow d_k(c_i) ENCRYPTION m₁ Eₖ c₁ m₂ Eₖ c₂ DECRYPTION c₁ Dₖ m₁ c₂ Dₖ m₂

The last block is padded (such as PKCS#7) to a multiple of nn.

Blocks are independent.

Allows parallel encryption and decryption. Error in a single block does not affect the other blocks. No IV or nonce to generate, transmit, or accidentally reuse.

Identical plaintext blocks (mi=mjm_i = m_j) produce identical ciphertext blocks (ci=cjc_i = c_j), letting an attacker spot repetition. Attacker can remove or copy-paste blocks in transit without detection.

CBC Mode

Aka. Cipher Block Chaining. Improves ECB by chaining ciphertext into each block’s encryption, giving every block context from its neighbors.

c1ek(m1IV),ciek(mici1) for i>1c_1 \leftarrow e_k(m_1 \oplus IV), \quad c_i \leftarrow e_k(m_i \oplus c_{i-1}) \text{ for } i > 1 m1dk(c1)IV,midk(ci)ci1 for i>1m_1 \leftarrow d_k(c_1) \oplus IV, \quad m_i \leftarrow d_k(c_i) \oplus c_{i-1} \text{ for } i > 1 ENCRYPTION m₁ IV Eₖ c₁ m₂ Eₖ c₂ DECRYPTION c₁ Dₖ IV m₁ c₂ Dₖ m₂

The last block is padded (such as PKCS#7) to a multiple of nn.

The IV can be a fixed public value (e.g. all-zero), which need not be transmitted, or a random nonce, which must be transmitted with the ciphertext as IV,c1,...,ctIV, c_1,...,c_t.

Allows parallel decryption as all ciphertext blocks are known initially. Identical plaintext blocks (mi=mjm_i = m_j) produce different ciphertext blocks (cicjc_i \neq c_j) (because of the IV and chaining), so an attacker cannot spot repetition. A fixed all-zero IV needs no transmission.

A single bit error in a received ciphertext block corrupts the entire corresponding plaintext block, and a single bit in the next block. This is one-block error propagation. Encryption is strictly sequential. A random IV must be unpredictable and adds one block of transmission overhead. A bit inserted or deleted in transit, rather than flipped, desynchronizes every following block.

OFB Mode

Aka. Output Feedback. Allows a block cipher operate as a stream cipher. An nn-bit shift register SiS_i feeds the cipher. Each step encrypts the register, takes the bb most significant bits of the output as the keystream segment ZiZ_i, and XORs it with the bb-bit plaintext or ciphertext segment. The segment size satisfies 1bn1 \le b \le n, with b=nb = n the full-block case.

ZiMSBb(ek(Si)),cimiZi,miciZiZ_i \leftarrow \operatorname{MSB}_b(e_k(S_i)), \quad c_i \leftarrow m_i \oplus Z_i, \quad m_i \leftarrow c_i \oplus Z_i S1IV,Si+1(Sib)ZiS_1 \leftarrow IV, \quad S_{i+1} \leftarrow (S_i \ll b) \mathbin{|} Z_i

MSBb\operatorname{MSB}_b takes the bb most significant bits. Si+1S_{i+1} shifts the register left by bb bits, discarding the top bb, and feeds ZiZ_i into the low end. With full-block feedback (b=nb = n) this reduces to Si+1ek(Si)S_{i+1} \leftarrow e_k(S_i). Both encryption and decryption use only eke_k.

ENCRYPTION SHIFT REGISTER S IV Eₖ MSB b Z₁ ≪ b m₁ c₁ DECRYPTION SHIFT REGISTER S IV Eₖ MSB b Z₁ ≪ b c₁ m₁

No padding is needed, since the keystream is truncated to the plaintext length.

The keystream can be precomputed before any ciphertext arrives, so OFB suits real-time streamed content. The ciphertext has the same length as the plaintext. Same hardware used for both encryption and decryption.

Encryption and decryption are strictly sequential.

A single bit error in a received ciphertext block corrupts only the corresponding single bit of plaintext. Remaining bits of the current block are not affected. There is no propagation into other blocks as well. The absence of error propagation also makes the ciphertext malleable. An attacker can flip any plaintext bit by flipping the matching ciphertext bit, undetected without a separate integrity check.

Because the keystream depends only on the key and IV, reusing a (k,IV)(k, IV) pair is catastrophic. XORing the two ciphertexts cancels the keystream and leaks the XOR of the plaintexts.

CFB Mode

Aka. Cipher Feedback. Also lets a block cipher operate as a stream cipher, using the same nn-bit shift register SiS_i as OFB, but shifts the ciphertext segment, rather than the keystream, into the register.

ZiMSBb(ek(Si)),cimiZi,miciZiZ_i \leftarrow \operatorname{MSB}_b(e_k(S_i)), \quad c_i \leftarrow m_i \oplus Z_i, \quad m_i \leftarrow c_i \oplus Z_i S1IV,Si+1(Sib)ciS_1 \leftarrow IV, \quad S_{i+1} \leftarrow (S_i \ll b) \mathbin{|} c_i ENCRYPTION SHIFT REGISTER S IV Eₖ MSB b Z₁ m₁ c₁ ≪ b DECRYPTION SHIFT REGISTER S IV Eₖ MSB b Z₁ c₁ ≪ b m₁

No padding is needed, since the keystream is truncated to the plaintext length.

Decryption can be parallel. Same hardware used for both encryption and decryption.

Encryption is strictly sequential. The keystream depends on the ciphertext and cannot be precomputed before ciphertext arrives. Less useful than OFB for real-time streaming.

A single bit error in a received ciphertext block corrupts a single bit in the corresponding plaintext block, then keeps corrupting the shift register SiS_i until it is flushed after n/bn/b rounds, completely corrupting the next n/bn/b blocks. This causes severe error propagation.

CTR Mode

Aka. Counter mode. Similar to ECB but removes its disadvantages while keeping its advantage of parallel processing.

cimiek(IVin),miciek(IVin)c_i \leftarrow m_i \oplus e_k(IV \oplus \langle i \rangle_n), \quad m_i \leftarrow c_i \oplus e_k(IV \oplus \langle i \rangle_n)

in\langle i \rangle_n is the binary representation of ii as an nn-bit number, matching the block length.

ENCRYPTION IV ⊕ ⟨1⟩ Eₖ m₁ c₁ IV ⊕ ⟨2⟩ Eₖ m₂ c₂ DECRYPTION IV ⊕ ⟨1⟩ Eₖ c₁ m₁ IV ⊕ ⟨2⟩ Eₖ c₂ m₂

No padding is needed, since the keystream is truncated to the plaintext length.

Same hardware used for both encryption and decryption.

Each plaintext block is encrypted independently, like ECB, so blocks can be processed in parallel. The counter also gives each block context, so unlike ECB, an attacker cannot cut-and-paste or rearrange ciphertext blocks undetected.

The counter input IViIV \oplus i must never be reused for a subsequent encryption under the same key kk.

Written by September 16, 2026 8 min read
Was this helpful?