A tiny, dependency-free TypeScript implementation of the Bazeries cipher — Étienne Bazeries' (1846–1931) two-stage product cipher combining a digit-keyed transposition with a keyed substitution, all driven by a single secret number.
Powers the free, in-browser decoder at Text Machine → Bazeries cipher — paste a number key and ciphertext, decode instantly, nothing leaves your browser.
🔎 Not sure your ciphertext is even Bazeries? Run it through the Cipher Identifier first — it fingerprints unknown ciphertext (frequency, Index of Coincidence, χ² tests) and points you to the right decoder.
npm install bazeries-cipherOr just copy src/bazeries-cipher.ts into your project — it has zero dependencies.
import { encode, decode } from "bazeries-cipher";
encode("DCODE", "23"); // → "DLSLO" (the canonical test vector)
decode("DLSLO", "23"); // → "DCODE"
encode("WE HAVE TAKEN THE BRIDGE", "23");
// → "SCYTFPSSUTPGHSFMBSRL"
// Round-trips for any valid number key:
decode(encode("ATTACKATDAWN", "1947"), "1947"); // → "ATTACKATDAWN" (J folded to I)The key is a number (1…999999). Leading zeros are insignificant ("023" === 23).
A digit 0 in the key means a transposition group of length 10.
A single secret number drives both stages:
- Transposition. The plaintext is cut into runs whose lengths are the
successive digits of the number (cycled), and each run is reversed. With key
23the stream is split2, 3, 2, 3, …and every group flipped. - Substitution. Two 5×5 squares (I/J merged → 25 letters) sit side by side.
Square 1 is the plain alphabet written down the columns; square 2 is keyed
by the number spelled out in words (
23 → "TWENTYTHREE") and written across the rows. Each transposed letter is located in square 1 and replaced by the letter at the same cell of square 2.
The squares' opposite fill directions (square 1 by column, square 2 by row) are
intrinsic to the classic definition — they are exactly what make the canonical
vector DCODE / 23 → DLSLO come out right. Decoding reverses the two stages.
Non-letters are dropped and J is folded onto I, so the cipher operates on the 25-letter alphabet only (output is recovered minus spacing/punctuation).
| Export | Description |
|---|---|
encode(text, key?) |
Encipher plaintext (key defaults to "23"). |
decode(text, key?) |
Decipher ciphertext. |
applyBazeries(text, key, "encode" | "decode") |
The underlying engine. |
checkNumber(raw) / numberValue(raw) |
Validate / parse a key. |
spellNumber(n) |
The English spelling that keys square 2 (23 → "TWENTYTHREE"). |
buildGrid1() / buildGrid2(keyword) / gridRows(grid) |
Build / lay out the squares. |
npm testPins the canonical DCODE/23 → DLSLO vector, the longer
WEHAVETAKENTHEBRIDGE/23 → SCYTFPSSUTPGHSFMBSRL vector, and random round-trips
(including internal and leading-zero keys).
MIT — see LICENSE. Part of the Text Machine collection of free, privacy-first text tools (31 classical ciphers and counting).