Lab 2 - Cryptography
Due: Monday, February 9th at 11:59PM

Overview

The goal behind this lab is to get you familiar with hashtables. When you add objects to a hashtable, you add the object based on a hashcode. In this lab you will be inserting items into the hashtable, based on the hashcode of a seperate Object. In other words, when you insert into the hashtable, you will pass in two arguments: the key, and the item to insert. You use the hashcode of the key to insert the item. Since we need to deal with collisions, and multiple keys might have a hashcode that indexes with the same slot, you should also insert the key along with the item. All you should need to do for that, is make an internal class that contains the key and the item to insert. The advantage of using this method is that it allows us to search for the value by the key, without having the value. In the context of this lab it will allow us to look up the encrypted value with just the original message and vice versa.

The encoding and decoding for this lab will be done one character at a time. However, in order to make this easier, your keys and values should just be Strings with length 1. (i.e. "a" -> "b"). When you use Scanner's next() method, remember that it will return the whole word, so you need to loop through the word to get all the substrings of length 1.

The Hash Table

You should build a generic open chain hash table, constructed as an array or ArrayList. It should have an add() method, a get() method, and a containsKey() method.

containsKey(key) - This method will return true if there is an entry with the key, false otherwise

add(key, value) - This method will store the value and key based on key's hashcode

get(key) - This method will return the value stored with the given key

Decoder

Your Decoder class will implement the following methods with a hashtable. For your decoder class, you should write the following methods:

DecodeString(String) - This method should take in a String and return the decoded version of that string

DecodeFile(String) - This method should take in the name of a file to encode and return the decoded version of the file as a String

In the constructor, you should build a hashtable from a file whose name will be passed in. Take a look at the example file to make sure you know what it looks like. The first value of every pair is the original String, and the second is the encoded String

Think about which of the two you want to be your key for the decoder. Remember that when you are trying to decode the file, you will only have the encoded String.

If you don't find an entry for a String in your message, just use the original. For example, if we only have a code file that uses all the letters of the alphabet, no value for "." would be found in our hashtable, so just use the original String ".

Encoder

Your encoder class will implement the following methods with a hashtable. For Your encoder class, you should write the following methods:

EncodeString(String) - This method should take in a String and return the encoded version of that string

EncodeFile(String) - This method should take in the name of a file to encode and return the encoded version of the file as a String In the constructor, you should build a hashtable from a file whose name will be passed in. Take a look at the example file to make sure you know what it looks like. The first value of every pair is the original String, and the second is the encoded String

Think about which of the two you want to be your key for the encoder. Remember that when you are trying to decode the file, you will only have the encoded String.

If you don't find an entry for a String in your message, just use the original. For example, if we only have a code file that uses all the letters of the alphabet, no value for "." would be found in our hashtable, so just use the original String "."

Main

This will contain your main method, where you will create a new Decoder and Encoder. You should use this for testing purposes. Create a code file that looks like the example above and a few sample files. Call encode on the files, print out the result and then decode the encoded version and print it out. Do the same for some example Strings.

Useful Files