## Ciphers, Beaufort, and Bears… Oh my!

3 09 2009

In my cryptography class, we’re starting to learn about various ciphers, and for a homework assignment, we were tasked with doing some ciphering by hand.

These rhinos are hot on the trail of some cryptography!

I find the task very interesting, but I quickly got tired of coding even a 10-letter “sentence”. Then I thought to myself, wait, I can program! This specifically seems like something that could quickly be done with a Python script. So I decided to spend a little time typing, and 5 minutes later, no more ciphering by hand! Below are the results for your own amusement!

For my assignment, we were told to use the Beaufort cipher, which is similar to the Vigenere cipher, both of which are several hundred years old. The Beaufort cipher takes some plaintext M and a key K of length t, and then performs the mathematical operation C[i]=(m[i] + k[i mod t]) mod 26 for each letter i of the plaintext. An interesting fact is that this operation will both encrypt and decrypt, making it a reciprocal cipher.

Anyways, here is the Python code that I wrote to perform encryption/decryption:

```
k = [10,4,24]
t = len(k)

def asint(theChar):
return ord(theChar)-ord('A')

def asletter(theNum):
return chr(theNum+ord('A'))

def applyBeaufort(plaintext):
results = []
for i in range(len(plaintext)):
results += [asletter((k[i % t] - asint(plaintext[i])) % 26)]
return results

That code isn’t bulletproof, obviously, and makes a few assumptions:

All text will be entered as a capital letter.
The key will be specified as a list of numbers.

These two points aren’t really that hard to correct, but they’re the 20% of work that will take 80% to do right. I just wanted to give you a quick sample.
Want to see if it works? Alright, here you go!
Using plaintext: RANTSRAMBLESANDRHINOS
['T', 'E', 'L', 'R', 'M', 'H', 'K', 'S', 'X', 'Z', 'A', 'G', 'K', 'R', 'V', 'T', 'X', 'Q', 'X', 'Q', 'G']
-------
['R', 'A', 'N', 'T', 'S', 'R', 'A', 'M', 'B', 'L', 'E', 'S', 'A', 'N', 'D', 'R', 'H', 'I', 'N', 'O', 'S']
That’s all for now, I hope you enjoyed this little tidbit about the Beaufort cipher and maybe even find the code snippet useful!

div.wpmrec2x{max-width:610px;}
div.wpmrec2x div.u > div{float:left;margin-right:10px;}
div.wpmrec2x div.u > div:nth-child(3n){margin-right:0px;}

(function(g,\$){if("undefined"!=typeof g.__ATA){
}})(window,jQuery);

var o = document.getElementById('crt-812923831');
if ("undefined"!=typeof Criteo) {
var p = o.parentNode;
p.style.setProperty('display', 'inline-block', 'important');
o.style.setProperty('display', 'block', 'important');
});
} else {
o.style.setProperty('display', 'none', 'important');
o.style.setProperty('visibility', 'hidden', 'important');
}

Related
```

### One response

3 09 2009

```    """Simple Beaufort cipher implementation.

Modified from..
https://samkerr.wordpress.com/2009/09/03/ciphers-beaufort-and-bears-oh-my/
..by dbr, http://neverfear.org
"""

from string import letters

def asint(theChar):
return letters.index(theChar)

def asletter(theNum):
return letters[theNum]

def applyBeaufort(plaintext, key):
t = len(key)

results = []
for i in range(len(plaintext)):
results += [asletter((key[i % t] - asint(plaintext[i])) % len(letters))]

return "".join(results)

if __name__ == '__main__':
key = [10, 4, 24]
intext = 'TeSt'
encoded = applyBeaufort(intext, key)
decoded = applyBeaufort(encoded, key)

assert intext == decoded, "%s != %s" % (intext, decoded)
```

Not sure if the formatting will be screwed up, if so http://pastie.org/604407