Project 1: 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 files from the course public directory.

cp /cslab/class/csci235/proj1/Cipher.java .
cp /cslab/class/csci235/proj1/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 cab type quit 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

Make a script file to demonstrate the working of your program(s) on at least one keyboard input and one file input. Clean the script and print it and your program source with

a2ps Cipher.java (the name of the script file)

Turn the printout in to either my office or my mailbox in the department office.

Also turn it in as project1 with the command

/cslab/class/csci235/bin/handin project1 Cipher.java (the name of the script file)

DUE: Monday, Sept. 27, 5:00 pm


Original by Thomas VanDrunen,
updated by Cary Gray
Last modified: Tue Sep 28 13:22:08 CDT 2010