CPSC 120 - Assignment #5

Code Breaker
Due by 4 p.m. Friday, November 30, 2007

Rumor has it that Boris and Natasha have a new plot for world domination. You have been recruited by the CIA to help scan network traffic for encrypted messages that detail their plans (with a proper warrant, of course). Fortunately for you Boris and Natasha use a relatively primitive encryption method, known as a Caesar cipher. In this cipher each letter in the text is replaced by the letter that is a fixed number of positions to the right (modulo 26 - that is, wraparound to the beginning of the alphabet occurs). For example, if each letter were shifted 3 positions to the right, the word key would become nhb .

Your job is to design a program that reverses the encryption and allows you to uncover the details of the plot. Of course for a simple cipher like this, you could use "brute force" - try substituting all 26 letters until you find a substitution that works. However, a better way to break the code is to use a technique called frequency analysis. For any substitution cipher, such as the Caesar cipher, where a given letter is always replaced by some fixed letter, the frequency of letters is not hidden by the cipher. Hence, the best way to try to break the ciphertext is to count the number of instances of each letter in the ciphertext, then try substituting the most frequent letter in English for the most frequent letter in the cipher text. If that doesn't work, substitute the next most frequent letter in English for the most frequent letter in the ciphertext. Eventually you get a substitution that "breaks" the ciphertext. (Note: you can find the frequency of letters in English by doing a search on the Web).

In this assignment you will write a program that allows you to read a message from a file, perform a frequency analysis and decrypt the message. Your program will have a GUI that looks something like this:

Program Structure

This program is comprised of three classes (CryptoPanel, CryptoApp and Email) You will be responsible for writing CryptoPanel and CryptoApp; the Email class will be provided for you.

CryptoPanel:

The class should have the following instance data:

The class should provide the following methods:

The class also must contain a nested listener class. The actionPerformed method of the listener should determine which button was pressed and take the appropriate actions.

CryptoApp:

This class contains the main method that instantiates a frame and adds the CryptoPanel to the contentpane of the frame. The frame should also contain the title of the program, including your name.

Email

This class can be added to your project by downloading the file Email.jar to your assign5 directory. Take the following steps to include the Email class in your project:

  1. Make sure that your package name is assign5.
  2. Right-click on the Assignments project (In the package explorer panel). Expand the option for "Build Path" and then select "Add External Archives"
  3. A new window will pop up titled "Jar Selection". Find the file called "Email.jar", select it and press ok.
  4. You should observe that "Email.jar" has been added to the package explorer (probably right below JRE System library or JDK 1.6).
You should now be able to add objects of type Email to your code. There are two methods that you should be interested in:


Programming Notes

Working with characters: To shift the characters you can use the fact that characters are represented by a number in Unicode (See Appendix C for the Unicode character set). Casting a character to int gives the Unicode value of the character and casting an int to char gives the character with that Unicode value (assuming the int is in the correct range). So the shifting for encryption and decryption can be done with careful casting. (Hint: (char)('a' + 3) gives you 'd') To get the wraparound (so that if you shift y by 5 you get d, for example) you need to use the remainder (%) operator.

In lab 10 we used a method from the Character class to test to see if a character was a letter. There are similar methods to check for lowercase and uppercase. You can use those or use the fact that characters can be compared using the comparison operators <, <=, etc. So, an expression such as

      ch >= 'a' && ch <= 'z'
is legal and would determine if the character is lowercase (see Unicode character set in Appendix C).

Text Areas in Java: Text areas in Java are similar to textfields except there is a larger area for entering text. They are type JTextArea. To declare a text area you must specify the number of rows and number of columns in the constructor. For example,

     JTextArea msgArea = new JTextArea (20, 30);
creates a text area that is 20 rows by 30 columns. For text areas it is nice to allow lines to wrap. This is accomplished using the setLineWrap method. For example,
     msgArea.setLineWrap(true);
Another option is to put the textarea inside a scrollpane. For the textarea above this would be accomplished as follows:
     JScrollPane sp = new JScrollPane (msgArea);
To put a string in a text area use the setText method that takes the string as a parameter (the same as the setText method for JTextFields). Similarly to get the string out of the text area use getText.

Implementation Suggestions:

As usual you should implement this in steps. For example you could start by getting the GUI to look good for the CryptoPanel class (without the code for the GUI to actually do anything). Once that is done get the GUI to start responding. First, get the GUI to load the file into the message textarea. Next get the frequency analysis to work Finally, get the decryption button to work correctly.

Testing your Program

The data file Sample.txt contains a simple, unencrypted message. Use this file to verify that your program is reading a file correctly.

The following files contain encrypted messages that can be used to incriminate Boris and Natasha (and test your program).

Note: You can also type messages directly into the text areas to verify that your frequency analysis and decryption methods are working correctly.

Other Requirements: As usual use good programming practices that include appropriate naming of variables and use of constants, correct indentation, and documentation that includes documentation at the beginning of every class describing that class, documentation at the beginning of every method that describes what that method does, its parameters, and what it returns (this documentation should be JavaDoc style), and internal documentation where necessary to explain what the code is doing.

Turn in: Printed copy of your work. Tar your assignment directory and send the tar file as an email attachment to your instructor.

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.

Bonus!

Your program works both ways. You can also use your decrypt method to encrypt messages as well. Create a new message that outlines what Boris and Natasha are plotting as well as when they will act. Encrypt it and send it to your instructor. If you are one of the first five to submit this information, you will receive bonus credit. Hurry, time is of the essence!