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.
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!