Ciphers, Beaufort, and Bears… Oh my!

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!

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:

[code language="python"]

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

[/code]

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!

About to encrypt!
Using plaintext: RANTSRAMBLESANDRHINOS
['T', 'E', 'L', 'R', 'M', 'H', 'K', 'S', 'X', 'Z', 'A', 'G', 'K', 'R', 'V', 'T', 'X', 'Q', 'X', 'Q', 'G']
-------
About to decipher!
['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!

About samkerr

I'm an eclectic person. I like to dabble in a multitude of things. I'm sure you'll find my blog reflects that.
This entry was posted in Code. Bookmark the permalink.

One Response to Ciphers, Beaufort, and Bears… Oh my!

  1. dbr says:

    I made a few small modifications to your code:

        """Simple Beaufort cipher implementation.
    
        Modified from..
        http://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

Leave a Reply

Your email address will not be published. Required fields are marked *