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.
Change into the directory where you put your labs and projects, and then copy the directory for project 2 with
$ cp -r /cslab/class/csci235/projects/project2 project2You should now be able to see a new directory containing a source file
Cipher.java
and several other "mystery" files, each of
which contains an encrypted text.
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 can 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 ciphertext goes here". A capital letter should be shifted to a capital letter, a lowercase to a lowercase, and non-letters, not at all.
If you can't think of how to shift a letter, you might look at this hint.Some possibly useful vocabulary:
Test your program by decoding the following messages sent over the front lines:
Also test it on the files mystery1
, mystery2
,
and mystery3
.
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.
When you have a program that works, think of it as a rough draft.
Now it is time to polish it. Read through it to see if there are
things that should be fixed. Are the variable names helpful? Is
there anything confusing in what you have written? Anywhere that a
comment is called for? Also, make sure that you have followed
the coding guidelines for this
class. And don't forget to put your name at the @author
tag.
You may find it easier to do this kind of proofreading and polishing if you print out a paper copy.
Make a script file to demonstrate the working of your program(s) on at least one keyboard input and one file input. Print the script 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 project2
with the command
/cslab/class/csci235/bin/handin project2 Cipher.java (the name of the script file)
DUE: Monday, Feb. 8, 5:00 p.m.