CIT 591 Assignment 5: Playfair Cipher
Fall 2008, David Matuszek

# Purposes of this assignment:

• To get you started using arrays
• To get you started using JUnit and doing TDD (Test Driven Development)
• To make you more familiar with Strings

# General idea of the assignment:

You are probably familiar with the idea of "secret codes," such as the cryptograms published in some newspapers. A cipher is a more secure way of making secret messages. In this assignment you will write a program to encipher and decipher secret messages according to the Playfair cipher. (For more about the Playfair cipher, see the Wikipedia article.)

## The general algorithms

Here's how to construct a Playfair cipher.

1. Choose a keyword, for example, `university`.
2. If the letter` j `occurs in the keyword, change it to` i`. (So `university` is not changed.)
3. If a letter occurs more than once in the keyword, keep only the first occurrence. (So `university` becomes `universty`.)
4. Put the letters of the keyword into a 5 by 5 array.
 `u` `n` `i` `v` `e` `r` `s` `t` `y` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` `
5. Fill in the rest of the letters of the alphabet, in order, omitting` j`.
 `u` `n` `i` `v` `e` `r` `s` `t` `y` `a` `b` `c` `d` `f` `g` `h` `k` `l` `m` `o` `p` `q` `w` `x` `z`

Here's how to encode a message, using the array:

1. Make all the letters lowercase, and throw away all blanks and punctuation marks.
2. Change any letter `j` to `i`.
3. Take the next two letters of the message. We can't let them be both the same, so:
1. If only one letter remains, use` x `as the second letter.
2. If the two letters are the same, use` x `instead of the second letter. (The second letter will be the first letter of the next group of two.)
3. If the two letters are both` x`, use` q `instead of the second letter. (The second` x `will be the first letter of the next group of two.)
4. If the two letters are in the same column, encode each one with the letter below it, wrapping around if necessarly. (That is, if the letter is in the bottom row, move to the top row.) For example, `id` become `tl`, while `up` becomes `ru`.
5. If the two letters are in the same row, encode each one with the letter to its right, wrapping around if necessarly. (That is, if the letter is in the rightmost column, move to the leftmost column.) For example, `st` become `ty`, while `lo` becomes `mh`.
6. If the two letters are in different rows and different columns, they form a rectangle. Replace each letter with the letter in the same row but in the other corner of the rectangle. For example, `sm` becomes `yk`.
7. To print out results, print five letters at a time (possibly fewer in the last "word").

Example:

``````  Programming in Java is fun!
programminginjavaisfun
programminginiavaisfun
pr og ra mx mi ng in ia va is fu nx
ub zo sr xv lv ec vi et ey tn bv vq
ubzos rxvlv ecvie teytn bvvq``````

Here's how to decode a message, using the array:

• Take the letters two at a time (there will be an even number of letters), and for each pair of letters,
• If they are in the same row, replace each letter with the letter to its left, wrapping around if necessary.
• If they are in the same column, replace each letter with the letter above it, wrapping around if necessary.
• If the two letters are in different rows and different columns, they form a rectangle. Replace each letter with the letter in the same row but in the other corner of the rectangle.

Example:

``````  ubzos rxvlv ecvie teytn bvvq
ub zo sr xv lv ec vi et ey tn bv vq
pr og ra mx mi ng in ia va is fu nx``````

This is as far as you can get in decoding the message. The message is readable, with a little effort on the human's part.

Create a project named `PlayfairCipher`. Create a package named `playfair`. Get CipherExpert.zip, unzip it, and add the files to your `playfair` package. Use this as your skeleton to get started. Do not change the names or parameters of any of the provided method stubs!
Write a program to encipher and decipher messages using the Playfair cipher, using `CipherExpert` as your "main" class. Each provided method has Javadoc comments that tell you what the method is supposed to do.
Write JUnit tests for every method in each of the provided classes, except the `CipherExpert` class and the `printInBlocksOfFive` method in the `StringUtilities` class. Whichever of your team members writes a method should also write the unit tests for that method.
Write an `AllTests.java` test suite to run all your unit tests at once.