Bacon Cipher Encoder and Decoder

Decode
Encode
const message = "hello world";
const aChar = "a";
const bChar = "b";

const alphabetIndex = "A".charCodeAt(0);
const table = [...Array(26).keys()].reduce((map, v) => {
  map[String.fromCharCode(alphabetIndex + v)] = v
    .toString(2)
    .padStart(5, "0")
    .replaceAll("0", aChar)
    .replaceAll("1", bChar);
  return map;
}, {});

const ciphered = [...message.toUpperCase()]
  .map((v) => table[v] ?? "")
  .filter(Boolean)
  .join(" ");

console.log(`Bacon cipher of "${message}": "${ciphered}"`);

The Bacon cipher

The Bacon cipher was devised in 1605 by Francis Bacon. It is a method of steganographic message encoding, where the true message is hidden inside a seemingly innocuous other message.

Each letter in the Latin alphabet is assigned a five digit binary string, in which each digit corresponds to either an 'a' or 'b'. There are two versions of the Bacon cipher: the original—in which the letters I, J, U, and V do not have unique binary assignments—and the unique variant, in which every letter is assigned a unique binary string.

The code examples above use the unique bacon table.

Original Bacon Table

LetterBinaryCodeLetterBinaryCode
A00000aaaaaN01100abbaa
B00001aaaabO01101abbab
C00010aaabaP01110abbba
D00011aaabbQ01111abbbb
E00100aabaaR10000baaaa
F00101aababS10001baaab
G00110aabbaT10010baaba
H00111aabbbU10011baabb
I01000abaaaV10011baabb
J01000abaaaW10100babaa
K01001abaabX10101babab
L01010ababaY10110babba
M01011ababbZ10111babbb

Unique Bacon Table

LetterBinaryCodeLetterBinaryCode
A00000aaaaaN01101abbab
B00001aaaabO01110abbba
C00010aaabaP01111abbbb
D00011aaabbQ10000baaaa
E00100aabaaR10001baaab
F00101aababS10010baaba
G00110aabbaT10011baabb
H00111aabbbU10100baabb
I01000abaaaV10101babab
J01001abaabW10110babba
K01010ababaX10111babbb
L01011ababbY11000bbaaa
M01100abbaaZ11001bbaab