Hail, Caesar!

This serves as a follow-up to my award-winning (untrue) blog post on How to Create Your Own Weather Forecast Program Using Python. This time around, we’re going to learn how to program own our Caesar cipher (encrypting and decrypting) using Python, as well as how to hack that same cipher. I suppose the ambitious student could combine these two tutorials in order to send encrypted weather forecasts to his friend. Here’s what you’ll be making:

Caesar cipher

To be very clear, most of the content in this tutorial is merely a modified version of Hacking Secret Ciphers with Python. In particular, I’ve tweaked and combined components of chapters 6, 7, and 12 in a way that I hope students will find interesting. If you find this material interesting, I encourage you to read the entire book, which is available in its entirety for free online.

Getting Started with Python

If you’re a Penn State IST student, you can use the classroom computers, which come pre-loaded with Python and IDLE. If you’re not on campus, you can access them through your browser (this is very cool).

If you’re not a Penn State IST student, or if you want to install Python on your own computer, I encourage you to read Chapter 2 (“Installing Python”) of Hacking Secret Ciphers with Python.

Preparing for Detecting English

I often have to evaluate written work created by college students, so I have signficant experience in detecting when something is written in English, and when it is not (ha!). Our Python script will face the same issue when we brute-force hack the encrypted string; it will use the Caesar deciphering code with every possible key, then evaluate the results against a dictionary of English words. As a heuristic, we’ll say that if 20% of the words in the deciphered message are found in the dictionary file, and 85% of the characters are letters or spaces, then we’ve possibly identified the correct key.

We could re-create this code ourselves, but once again Al Sweigart’s Hacking Secret Ciphers with Python has us covered; see Chapter 12 (“Detecting English Programmatically”). I encourage you to read the entire chapter, but in the interest of creating a tutorial that can hopefully be completed in about an hour, you should:

  • Download the dictionary file and save it to your computer’s desktop. This file contains a whole bunch of English words. You’re probably already familiar with many of them.
  • Download detectEnglish.py and save it to your computer’s desktop. This is a Python module that simply returns true or false, according to the heuristic described above, as to whether the message contains English.

Shifting Keys

In the spirit of putting the bottom line up front, here’s the completed script:

You can paste that into IDLE, save as caesarCipher.py, then hit F5 on your keyboard to run the program.

As I said, this is mostly a combination of chapters 6 (for encryption and decryption), 7 (for brute force hacking), and 12 (for detecting English) of the hacking Python book. For line-by-line commentary on how the code works, please see those chapters. My version of the script merely combines the several functions together, and adds some error-checking.

What’s Next?

I don’t know, Christmas I guess? But if you mean in terms of learning more Python and/or cryptography, I recommend: