Cub Scout Cryptography: Using Python For Puzzles & Codes

This site started as a casual programming project. Actually, the code behind our first word solvers wasn’t even intended as a website. It was a collection of small python scripts (”code doodles”) which I threw together to play with some word game ideas. It wasn’t until later that I realized these programs could be delivered as a website.

Once you learn the basics, computer programming is easier than it looks. The secret is to break it up into small meaningful chunks: little micro-programs you can knock out in under an hour. Most of my stuff involves either Python (for solvers & server code) or Javascript (little widgets that run in a Web browser). I call these ”code doodles”, since I knock them out by opening up a code interpreter and tinkering with a couple of short lines of code until it works. Once I have a decent looking code doodle, I can plug it into a larger system (python webserver and my page templates).

We just opened up a new section of the site focused on simple codes, what I refer to as cub scout cryptography. These are computer implementations of basic pen & paper ciphers. The first two solvers are a Caesar cipher decoder and a Reverse Alphabet Code. While I would not advise using these tools to try to hide massive secrets, they can probably deter your nosy big siblings from reading your diary and may help you solve a puzzle or two….

But for the cub scouts among you, you can actually create your own version of this solver at home using Python! These codes are what is known as simple monoalphabetic substitution ciphers: every letter in the alphabet is mapped to another letter.

You will need to install Python on your computer (warning: ask your parents first!). An installer is available from the official Python site – they also have some tutorials on how to get started. Once you have Python installed and can run basic scripts (the ”hello world” example), start the IDLE interpreter and enter the following program:

import string
message = ”my secret code”
alphabet = ”abcdefghijklmnopqrstuvwxyz”
codebook= sorted(alphabet, reverse=true)
table = string.maketrans(alphabet, codebook)

print ”coded message:”, message.translate(table)

Once you have this working, play with it a bit. You can decrypt your messages by flipping the alphabet and the codebook variables in the maketrans statement. You can create new codes by swapping out the codebook statement for a new mapping. Take a look at rot13 and the Caesar cipher and see if you can change the program to handle these ciphers!

Remember….small bites (bytes?). And don’t be afraid to look questions up on Google when it doesn’t work the first time. I have been programming for over 30 years and I consult Google many times a day for help…

Good luck and have fun!

Leave a Reply

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