CPSC 120 - Assignment #4

Encryption and Decryption
Due by 4 p.m. Friday, December 8, 2006

Security is a major concern in modern computing systems. Confidential information stored in computer databases and transmitted over computer networks needs to be protected from unauthorized access. The systems themselves must be restricted to authorized users. One of many means of ensuring such security is by encrypting data that is stored and transmitted by computers, and using encrypted passwords to limit access to systems. Encryption is the process of converting data (generally called a message) from its original form, called plaintext, into "scrambled" form, called ciphertext. The process of turning ciphertext back to plaintext is called decryption. A goal in systems security is to find encryption algorithms (called ciphers) that are relatively easy to compute but difficult to "reverse" (to break) without some knowledge of the decryption algorithm (especially the "key" needed to decrypt). Most modern algorithms rely heavily on number theory and use the fact that it is difficult to factor very large numbers (a few hundred digits long) in a reasonable length of time. Early algorithms, however, relied on much simpler ideas. One of the simplest algorithms is called the shift transformation or Caesar cipher (supposedly due to Julius Caesar). In this cipher each letter in the plaintext 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). In the original Caesar cipher, each letter was shifted 3 positions to the right. So an a in plaintext would become a d in ciphtertext, a c in plaintext would become an f, a y in plaintext would become a b, and z would become c.

In this assignment you will write a program with a graphical user interface that simulates sending encrypted messages across a network. Sending messages involves two parties that want to communicate, a network (or a communications channel), and the messages. Your program will have a GUI with three side by side panels - one for each of the parties who want to exchange messages and one in between that represents the network. Each of these panels will have a place for a message (we will use a text area instead of a textfield so there is more room for the message) and other components to let the user do things with the message. For example, the panels for the parties involved will have a button to let the user send the message in plaintext (unencrypted) and another to let the user send a message encrypted (in this case the user must also enter a shift for the encryption in a textfield). The panel representing the network will have a text area that a sent message is displayed in - if the message is sent encrypted the encrypted message will be displayed; otherwise the plaintext will be displayed.

The network panel will have components that let the user try to "break" the message using 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 do a frequency analysis (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. Of course for a simple cipher you could use "brute force" - try all 26 letters until you find a substitution that works.

Program Structure

It should be evident from the description above that the objects involved here are messages, the communicating parties, and the network. Each of these will be modeled using classes with the classes for the parties and the network each being GUI panels.

The Message Class: The Message class should have the following instance data: a String for the message itself (which could be either in plaintext or ciphertext form), a boolean indicating whether or not the message is encrypted (true if it is encrypted, false otherwise), an int that is the shift used in encrypting the message.

The class should have the following methods:

The PartyPanel Class: This is the GUI representing a party wishing to send messages. 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 get the string from the textarea. If the button that triggered the event was the button to send the message as plaintext then an unencrypted message should be declared and instantiated. The message should be "sent" to the recipient by "asking" the network to receive it (see the receive method in the NetworkPanel class below). If the button that triggered the event was the button to encrypt and send, the shift value needs to be gotten from the textfield and converted to an int, then an encrypted message should be instantiated. That message should be "sent" to the recipient through the network in the same manner as the plaintext message. Note that "sending" a message is just one method call!

The NetworkPanel Class: The only instance variables in the NetworkPanel class are components needed in the GUI.

The class should provide the following methods:

The class also must have a nested listener class. The actionPerformed method should display the most frequent character if the button requesting that was clicked. If the button to try a substitution was clicked then the method needs to get the character for the substitution from the textfield, compute the correct shift, then decrypt the message using that shift and display the result in the bottom textarea.

A class named HackerHelper has been created containing two static methods you can use. You can download the bytecode HackerHelper.class to your project directory. The method signatures are as follows:

    public static char mostFrequent (String str)
      -- finds the most frequent letter in the string str and returns it

    public static String hack (String msg, int shift)
      -- uses the shift to shift every letter in the string msg that
         many characters down in the alphabet. The shifted string is
         returned.


The classes above are the main ones for the program but to fit everything together you need two more classes - one for a main panel that puts together the other panels (TransmissionPanel) and one with a main method that instantiates a frame and adds the main panel to the contentpane of the frame (Crypto.java). The TransmissionPanel class is as follows.

The TransmissionPanel class This class needs only a constructor that does the following:


Programming Notes

Encryption and Decryption - 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 (and in some cases you don't need to cast - adding an int to a char gives a char). To get the wraparound (so that if you shift y by 5 you get d, for example) you need to use the remainder (%) operator.

Also for this program we will encrypt only letters. So your encrypt and decrypt methods should check to make sure the character is a letter. If it is a letter it should be encrypted (or decrypted) otherwise it shouldn't be. You should encrypt lowercase letters to be lowercase and uppercase letters to be uppercase. In lab 9 we used a method from the Character class to test to see if a character was a letter (and there are also 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 PartyPanel class (without the code for the GUI to actually do anything). Once that is done get the GUI for the NetworkPanel class. The Message class can be written and tested totally separately (you can download and use the test program TestCrypto.java or write your own test program. Finally implement the listeners so the GUI actually responds to events.

Other Requirements: As usual use good programming practices that include appropriate naming of variables, 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 and what it returns (this documentation should be clearly delineated with rows of dashes or stars or something!), and internal documentation where necessary to explain what the code is doing.

Turn in: Printed copy of Message.java, PartyPanel.java, NetworkPanel.java, and TransmissionPanel.java. Tar your project directory and send the tar file as an email attachment.

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.