---
title: "Bacon's cipher"
description: "Five A or B per letter, no separators. The 26-letter table by default, Bacon's own 24-letter one with I/J and U/V shared on request."
canonical_url: "https://ciphers.agntn.dev/ciphers/bacon"
---
# Bacon's cipher

> Five A or B per letter, no separators. The 26-letter table by default, Bacon's own 24-letter one with I/J and U/V shared on request.

<cipher-facts name="bacon">



</cipher-facts>

Francis Bacon's cipher writes each letter as five symbols from a two-letter alphabet, originally to hide a message in the italics of an innocent text. His table has 24 letters, I and J on one code, U and V on another. The default here is the 26-letter version - A is `AAAAA`, B is `AAAAB`, Z is `BBAAB` - so the round trip is exact. `letters: 24` switches to Bacon's table. No separators in the output. Decoding reads any A and B, ignores everything else, takes them five at a time. When they don't come out even, it says so instead of dropping the rest. Text without a single A or B is an error too, not an empty answer.

```ts
const bacon = create("bacon");
bacon.encode("A").text; // "AAAAA"
bacon.encode("HI").text; // "AABBBABAAA"
bacon.decode("AABBB ABAAA").text; // "HI"
bacon.decode("AABBB ABAAA AB"); // CipherError: Invalid Bacon code: 12 letters A and B, not a multiple of five
bacon.encode("KNIGHT", { letters: 24 }).text; // "ABAABABBAAABAAAAABBAAABBBBAABA"
bacon.decode("ABAABABBAAABAAAAABBAAABBBBAABA", { letters: 24 }).text; // "KNIGHT"
bacon.decode("ABAABABBAAABAAAAABBAAABBBBAABA").text; // "JMIGHS", the same groups on the 26-letter table
```

The two tables agree from A to I and drift by one code from K on, by two from W, so a 24-letter puzzle decoded on the 26-letter table comes out wrong from the first K, not just at J and V. Wikipedia's example encodes `steganography` on the 24-letter table, `baaab baaba aabaa aabba aaaaa abbaa abbab aabba baaaa aaaaa abbba aabbb babba`, and that's the vector in the test suite. On the 24-letter table J encodes as I and V as U, and decoding gives back I and U. A group past the end of the table, `BBAAA` and up on 24 letters, `BBABA` and up on 26, decodes to `?`.

## Hiding in case

The message hides in how a sentence is typed, not what it says. It looks like someone fighting the shift key. That's the kind of hiding Bacon designed the cipher for.

Set `lowercase` to `A` or `B` to say what lowercase letters mean. Uppercase means the other symbol. The option value accepts either case. Without it, decoding reads literal A and B. There's no automatic detection. A wrong choice can produce nonsense without an error, so try both.

```ts
bacon.decode("heLLO, wOrld!", { lowercase: "A" }).text; // "HI", h e L L O is AABBB
bacon.decode("HEllo, WoRLD!", { lowercase: "B" }).text; // "HI", the same message with the case flipped
bacon.decode("heLLO, wOrld!", { lowercase: "B" }).text; // "YX", wrong polarity still decodes, to nonsense
bacon.decode("heLLO wOrl", { lowercase: "A" }); // CipherError: Invalid Bacon code: 9 A-Z letters, not a multiple of five
bacon.decode("héLLO wOrld", { lowercase: "A" }); // CipherError: Invalid Bacon code: é (U+00E9) is a letter outside A-Z, so its case isn't read
```

Only A-Z letters count. Spaces, digits, punctuation, combining marks and emoji are skipped. Other letters, an `é` say, are errors: a silent shift would be worse.

They have to come out in whole groups of five. Text without a single A-Z letter is an error. Blank text decodes to nothing. `letters: 24` works here too, and a group past the end of the table is still `?`.

Encoding refuses `lowercase`: there's no carrier text. On the CLI, `ciphers decode bacon "heLLO, wOrld!" --lowercase A` prints `HI`.


## Sitemap

See the full [sitemap](https://ciphers.agntn.dev/sitemap.md) for all pages.
