## Beale’s Cipher

### December 2, 2016

In the early 1800, Thomas J. Beale mined a large quantity of gold and silver, secretly, some place in the American West, and brought the gold, silver, and some jewels purchased with the treasur to Virginia, where he buried it. He wrote three coded documents that described the location of the buried treasure, the nature of the treasure, and the names of the owners. He never came back to retrieve the treasure. Only the second of those documents has been decoded, and many people, even today, are scouring Bedford County, Virginia, looking for buried treasure. Or so the story goes.

Beale used a variant of a book cipher. He chose a long text as a key, numbered each of the words in the text sequentially, starting from 1, and formed a message by choosing from the key text a word for each character of plain text having the same initial letter as the plain text; the cipher text consists of a list of the sequence numbers of the chosen words. For instance, if the key text is “now is the time” and the plain text is “tin”, then either (3 2 1) or (4 2 1) are valid encipherments. If the key text is long, there will be many duplicates, as we saw with the letter “t”, and the resulting cipher will be reasonably secure. Beale used the 1322 words of the Declaration of Independence as the key text for the second document; the key texts associated with the first and third documents are unknown.

Your task is to write programs that encipher and decipher messages using the Beale cipher; use it to decode the second document. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2 3

### 4 Responses to “Beale’s Cipher”

1. KCR said

This was my 2nd ever programming course assignment, and the one I enjoyed programming the most. Thanks for posting this.

2. Paul said

In Python. The Beale cipher is apparently so complicated to use, that Beale (probably) messed up the encoding. It is, of course, a daunting task to set up the encoding with a document of 1322 words without a computer. It is also not really known which version of the Declaration of Independence he used.

```def read_decl(list_of_words):
"""create encode and decode dictionaries
the input is a sequence of lowercase words
"""
E, D = defaultdict(list), {}
for n, word in enumerate(list_of_words, 1):
E[word[0]].append(n)
D[n] = word[0]
return E, D

def decode(code, D):
'input is list of ints - output is string'
return "".join(D.get(n, "?") for n in code)

def encode(txt, E):
'input is string - output is list of ints'
txt = txt.lower()
return [choice(E.get(c, [0])) for c in txt]

print(decode((int(n) for n in open(LETTER).read().split(", ")), D))
```
3. matthew said

Sounds like a good excuse to play around with Unicode and ES6 a little more. ES6 has a number of features that make proper Unicode handling rather easier than in previous versions of Javascript. Notably, strings now support the new iterator protocol that allows us to easily convert strings to arrays of single Unicode characters rather than having to deal with surrogate pairs. For testing this, we will use texts in the Gothic script which uses the astral Unicode codepoints U+10330 to U+1034A, and seems to be reasonably well supported by fonts and browsers. Extant Gothic scripts are mainly religious, but there is one poem in Gothic, “Bagme Bloma”, written by J. R. R. Tolkein. Here we encrypt that poem, using as key text the Gothic version of the Lord’s Prayer. I couldn’t find the texts already in the Gothic script, so we start by converting from latin transliterations.

```"use strict"

// The Lord's Prayer in Gothic, transliterated.
const text1 = [
"atta unsar þu in himinam",
"weihnai namo þein",
"qimai þiudinassus þeins",
"wairþai wilja þeins",
"swe in himina jah ana airþai",
"hlaif unsarana þana sinteinan gif uns himma daga",
"jah aflet uns þatei skulans sijaima",
"swaswe jah weis afletam þaim skulam unsaraim",
"jah ni briggais uns in fraistubnjai",
"ak lausei uns af þamma ubilin",
"unte þeina ist þiudangardi jah mahts",
"jah wulþus in aiwins"
];

// Tolkein's Bagme Bloma poem
const text2 = [
"brunaim bairiþ bairka bogum",
"laubans liubans liudandei",
"gilwagroni glitmunjandei",
"bagme bloma blauandei",
"fagrafahsa liþulinþi",
"fraujinondei fairguni",

"wopjand windos wagjand lindos",
"lutiþ limam laikandei",
"slaihta raihta hweitarinda",
"razda rodeiþ reirandei",
"bandwa bairhta runa goda",
"þiuda meina þiuþjandei",

"andanahti milhmam neipiþ",
"liuhteiþ liuhmam lauhmuni",
"laubos liubai fliugand lausai",
"tulgus triggwa standandei",
"bairka baza beidiþ blaika",
"fraujinondei fairguni"
];

// Gothic alphabet and the standard latin transliteration
const gothic = "𐌰𐌱𐌲𐌳𐌴𐌵𐌶𐌷𐌸𐌹𐌺𐌻𐌼𐌽𐌾𐌿𐍀𐍁𐍂𐍃𐍄𐍅𐍆𐍇𐍈𐍉𐍊";
const latin = "abgdeqzhþiklmnjup*rstwfxƕo^";
const gchars = [...gothic] // iterator respects codepoints
const lchars = [...latin]
const charmap = new Map(lchars.map((c,i)=>[c,gchars[i]])) // zip
const convert = s => [...s].map(c=>charmap.get(c)||c).join("")
const keytext = text1.map(convert)

// Now construct the encoding tables
const encoder = new Map()
const decoder = new Map([[0,"."]]) // Unknown character

const allwords = [].concat(...keytext.map(s=>s.split(" ")))
allwords.forEach((w,i) => {
const index = i+1
const c = String.fromCodePoint(w.codePointAt(0))
if (!encoder.has(c)) encoder.set(c,[])
encoder.get(c).push(index)
decoder.set(index,c)
})

const encodeone = c => {
const a = encoder.get(c)
if (a == undefined) return 0
else return a[Math.floor(Math.random() * a.length)]
}
const encode = s => [...s].map(encodeone,s)
const decode = s => s.map(n => decoder.get(n)).join("")

const plaintext = text2.map(convert)
const ciphertext = plaintext.map(encode)
const plaintext2 = ciphertext.map(decode)

keytext.forEach(s=>console.log(s))
console.log()
plaintext.forEach(s=>console.log(s))
console.log()
plaintext2.forEach(s=>console.log(s))
```

Here’s the output with the key text, the plain text and the decrypted cipher text, we can see one of the disadvantages of Beale’s scheme – some of the letters do not occur as first letter in the key text so cannot be enciphered:

```𐌰𐍄𐍄𐌰 𐌿𐌽𐍃𐌰𐍂 𐌸𐌿 𐌹𐌽 𐌷𐌹𐌼𐌹𐌽𐌰𐌼
𐍅𐌴𐌹𐌷𐌽𐌰𐌹 𐌽𐌰𐌼𐍉 𐌸𐌴𐌹𐌽
𐌵𐌹𐌼𐌰𐌹 𐌸𐌹𐌿𐌳𐌹𐌽𐌰𐍃𐍃𐌿𐍃 𐌸𐌴𐌹𐌽𐍃
𐍅𐌰𐌹𐍂𐌸𐌰𐌹 𐍅𐌹𐌻𐌾𐌰 𐌸𐌴𐌹𐌽𐍃
𐍃𐍅𐌴 𐌹𐌽 𐌷𐌹𐌼𐌹𐌽𐌰 𐌾𐌰𐌷 𐌰𐌽𐌰 𐌰𐌹𐍂𐌸𐌰𐌹
𐌷𐌻𐌰𐌹𐍆 𐌿𐌽𐍃𐌰𐍂𐌰𐌽𐌰 𐌸𐌰𐌽𐌰 𐍃𐌹𐌽𐍄𐌴𐌹𐌽𐌰𐌽 𐌲𐌹𐍆 𐌿𐌽𐍃 𐌷𐌹𐌼𐌼𐌰 𐌳𐌰𐌲𐌰
𐌾𐌰𐌷 𐌰𐍆𐌻𐌴𐍄 𐌿𐌽𐍃 𐌸𐌰𐍄𐌴𐌹 𐍃𐌺𐌿𐌻𐌰𐌽𐍃 𐍃𐌹𐌾𐌰𐌹𐌼𐌰
𐍃𐍅𐌰𐍃𐍅𐌴 𐌾𐌰𐌷 𐍅𐌴𐌹𐍃 𐌰𐍆𐌻𐌴𐍄𐌰𐌼 𐌸𐌰𐌹𐌼 𐍃𐌺𐌿𐌻𐌰𐌼 𐌿𐌽𐍃𐌰𐍂𐌰𐌹𐌼
𐌾𐌰𐌷 𐌽𐌹 𐌱𐍂𐌹𐌲𐌲𐌰𐌹𐍃 𐌿𐌽𐍃 𐌹𐌽 𐍆𐍂𐌰𐌹𐍃𐍄𐌿𐌱𐌽𐌾𐌰𐌹
𐌰𐌺 𐌻𐌰𐌿𐍃𐌴𐌹 𐌿𐌽𐍃 𐌰𐍆 𐌸𐌰𐌼𐌼𐌰 𐌿𐌱𐌹𐌻𐌹𐌽
𐌿𐌽𐍄𐌴 𐌸𐌴𐌹𐌽𐌰 𐌹𐍃𐍄 𐌸𐌹𐌿𐌳𐌰𐌽𐌲𐌰𐍂𐌳𐌹 𐌾𐌰𐌷 𐌼𐌰𐌷𐍄𐍃
𐌾𐌰𐌷 𐍅𐌿𐌻𐌸𐌿𐍃 𐌹𐌽 𐌰𐌹𐍅𐌹𐌽𐍃

𐌱𐍂𐌿𐌽𐌰𐌹𐌼 𐌱𐌰𐌹𐍂𐌹𐌸 𐌱𐌰𐌹𐍂𐌺𐌰 𐌱𐍉𐌲𐌿𐌼
𐌻𐌰𐌿𐌱𐌰𐌽𐍃 𐌻𐌹𐌿𐌱𐌰𐌽𐍃 𐌻𐌹𐌿𐌳𐌰𐌽𐌳𐌴𐌹
𐌲𐌹𐌻𐍅𐌰𐌲𐍂𐍉𐌽𐌹 𐌲𐌻𐌹𐍄𐌼𐌿𐌽𐌾𐌰𐌽𐌳𐌴𐌹
𐌱𐌰𐌲𐌼𐌴 𐌱𐌻𐍉𐌼𐌰 𐌱𐌻𐌰𐌿𐌰𐌽𐌳𐌴𐌹
𐍆𐌰𐌲𐍂𐌰𐍆𐌰𐌷𐍃𐌰 𐌻𐌹𐌸𐌿𐌻𐌹𐌽𐌸𐌹
𐍆𐍂𐌰𐌿𐌾𐌹𐌽𐍉𐌽𐌳𐌴𐌹 𐍆𐌰𐌹𐍂𐌲𐌿𐌽𐌹
𐍅𐍉𐍀𐌾𐌰𐌽𐌳 𐍅𐌹𐌽𐌳𐍉𐍃 𐍅𐌰𐌲𐌾𐌰𐌽𐌳 𐌻𐌹𐌽𐌳𐍉𐍃
𐌻𐌿𐍄𐌹𐌸 𐌻𐌹𐌼𐌰𐌼 𐌻𐌰𐌹𐌺𐌰𐌽𐌳𐌴𐌹
𐍃𐌻𐌰𐌹𐌷𐍄𐌰 𐍂𐌰𐌹𐌷𐍄𐌰 𐌷𐍅𐌴𐌹𐍄𐌰𐍂𐌹𐌽𐌳𐌰
𐍂𐌰𐌶𐌳𐌰 𐍂𐍉𐌳𐌴𐌹𐌸 𐍂𐌴𐌹𐍂𐌰𐌽𐌳𐌴𐌹
𐌱𐌰𐌽𐌳𐍅𐌰 𐌱𐌰𐌹𐍂𐌷𐍄𐌰 𐍂𐌿𐌽𐌰 𐌲𐍉𐌳𐌰
𐌸𐌹𐌿𐌳𐌰 𐌼𐌴𐌹𐌽𐌰 𐌸𐌹𐌿𐌸𐌾𐌰𐌽𐌳𐌴𐌹
𐌰𐌽𐌳𐌰𐌽𐌰𐌷𐍄𐌹 𐌼𐌹𐌻𐌷𐌼𐌰𐌼 𐌽𐌴𐌹𐍀𐌹𐌸
𐌻𐌹𐌿𐌷𐍄𐌴𐌹𐌸 𐌻𐌹𐌿𐌷𐌼𐌰𐌼 𐌻𐌰𐌿𐌷𐌼𐌿𐌽𐌹
𐌻𐌰𐌿𐌱𐍉𐍃 𐌻𐌹𐌿𐌱𐌰𐌹 𐍆𐌻𐌹𐌿𐌲𐌰𐌽𐌳 𐌻𐌰𐌿𐍃𐌰𐌹
𐍄𐌿𐌻𐌲𐌿𐍃 𐍄𐍂𐌹𐌲𐌲𐍅𐌰 𐍃𐍄𐌰𐌽𐌳𐌰𐌽𐌳𐌴𐌹
𐌱𐌰𐌹𐍂𐌺𐌰 𐌱𐌰𐌶𐌰 𐌱𐌴𐌹𐌳𐌹𐌸 𐌱𐌻𐌰𐌹𐌺𐌰
𐍆𐍂𐌰𐌿𐌾𐌹𐌽𐍉𐌽𐌳𐌴𐌹 𐍆𐌰𐌹𐍂𐌲𐌿𐌽𐌹

𐌱.𐌿𐌽𐌰𐌹𐌼.𐌱𐌰𐌹.𐌹𐌸.𐌱𐌰𐌹..𐌰.𐌱.𐌲𐌿𐌼
𐌻𐌰𐌿𐌱𐌰𐌽𐍃.𐌻𐌹𐌿𐌱𐌰𐌽𐍃.𐌻𐌹𐌿𐌳𐌰𐌽𐌳.𐌹
𐌲𐌹𐌻𐍅𐌰𐌲..𐌽𐌹.𐌲𐌻𐌹.𐌼𐌿𐌽𐌾𐌰𐌽𐌳.𐌹
𐌱𐌰𐌲𐌼..𐌱𐌻.𐌼𐌰.𐌱𐌻𐌰𐌿𐌰𐌽𐌳.𐌹
𐍆𐌰𐌲.𐌰𐍆𐌰𐌷𐍃𐌰.𐌻𐌹𐌸𐌿𐌻𐌹𐌽𐌸𐌹
𐍆.𐌰𐌿𐌾𐌹𐌽.𐌽𐌳.𐌹.𐍆𐌰𐌹.𐌲𐌿𐌽𐌹
𐍅..𐌾𐌰𐌽𐌳.𐍅𐌹𐌽𐌳.𐍃.𐍅𐌰𐌲𐌾𐌰𐌽𐌳.𐌻𐌹𐌽𐌳.𐍃
𐌻𐌿.𐌹𐌸.𐌻𐌹𐌼𐌰𐌼.𐌻𐌰𐌹.𐌰𐌽𐌳.𐌹
𐍃𐌻𐌰𐌹𐌷.𐌰..𐌰𐌹𐌷.𐌰.𐌷𐍅.𐌹.𐌰.𐌹𐌽𐌳𐌰
.𐌰.𐌳𐌰...𐌳.𐌹𐌸...𐌹.𐌰𐌽𐌳.𐌹
𐌱𐌰𐌽𐌳𐍅𐌰.𐌱𐌰𐌹.𐌷.𐌰..𐌿𐌽𐌰.𐌲.𐌳𐌰
𐌸𐌹𐌿𐌳𐌰.𐌼.𐌹𐌽𐌰.𐌸𐌹𐌿𐌸𐌾𐌰𐌽𐌳.𐌹
𐌰𐌽𐌳𐌰𐌽𐌰𐌷.𐌹.𐌼𐌹𐌻𐌷𐌼𐌰𐌼.𐌽.𐌹.𐌹𐌸
𐌻𐌹𐌿𐌷..𐌹𐌸.𐌻𐌹𐌿𐌷𐌼𐌰𐌼.𐌻𐌰𐌿𐌷𐌼𐌿𐌽𐌹
𐌻𐌰𐌿𐌱.𐍃.𐌻𐌹𐌿𐌱𐌰𐌹.𐍆𐌻𐌹𐌿𐌲𐌰𐌽𐌳.𐌻𐌰𐌿𐍃𐌰𐌹
.𐌿𐌻𐌲𐌿𐍃...𐌹𐌲𐌲𐍅𐌰.𐍃.𐌰𐌽𐌳𐌰𐌽𐌳.𐌹
𐌱𐌰𐌹..𐌰.𐌱𐌰.𐌰.𐌱.𐌹𐌳𐌹𐌸.𐌱𐌻𐌰𐌹.𐌰
𐍆.𐌰𐌿𐌾𐌹𐌽.𐌽𐌳.𐌹.𐍆𐌰𐌹.𐌲𐌿𐌽𐌹
```
4. V said

In Ruby

```
def encipher(key, text)
words = key.scan(/\w+/)
text.scan(/\w+/).flat_map do |word|
word.chars.map do |char, i|
words
.each_with_index
.select { |word, _| word.chars.first == char }
.sample
.last + 1
end
end
end

def decipher(key, cipher)
char_map = Hash[
key
.scan(/\w+/)
.each_with_index
.map { |w, i| [i + 1, w[0]] }
]

cipher.map { |char| char_map[char] }.join
end

encipher("now is the time" , "tin")
=> [4, 2, 1]

decipher("now is the time",  [4, 2, 1])
=> "Tin"

key = "lorem ipsum dolor sit amet, consectetur adipisicing elit"
decipher(key, encipher(key, "lisa is ace"))
=> "lisaisace"

```