How Blowfish builds its state from the digits of pi

Short answer: Blowfish is a 16-round Feistel cipher on 8-byte blocks. Eighteen P-words and four S-boxes of 256 words start as the fractional hexadecimal digits of pi. The key is xored into the P-array, and the cipher then encrypts a running block to replace every P-word and every S-box word. The round function splits a word into four bytes and combines the four S-box entries by addition and xor. The all-zero key and the all-zero block encrypt to 4ef997456198dd78.


What the problem actually asks

A Feistel cipher needs a round function and a key schedule. Blowfish fills both from one idea: start from the digits of pi, fold the key into the P-array, and then use the cipher itself to replace the tables so the key affects every lookup. The digits are generated here by Machin’s formula, 16 arctan(1/5) - 4 arctan(1/239), so the fragment does not paste 1,042 constants. The first word is 0x243f6a88. The block is still 8 bytes.


What the algorithm guarantees

For a key whose length is a multiple of 4, the schedule produces one permutation of 8-byte blocks. The example key is 8 bytes. Decryption uses the same tables with the P-array applied in reverse. The 8-byte block, not the key length, bounds how much data one key should encrypt. The construction does not add a nonce or a tag.


The working steps

  1. Compute the fractional hex digits of pi and split them into the 18 P-words and the four S-boxes.
  2. Xor each P-word with the next four key bytes, wrapping through the key.
  3. Encrypt a running 8-byte block, starting from zero, and write each ciphertext back over the next two P-words and then over the S-boxes.
  4. Encrypt the message block with sixteen Feistel rounds, then swap the halves and apply the last two P-words.

Approaches that look simpler and fail

Approach Why it fails as the cipher for a long message
Paste a random-looking table that is not the digits of pi The published initial state is those digits. A different table is a different cipher.
Xor the key in and skip the self-encryption of the tables The key schedule is not finished until the running block has replaced the P-array and the S-boxes.
Encrypt a long message under one key because the key can be long The block is 8 bytes. The birthday bound is the block size.
Use the same key and no authenticator Blowfish is a block permutation. A modified ciphertext is not rejected.

AES uses a 16-byte block and a public S-box. Blowfish is the Feistel cipher whose tables start from pi.


A working example

This Python fragment derives the P-array and the S-boxes from pi, expands the all-zero key, and encrypts the all-zero block. It prints 4ef997456198dd78.

import struct

def _pi_words(count: int) -> list[int]:
    digits = count * 8 + 4

    def arctan_inv(x: int) -> int:
        scale = 16 ** digits
        term = scale // x
        total = term
        x2 = x * x
        k = 1
        while term:
            term //= x2
            piece = term // (2 * k + 1)
            total += -piece if k & 1 else piece
            k += 1
            if piece == 0:
                break
        return total

    scaled = 16 * arctan_inv(5) - 4 * arctan_inv(239)
    fractional = scaled % (16 ** digits)
    text = f"{fractional:x}".zfill(digits)[: count * 8]
    return [int(text[i : i + 8], 16) for i in range(0, count * 8, 8)]

def _blowfish_state(key: bytes) -> tuple[list[int], list[list[int]]]:
    words = _pi_words(18 + 4 * 256)
    p = words[:18]
    s = [words[18 + i * 256 : 18 + (i + 1) * 256] for i in range(4)]
    for i in range(18):
        chunk = key[(4 * i) % len(key) : (4 * i) % len(key) + 4]
        if len(chunk) < 4:
            chunk = (key + key)[(4 * i) % len(key) : (4 * i) % len(key) + 4]
        p[i] ^= int.from_bytes(chunk, "big")

    def encrypt_pair(left: int, right: int) -> tuple[int, int]:
        for i in range(16):
            left ^= p[i]
            a, b, c, d = left.to_bytes(4, "big")
            f = (s[0][a] + s[1][b]) & 0xFFFFFFFF
            f = (f ^ s[2][c]) & 0xFFFFFFFF
            f = (f + s[3][d]) & 0xFFFFFFFF
            right ^= f
            left, right = right, left
        left, right = right, left
        right ^= p[16]
        left ^= p[17]
        return left, right

    block = 0
    for i in range(0, 18, 2):
        left, right = encrypt_pair(block >> 32, block & 0xFFFFFFFF)
        p[i], p[i + 1] = left, right
        block = (left << 32) | right
    for box in s:
        for i in range(0, 256, 2):
            left, right = encrypt_pair(block >> 32, block & 0xFFFFFFFF)
            box[i], box[i + 1] = left, right
            block = (left << 32) | right
    return p, s

def blowfish_encrypt(block: bytes, key: bytes) -> bytes:
    p, s = _blowfish_state(key)
    left, right = struct.unpack(">II", block)
    for i in range(16):
        left ^= p[i]
        a, b, c, d = left.to_bytes(4, "big")
        f = (s[0][a] + s[1][b]) & 0xFFFFFFFF
        f = (f ^ s[2][c]) & 0xFFFFFFFF
        f = (f + s[3][d]) & 0xFFFFFFFF
        right ^= f
        left, right = right, left
    left, right = right, left
    right ^= p[16]
    left ^= p[17]
    return struct.pack(">II", left, right)

cipher = blowfish_encrypt(bytes(8), bytes(8))
assert cipher.hex() == "4ef997456198dd78"
print(cipher.hex())

Where it is used

Blowfish remains in legacy file and session ciphers. The surviving descendant is bcrypt, whose EksBlowfish setup is this Feistel network with a cost parameter. New designs use AES or ChaCha20.


When you can drop this algorithm

Drop Blowfish for a new message. The 8-byte block is the limit. Keep the schedule as the example of a key-dependent table built from a public constant.


Why this stays published

We publish this so a search for Blowfish returns the pi-digit tables and the all-zero ciphertext. Blowfish fills a 16-round Feistel cipher from the hex digits of pi, folds the key into that state, and encrypts 8-byte blocks.

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

Leave a Reply

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