Double-Caesar Cipher

This assignment is to be done individually. You may share ideas with others, discuss your programs, and help each other debug by looking at someone else's screen; but you should not print out any code to share with each other. Your should ultimately submit your own work.

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, an 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

Your assignment

Create a directory called cipher. You will actually write two programs. The first one, called encoder.py, should read a message in English and a key, both contained in a file called source.txt. Your program should assume that your input text is entirely in lower case, with no punctuation, and no spaces. The first line of your file should contain the text, and the second line should contain the key. encoder.py should read in this information, then print out to the screen the encrypted message via the double-Caesar cipher.

Your second program, called decoder.py, should read an encrypted message and a key from a file called encrypted.txt. Your program should assume that your encrypted text is entirely in lower case, with no punctuation, and no spaces. Similar to the above, the first line of your file should contain the encrypted message, and the second line should contain the key. Your program should print out the decoded message to the screen.

If you complete all of the above, this will get you 19 out of 20 points. As an extra challenge, 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.

Have fun, and good luck!