Homework 9: Caesar Cipher

Assigned
Wednesday October 12, 2016
Due
11 p.m., Monday October 17, 2016
Summary
The goal of this assignment is to gain further experience with strings, file I/O, loops, conditionals, and functions.
Collaboration
Do this assignment with your assigned partner.
Submitting
Submit a Python program using the online turnin form. See below.
Scoring
16 points

Your Task

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 trick is the Caesar cipher. Pick a key between 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 encrypted 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

Create a python module named cipher.py. This module will contain all of your code for this assignment.

Part 1. Encoder

Create a function named encode that has two parameters: a filename (the name of the file with the original message) and a key. This function should return a string containing the encoded message.

Thus, your function definition should look like this:

def encode(filename, key):
<body>

Your function should only encode text that is lower-case letters. If you encounter characters that are not letters (such as punctuation, spaces, etc.,) you should not encode the character. (Leave it as is.)

Thus, going back to our original example, Ühello you” with the key “dog” would encrypt to Üksroc eri”.

Part 2. Decoder

Create a second function named decode that also has two parameters: a filename and a key. This function should return a string containing the decrypted text.

Again, you should only worry about decoding lower-case letters. Other characters should be left as is.

Part 3. Main

Create a main function. The main function should do the following:

  1. Ask the user if they want to encode or decode. Allow the user to specify this with either e or d as input.

  2. Ask the user for the name of the text file that contains the message.

  3. Ask the user for the key.

It should then call the appropriate function, either encode or decode, and print out the encoded or decoded message.

Sample Run

$ python3 cipher.py
Would you like to (e)ncode or (d)ecode? e
What is your filename? mymessage.txt
What is the key? dog
ksroceri
$ python3 cipher.py
Would you like to (e)ncode or (d)ecode? d
What is your filename? mycodedmessage.txt
What is the key? dog
helloyou

Part 4. Including Spaces

This last part is a lot trickier, but only worth 2 points of the total assignment. Create 2 new functions, named encode_space and decode_space. These functions should also encode the spaces and newlines, so that someone looking at the encrypted message canÙt tell how many letters were in each word or line in the plain text. To do this, the simplest way is to change your setup so that you think of your alphabet as having 2 letters: the original 26 letters and a space and a newline character. This means that your encoded message may have spaces or newlines in it, but they wonÙt correspond to positions where spaces were in the plain text.

Grading and Submission

Please document your functions and organize your code per section 3.4 of the textbook.

Submit one file, cipher.py, through the online turnin form. If you submitted a separate Above & Beyond program, make sure it is clearly identifiable.

  • Part 1: Encode - 5 pts

  • Part 2: Decode - 5 pts

  • Part 3: Main - 2 pts

  • Part 4: Include spaces and newlines  - 2 pts

  • Style, comments, following instructions - 2 pts

Good luck, and have fun!



Janet Davis (davisj@whitman.edu).
This assignment is adapted by Andy Exley from one developed by Dave Musicant.

Created October 5, 2016
Last revisedOctober 17, 2016, 02:19:52 PM PDT
CC-BY-NC-SA This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.