Project 2: The Caesar Cipher

1. Introduction

A Caesar cipher is an encryption algorithm that shifts each letter down a set number of places in the alphabet. The shift amount serves as the key to the encryption scheme. For example, if we wanted to encrypt the string "hello" with shift amount 2, the result would be "jgnnq." Similarly we would shift each letter back by 2 to decrypt the word. Letters at the end of the alphabet would wrap around to the front. So, 'y' shifted ahead 5 would be 'd'.

You are a programmer for the ancient Gauls who are being oppressed by Roman invaders under Julius Caesar. The Romans, of course, are using the Caesar cipher, invented by their general. Your spies are able to intercept much of the Roman communication, but you do not have any of the keys (ie, the number of letters to shift).

You are commissioned to write a program that will decrypt any message, even if you don't have the key. The idea is that there are only 26 possible keys (because if you shift the message any more than 26 positions, then it simply wraps around; shifting the message 27 characters is the same as shifting it 1). You will write a program that will allow a user to shift a message ahead until an intelligible message appears.

2. Setup

Copy the following file from the course public directory.

cp /homes/tvandrun/Public/cs235/proj2/Cipher.java .
cp /homes/tvandrun/Public/cs235/proj2/mystery* .

3. Finishing the program

If a user runs the program the normal way, that is

java Cipher

then the program will ask the user for a message and then print the shifted versions, waiting for the user to hit enter before displaying the next version. (Note that this can be used to encrypt a message just as well as to decrypt one.) For example, if the text the user enters is "My name is Vercingetorix", pressing enter will change the text to "Nz obnf jt Wfsdjohfupsjy." Pressing enter again will change the text to "Oa pcog ku Zgtekpigvqtkz." (The user will have to hit control-c to quit.)

However, the user can also use this program on a larger piece of text. If a file (say, somefile) contains an encrypted text, then the user can run the program with

java Cipher somefile

the the program will not prompt the user for a string but will load the text in that file.

All that (prompting the user or reading in from a file) is done for you. Your task is to do the shifting of the message to try a new key.

Fill in the section that says " your code to shift the message again one letter goes here". A capital letter should be shifted to a capital letter, a lowercase to a lowercase, and punctuation and letters not at all.

4. Testing

Test your program by decoding the following messages sent over the front lines:

Also test it on the files mystery1, mystery2, and mystery3.

5. Extra credit

Instead of making the user shift until he or she recognizes the message, rewrite the program so that it makes use of known facts about letter and letter group frequencies in English to guess the key. If you do the extra credit, make a separate program and turn in both. Your extra credit version should get rid of the while loop that repeatedly prompts the user for the next shift.

6. To turn in

Use a script file to demonstrate the working of your program(s). Print it out and hand in a hard copy.

a2ps (the name of the script file)

DUE: Friday, Sept 18, 2009, 5:00 pm


Thomas VanDrunen
Last modified: Fri Sep 11 12:34:42 CDT 2009