Double-Caesar Cipher

Table of Contents

This assignment is to be done individually. You can talk to other people in the class, me (Dave), the prefect, and lab assistants for ideas and to gain assistance. You can help each other debug programs, if you wish. The code that you write should be your own, however, and you shouldn't hand a printout of your program to others. See the course syllabus for more details or just ask me if I can clarify.

We will use anonymous grading on Moodle, which means that the grader won't see your name until after the grading is done. This is an easy way to help add an extra element of fairness to the grading. Therefore, make sure your name doesn't appear on your actual submission. When you submit via Moodle, it will know you are. Thanks!

1 Overview

An important class of algorithms that some computer scientists concern themselves with are encryption and decryption techniques. How can text be encoded so that no one can read it apart from the desired recipient? This is critical for trade secrets and government messages, but even more critical for such common needs as secure web-based purchases, being able to send your password over the web without others listening in, and so on.

An old simplistic trick is the Caesar cipher. Pick a key from 1 and 25; then for each character in your message, shift each letter forward by the key, wrapping around the end of the alphabet. For example, if your original message is helloyou, and your key is 2, your encrypted message is jgnnqaqw. Supposedly, Julius Caesar used this technique to communicate with his generals. It is very easy to crack, however.

A slightly harder but dramatically more effective variation is the double-Caesar cipher. It works just like the Caesar cipher, but each letter in your message gets shifted by a different amount. How much? It is based on another string of text, called the key. Each letter in the key tells you how many letters to advance: an a is 0, a b is 1, and so on. For example, if your original message is helloyou and your key is dog, the d in dog means that you shift the first letter of helloyou 3 letters, the o in dog means you shift the second letter of helloyou by 14 letters, and the g in dog means you shift the third letter of helloyou by 6 letters. You then repeat the pattern: the fourth letter of helloyou gets shifted by 3 letters, the fifth letter gets shifted by 14 letters, and so on. This produces the encrupted message ksroceri. Double-Caesar ciphers are actually quite hard to crack.

Just to try to help make it clear, here's that same encoding again:

Original:     h e l l o y o u
Repeated key: d o g d o g d o
Encrypted:    k s r o c e r i

2 Your assignment

Create a directory called cipher. You will actually write two programs.

2.1 The first program

The first program, called encoder.py, should have the user first enter in a message in English, and then a key. Your program should assume that your input text is entirely in lower case, with no punctuation, and no spaces. encoder.py should read in this information, then print out to the screen the encrypted message via the double-Caesar cipher.

Here is a sample run below.

What is the original message? helloyou
What is the key? dog
The encrypted message is ksroceri

Once you have the basic input setup working, you should save yourself time by putting your input in a file, and redirecting your input from there. That is how we will test your code. In other words, we will create a file that looks like this:

helloyou
dog

If that file happened to be named source.txt, we would run your program as follows:

$ python3 encoder.py < source.txt

2.2 The second program

Your second program, called decoder.py, should read an encrypted message and a key. Your program should assume that your encrypted text is entirely in lower case, with no punctuation, and no spaces. Similarly to the above, your program should first prompt for the encrypted message, and then prompt for the key. Your program should print out the decoded message to the screen.

Here is a sample run below.

What is the encrypted message? ksroceri
What is the key? dog
The original message is helloyou

As with the first program, you'll be faster at testing it (and we will test it this way) by redirecting from an input file. In other words, we will create a file that looks like this:

ksroceri
dog

If that file happened to be named encrypted.txt, we would run your program as follows:

$ python3 decoder.py < encrypted.txt

3 Some Python notes and hints

To do this exercise, you'll want to use a loop where you loop over each character in a string. Here's a sample that just prints out every character individually:

for letter in "happyday":
    print(letter)

Some of you may be tempted to write an if statement with 26 cases, one for each letter of the alphabet. Do not do this; we'll take off significant design points if you do. Make use of the built-in chr and ord functions, which help you convert letters to numbers and back again. Try the following program to see what you get:

print(ord("a"))
print(chr(98))

4 The last point

If you complete the above successfully (and have good style in your code), you will receive nearly all the points for the assignment. If you get this far, you should feel proud of your achievements! If you want to push yourself harder and go for the last point, submit a variation on your programs that also allows spaces between words. The catch is that you should also encode the spaces, so that the person looking at the encrypted message can't tell how many letters each word in the original was. To do this, the simplest way is change your setup so that you think of your alphabet as having 27 letters: the original 26 letters and a space. This means that your encoded message may have spaces in it, but they may not correspond to positions where spaces were in the original. If you do this, first do the original approach as described above, then submit your updated programs as encoder_space.py and decoder_space.py. Separating this out makes grading easier.

5 Style

You should make sure that your program follows good style. You should it as readable as possible for someone else trying to understand them. At a minimum, you should put a comment at the top explaining what the program does, and use comments above consecutive portions of Python code explaining what they do. You should also use meaningful and readable variable names. Furthermore, (I'm adding this on from previous assignments), your code should be minimal where reasonable. In other words, your code shouldn't contain an excessive number of variables or lines of code, unless doing so adds to readability.

6 Submit your work

When finished, zip up your code and submit your work through Moodle.

Good luck, and have fun! Remember that lab assistants are available in the evenings in CMC 102 and CMC 306 to help out if you need it, and you can attend prefect sessions as well.

If you want to feel super powerful: this assignment is anonymous, but if you use your program to encode your name with a key that you don't give us, you can put your encoded name in a comment and not violate the anonymity requirement. There's no reason or requirement to do this, but you might enjoy doing so.

Author: Dave Musicant

Emacs 24.5.1 (Org mode 8.2.10)

Validate