Python

Caesar Cipher

12 points 

This program involves decoding a message that has been encoded (encrypted) using the Caesar cipher. When encoding, the Caesar cipher replaces each character with a character a certain number of positions to the right. For example, let's say the shift to the right is 5 positions. Then an A in the original message would be replaced by F in the encoded message: A -> F. Similarly, B in the original message would be replaced by G: B -> G. The same holds true for lowercase letters. A 'p' in the original message would be replaced by 'u' in the encoded message: p -> u. Sometimes the shift to the right will cause a letter to fall off the end of the alphabet. If a Z is shifted 5 positions to the right, where does it go? It wraps around to the beginning of the alphabet. Z -> E. Similarly, V -> A. The program you're writing here decodes a file that has been encoded. A right-shift of 17 positions (not 5) has been applied to the document. You'll want to decode the file by undoing the right-shift of 17, by shifting each character 17 spaces to the left. If you successfully do this, you'll be able to read the unscrambled document.

 

 

Starting Point

Download Caesar.py as a starting point for the project. The file guides you towards completing the program. You should also download the encoded or scrambled input file, RJScrambled.txt.

 

Extra Credit

5 points

In the extra credit version, you'll download the encoded or scrambled input file, ExtraCredit.txt. A certain right-shift has been applied, but in this case you don't know the size of the shift. Decoding this file first requires you to figure out what size shift has been applied to the document. To do this, you can do a letter frequency analysis. The most common letter used in English is 'e'. Your Python program could read through the document and keep track of how many times each letter appears. The letter that appears the most often corresponds to 'e' in the original document. If you find, for example, that 'h' is the letter that appears most often in the scrambled document, you could conclude that the right-shift was 3 positions. And then you could decode or unscramble the document by doing a left-shift of 3.

A Python dictionary with 26 positions could be useful here. Each letter in the dictionary would keep track of how many times that letter occurred in the document. For example, if there were 1000 occurrences of "a", dictionary["a"] would equal 1000.