{"id":486,"date":"2026-11-13T13:39:20","date_gmt":"2026-11-13T05:39:20","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-cbc-chains-ciphertext-into-the-next-plaintext-block\/"},"modified":"2026-11-13T21:58:49","modified_gmt":"2026-11-13T13:58:49","slug":"how-cbc-chains-ciphertext-into-the-next-plaintext-block","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-cbc-chains-ciphertext-into-the-next-plaintext-block\/","title":{"rendered":"How CBC chains ciphertext into the next plaintext block"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Cipher block chaining<\/strong> 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 <code>2b7e151628aed2a6abf7158809cf4f3c<\/code>, the IV <code>000102030405060708090a0b0c0d0e0f<\/code>, and one plaintext block. Decryption applies the inverse cipher and xors the previous ciphertext.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Choose a random IV of one block and send it with the ciphertext.<\/li>\n<li>Xor the first plaintext block with the IV and encrypt the result.<\/li>\n<li>Xor each later plaintext block with the previous ciphertext block and encrypt that.<\/li>\n<li>Decrypt by inverting the block cipher and xoring the previous ciphertext, with the IV standing in for the block before the first.<\/li>\n<\/ol>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when a modified ciphertext must be rejected<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Reuse one IV under one key<\/td>\n<td>The first blocks are xored with the same value. Equal first blocks stay equal.<\/td>\n<\/tr>\n<tr>\n<td>Derive the IV from the plaintext and call that authentication<\/td>\n<td>CBC still decrypts a modified ciphertext to some plaintext. A tag is what rejects it.<\/td>\n<\/tr>\n<tr>\n<td>Pad by appending zeros and hope the length is obvious<\/td>\n<td>Ambiguous padding is not a padding scheme. The fragment does not pad.<\/td>\n<\/tr>\n<tr>\n<td>Chain the blocks and skip the block cipher on the later blocks<\/td>\n<td>The xor alone is not the mode. Each chained block is encrypted.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">GCM keeps a counter-mode encryption and adds a tag. CBC is the chaining mode.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment encrypts the first NIST SP 800-38A plaintext block under CBC with AES-128. It prints <code>7649abac8119b246cee98e9b12e9197d<\/code>.<\/p>\n\n<pre><code class=\"language-python\">def _xtime(a: int) -&gt; int:\n    return ((a &lt;&lt; 1) ^ 0x1B) &amp; 0xFF if a &amp; 0x80 else (a &lt;&lt; 1) &amp; 0xFF\n\ndef _gmul(a: int, b: int) -&gt; int:\n    p = 0\n    for _ in range(8):\n        if b &amp; 1:\n            p ^= a\n        a = _xtime(a)\n        b &gt;&gt;= 1\n    return p\n\ndef _sbox() -&gt; list[int]:\n    box = []\n    for x in range(256):\n        inv = 0 if x == 0 else next(i for i in range(1, 256) if _gmul(x, i) == 1)\n        y = 0\n        for i in range(8):\n            bit = (inv &gt;&gt; i) ^ (inv &gt;&gt; ((i + 4) % 8)) ^ (inv &gt;&gt; ((i + 5) % 8))\n            bit ^= (inv &gt;&gt; ((i + 6) % 8)) ^ (inv &gt;&gt; ((i + 7) % 8)) ^ (0x63 &gt;&gt; i)\n            y |= (bit &amp; 1) &lt;&lt; i\n        box.append(y)\n    return box\n\n_S = _sbox()\n\ndef aes128_encrypt(block: bytes, key: bytes) -&gt; bytes:\n    words = list(key)\n    rcon = 1\n    while len(words) &lt; 176:\n        t = words[-4:]\n        if len(words) % 16 == 0:\n            t = t[1:] + t[:1]\n            t = [_S[b] for b in t]\n            t[0] ^= rcon\n            rcon = _xtime(rcon)\n        earlier = words[-16:-12]\n        words.extend(earlier[i] ^ t[i] for i in range(4))\n    state = list(block)\n\n    def add(rnd: int) -&gt; None:\n        rk = words[rnd * 16 : (rnd + 1) * 16]\n        for i in range(16):\n            state[i] ^= rk[i]\n\n    def sub() -&gt; None:\n        for i in range(16):\n            state[i] = _S[state[i]]\n\n    def shift() -&gt; None:\n        for r in range(1, 4):\n            row = [state[r + 4 * c] for c in range(4)]\n            row = row[r:] + row[:r]\n            for c in range(4):\n                state[r + 4 * c] = row[c]\n\n    def mix() -&gt; None:\n        for c in range(4):\n            col = state[4 * c : 4 * c + 4]\n            state[4 * c] = _gmul(col[0], 2) ^ _gmul(col[1], 3) ^ col[2] ^ col[3]\n            state[4 * c + 1] = col[0] ^ _gmul(col[1], 2) ^ _gmul(col[2], 3) ^ col[3]\n            state[4 * c + 2] = col[0] ^ col[1] ^ _gmul(col[2], 2) ^ _gmul(col[3], 3)\n            state[4 * c + 3] = _gmul(col[0], 3) ^ col[1] ^ col[2] ^ _gmul(col[3], 2)\n\n    add(0)\n    for rnd in range(1, 10):\n        sub()\n        shift()\n        mix()\n        add(rnd)\n    sub()\n    shift()\n    add(10)\n    return bytes(state)\n\ndef aes_cbc_encrypt(key: bytes, iv: bytes, plaintext: bytes) -&gt; bytes:\n    if len(plaintext) % 16:\n        raise ValueError(&quot;this example takes a whole number of blocks&quot;)\n    out = b&quot;&quot;\n    prev = iv\n    for off in range(0, len(plaintext), 16):\n        block = bytes(p ^ v for p, v in zip(plaintext[off : off + 16], prev))\n        prev = aes128_encrypt(block, key)\n        out += prev\n    return out\n\nkey = bytes.fromhex(&quot;2b7e151628aed2a6abf7158809cf4f3c&quot;)\niv = bytes.fromhex(&quot;000102030405060708090a0b0c0d0e0f&quot;)\nblock = bytes.fromhex(&quot;6bc1bee22e409f96e93d7e117393172a&quot;)\ncipher = aes_cbc_encrypt(key, iv, block)\nassert cipher.hex() == &quot;7649abac8119b246cee98e9b12e9197d&quot;\nprint(cipher.hex())\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">AES-CBC is a legacy <strong>TLS 1.2<\/strong> 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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for CBC returns the ciphertext feedback and the NIST block. <strong>CBC xors each plaintext block with the previous ciphertext, encrypts the result, and does not by itself reject a modified ciphertext.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,49],"tags":[],"class_list":["post-486","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-cryptography"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=486"}],"version-history":[{"count":1,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/486\/revisions"}],"predecessor-version":[{"id":698,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/486\/revisions\/698"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}