How CBC chains ciphertext into the next plaintext block

Short answer: Cipher block chaining xors each plaintext block with the previous ciphertext block, then applies the block cipher. The first block is xored with an initialization vector. The fragment uses AES-128, the SP 800-38A key 2b7e151628aed2a6abf7158809cf4f3c, the IV 000102030405060708090a0b0c0d0e0f, and one plaintext block. Decryption applies the inverse cipher and xors the previous ciphertext.


What the problem actually asks

ECB encrypts equal blocks to equal blocks. Chaining makes each ciphertext block depend on every plaintext block before it, so two equal plaintext blocks do not stay equal once their preceding ciphertext differs. The IV has to be unpredictable. A fixed IV makes the first block behave like ECB. CBC does not reject a modified ciphertext. A flipped bit in one ciphertext block randomizes that plaintext block after decryption and flips the matching bit in the next block. The fragment takes a whole number of blocks and does not define padding.


What the algorithm guarantees

With a fresh IV, the ciphertext of a repeated plaintext block changes. Decryption with the same IV and key recovers the plaintext when the ciphertext is intact. Integrity is not part of the guarantee. A receiver that accepts any decrypted bytes has not authenticated the message.


The working steps

  1. Choose a random IV of one block and send it with the ciphertext.
  2. Xor the first plaintext block with the IV and encrypt the result.
  3. Xor each later plaintext block with the previous ciphertext block and encrypt that.
  4. Decrypt by inverting the block cipher and xoring the previous ciphertext, with the IV standing in for the block before the first.

Approaches that look simpler and fail

Approach Why it fails when a modified ciphertext must be rejected
Reuse one IV under one key The first blocks are xored with the same value. Equal first blocks stay equal.
Derive the IV from the plaintext and call that authentication CBC still decrypts a modified ciphertext to some plaintext. A tag is what rejects it.
Pad by appending zeros and hope the length is obvious Ambiguous padding is not a padding scheme. The fragment does not pad.
Chain the blocks and skip the block cipher on the later blocks The xor alone is not the mode. Each chained block is encrypted.

GCM keeps a counter-mode encryption and adds a tag. CBC is the chaining mode.


A working example

This Python fragment encrypts the first NIST SP 800-38A plaintext block under CBC with AES-128. It prints 7649abac8119b246cee98e9b12e9197d.

def _xtime(a: int) -> int:
    return ((a << 1) ^ 0x1B) & 0xFF if a & 0x80 else (a << 1) & 0xFF

def _gmul(a: int, b: int) -> int:
    p = 0
    for _ in range(8):
        if b & 1:
            p ^= a
        a = _xtime(a)
        b >>= 1
    return p

def _sbox() -> list[int]:
    box = []
    for x in range(256):
        inv = 0 if x == 0 else next(i for i in range(1, 256) if _gmul(x, i) == 1)
        y = 0
        for i in range(8):
            bit = (inv >> i) ^ (inv >> ((i + 4) % 8)) ^ (inv >> ((i + 5) % 8))
            bit ^= (inv >> ((i + 6) % 8)) ^ (inv >> ((i + 7) % 8)) ^ (0x63 >> i)
            y |= (bit & 1) << i
        box.append(y)
    return box

_S = _sbox()

def aes128_encrypt(block: bytes, key: bytes) -> bytes:
    words = list(key)
    rcon = 1
    while len(words) < 176:
        t = words[-4:]
        if len(words) % 16 == 0:
            t = t[1:] + t[:1]
            t = [_S[b] for b in t]
            t[0] ^= rcon
            rcon = _xtime(rcon)
        earlier = words[-16:-12]
        words.extend(earlier[i] ^ t[i] for i in range(4))
    state = list(block)

    def add(rnd: int) -> None:
        rk = words[rnd * 16 : (rnd + 1) * 16]
        for i in range(16):
            state[i] ^= rk[i]

    def sub() -> None:
        for i in range(16):
            state[i] = _S[state[i]]

    def shift() -> None:
        for r in range(1, 4):
            row = [state[r + 4 * c] for c in range(4)]
            row = row[r:] + row[:r]
            for c in range(4):
                state[r + 4 * c] = row[c]

    def mix() -> None:
        for c in range(4):
            col = state[4 * c : 4 * c + 4]
            state[4 * c] = _gmul(col[0], 2) ^ _gmul(col[1], 3) ^ col[2] ^ col[3]
            state[4 * c + 1] = col[0] ^ _gmul(col[1], 2) ^ _gmul(col[2], 3) ^ col[3]
            state[4 * c + 2] = col[0] ^ col[1] ^ _gmul(col[2], 2) ^ _gmul(col[3], 3)
            state[4 * c + 3] = _gmul(col[0], 3) ^ col[1] ^ col[2] ^ _gmul(col[3], 2)

    add(0)
    for rnd in range(1, 10):
        sub()
        shift()
        mix()
        add(rnd)
    sub()
    shift()
    add(10)
    return bytes(state)

def aes_cbc_encrypt(key: bytes, iv: bytes, plaintext: bytes) -> bytes:
    if len(plaintext) % 16:
        raise ValueError("this example takes a whole number of blocks")
    out = b""
    prev = iv
    for off in range(0, len(plaintext), 16):
        block = bytes(p ^ v for p, v in zip(plaintext[off : off + 16], prev))
        prev = aes128_encrypt(block, key)
        out += prev
    return out

key = bytes.fromhex("2b7e151628aed2a6abf7158809cf4f3c")
iv = bytes.fromhex("000102030405060708090a0b0c0d0e0f")
block = bytes.fromhex("6bc1bee22e409f96e93d7e117393172a")
cipher = aes_cbc_encrypt(key, iv, block)
assert cipher.hex() == "7649abac8119b246cee98e9b12e9197d"
print(cipher.hex())

Where it is used

AES-CBC is a legacy TLS 1.2 cipher suite and still appears in some disk encryption. TLS 1.3 dropped CBC. A new channel uses an authenticated mode, usually GCM or ChaCha20-Poly1305, because CBC does not reject a modified ciphertext.


When you can drop this algorithm

Drop unauthenticated CBC for a message that an attacker can modify. Keep the chaining only together with a message authentication code, or use an authenticated mode.


Why this stays published

We publish this so a search for CBC returns the ciphertext feedback and the NIST block. CBC xors each plaintext block with the previous ciphertext, encrypts the result, and does not by itself reject a modified ciphertext.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *