CIT 591 Playfair Cipher Clarifications
Fall 2008, David Matuszek


The purpose of the StringReader is, given a String, to return the characters from that String two at a time. It also ensures that the two characters returned are not the same, by inserting an 'x' or a 'q' as necessary.

For example, suppose you construct the StringReader with
    StringReader reader = new StringReader("helloworld");
then the subsequent calls to reader should return the following two-element character arrays:

  1. {'h', 'e'}
  2. {'l', 'x'}
  3. {'l', 'o'}
  4. {'w', 'o'}
  5. {'r', 'l'}
  6. {'d', 'x'}
  7. null // To indicate that no characters remain
It is not the job of the StringReader to do any encoding or decoding; it just returns the next pair of letters, each time it is called.


The constructor for CodeBlock has the task of creating a 5x5 array from the keyword or key phrase that is passed in. To do this, it first has to "clean up" the keyword or key phrase by lowercasing it, replacing 'j' with 'i', removing all nonletters, and removing all duplicate characters. There are methods in Sun's String class and your own StringUtilities class that it can use to do this.

CodeBlock contains a getCodeArray() method. The only purpose of this method is so that you can JUnit test the constructor, to see whether it constructs the correct 5x5 array from the keyword or key phrase.

Since the CodeBlock object contains the 5x5 array, it has encipher and decipher methods for character pairs. These methods take a pair of characters (in a two-element char[] array), encipher or decipher that pair of characters, and return the enciphered/deciphered pair in another two-element char[] array). The job of enciphering or deciphering a complete message is done in the PlayfairCipher class.


This is the "main" class--the one containing a main method.

The main method (along with any helper methods it may call) asks the user for a keyword or keyphrase, and constructs a PlayfairCipher object with that keyword or key phrase. The main method then repeatedly lets the user choose to enter a message to be enciphered, enter a message to be deciphered, change the keyword (for future enciphering/deciphering), or quit. The actual enciphering/deciphering is done by calling methods in PlayfairCipher.

The main method (or a helper method) also calls printInBlocksOfFive(String) in the StringUtilities class to print results.


The constructor for PlayfairCipher creates a CodeBlock using that keyword or key phrase; that's about all the constructor is expected to do. Of course, it has to keep that CodeBlock object (in an instance variable) for future use.

The encipher(String) and decipher(String) methods of PlayfairCipher encipher and decipher, respectively, complete messages. They do this two characters at a time, by calling the StringReader method getNextPairOfLetters() and the CodeBlock methods encipher(char[]) and decipher(char[]).


This class provides just methods for use by the other classes. Since all the methods in this class are static, there is no reason to construct any objects of type StringUtilities.

JUnit Testing

You should write JUnit tests for:

You should not write JUnit tests for