CPSC 120 -- Assignment #3: A Cryptic Assignment
Due Monday, November 10, 2008 by 4 p.m.

A Vigenère cipher is a method of encrypting text that was invented in the 16th century that obfuscates text by shifting letters based on a keyword. The cipher is named after Blaise de Vigenère, who did not invent it but invented a different more powerful cipher which was not named after him. The Confederate Army used a Vigenère cipher to send secret coded messages. Unfortunately, the Vigenère cipher had been cracked in the 16th century and the Union Army was able to tap telegraph lines and decipher messages that it intercepted.

Your task is to write a program that can encipher and decipher text files using a Vigenère cipher. A Vigenère cipher works by shifting each character in a message by an amount determined by a keyword. Characters can be shifted quite easily in Java because characters are represented by numbers. Recall that (int)'a' in Java would evaluate to 97 because each character has a number associated with it. In this case the character 'a' is associated with the number 97. Also, because the letters 'a' through 'z' have sequential numbers it is possible to shift a character to the right by first converting it to an integer, then adding the amount to shift it by, and finally converting it back to a character. For example, in order to shift the character 'a' three letters to the right to 'd' the following code (char)(((int)'a')+3) could be used. This could be simplified to just (char)('a'+3) by taking advantage of automatic promotion in Java. In order to shift the character 'y' to the right by five to the character 'd' it is not sufficient to just add 5 to the character 'y'. Instead you must have the shift wrap back around to the beginning of the alphabet if it exceeds the last character, 'z'. (hint: this can be accomplished using modulo)

In a Vigenère cipher each character in the message to be enciphered is shifted to the right by an amount based on a character in a keyword. For example, to shift the character 's' to the right by the character 'd' the character 'd' is converted into an integer based on how many characters it is from the character 'a'. The character 'd' is 3 characters from the letter 'a'. This can be computed in by subtracting 'a' from 'd'. Therefore, in order to shift the character 's' to the right by 'd', shift the character 's' by 3. In order to decipher messages the same procedure is followed, except, characters are shifted to the left instead of the right.

However, in a Vigeneger cipher not every character in a message is shifted by the same character. Each character is shifted by the corresponding character in the keyword. That is, if the message being enciphered is "something fowl", the keyword is "duck", and the first letter of the message, 's', is shifted by the first letter of the keyword, 'd', then the second letter of the message, 'o' is shifted by the second letter of the keyword, 'u'. If there are more letters in the message than there are in the keyword, which there usually are, then the amount to shift by wraps back around to the first letter in the keyword after a character in the message has been shifted by the last letter. Characters that are not in the alphabet, like the space character, should not be shifted. Using the above example the message "something fowl" enciphered using the keyword "duck" is:

plain text s o m e t h i n g   f o w l
shift ammounts d u c k d u c k d u c k d u
enciphered text v i o o w b k x j   h y z f

Your program should prompt the user to enter the name of a text file that contains a message, the keyword to use with the message, and whether to encipher or decipher the message. It should then print the result of enciphering or deciphering the text file to the console. In the Java Character class there is a method isLetter that returns true if a character is a letter and false if it is not. Capital letters can be converted to lowercase letters by using the toLowerCase method in the String class. You can test your program on the following files:

Other Requirements

As usual, use good programming techniques. You should use good variable names, constants where appropriate, whitespace, and correct indentation and alignment of statements. Documentation must include a comment for the class (it should include the @author tag and a clear, complete description of what the program does) and comments for the main method. The program should be broken up into logical sections and each section should be documented with a brief description of what it does. Your program should be in the package assign3.

Your program must compile!!! A program with a compilation error, no matter how small, will receive at most 25% credit. Your program must compile and run.

Academic Integrity Reminder!!! Programming assignments are to be your own work. You may get help on the specifics of the assignment from no one except the instructor. You may not show your program to anyone or look at anyone else's program or share ideas with anyone about how to write the program or solve the problem.

Hand In

Turn in a printed copy of your program (the source code) and email the java file to your instructor. Be sure the subject line says cpsc120 assign3.