Project P5

Wheat Square Cipher

The Wheat Square Cipher is a simple encryption technique for encoding plaintext messages.

The encoding scheme uses a keyword which in real life is known only to the correspondents.  The letters of the keyword are used to create a "code block," which is a 5 by 5 matrix of letters. Since the code block is limited to 25 letters, if the keyword contains the letter "J" it is changed into an "I" any place that it occurs.  The keyword is then written into the square by rows, omitting repeated letters.   Follow the keyword with the rest of the alphabet's letters in alphabetical order.  Each letter of the alphabet occurs once and only once in the code block. You may assume the keyword contains no blanks.
For example, the keyword "Villanova" would produce this code block:

V I L A N
O B C D E
F G H K M
P Q R S T
U W X Y Z
To encode a message, first prepare the message text as follows:
  1. Delete all blanks and punctuation, leaving only letters.
  2. Convert to uppercase.
  3. Convert any 'J' to 'I'.
  4. Anyplace there is a double letter (like "TT"), put an 'X' in between them (so "LETTER" becomes "LETXTER").   (If the repeated letters are themselves 'X', do not place an 'X' between them.) Notice that in step 3, 'JI' becomes 'II' which is considered a double letter for step 4.
  5. If there are an odd number of letters, add an 'X' at the end of the message.

The encoding process works like this:

  1. Take the next two letters of the message and find them in the code block.
  2. If they are not in the same row or in the same column, then they are at two corners of a rectangle; Replace each letter with the one in the same row that forms the other corner of the rectangle. For example,
    "AM" becomes "NK",
    "WD" becomes "YB",
    "SL" becomes "RA".
  3. If they are in the same row take the letter immediately to the right of each letter of the pair, unless the letter is the last one in the row, then take the first letter in the row. For example, "IA" becomes "LN" and "RT" becomes "SP".
  4. Otherwise, if they are in the same column take the letter immediately below each letter of the pair, unless the letter is the last one in the column, then take the first letter in the column. For example, "DS" becomes "KY", and "UO" becomes "VF".

Examples

Using the keyword "Villanova",

Amazingly few discotheques provide jukeboxes.

is encoded as:

NKNYLVHIUKBZBARDEPMCPWDTQSFOABBNYFOCCUDT

Another example - the keyword "Jackpot" would produce this code block:
I A C K P
O T B D E
F G H L M
N Q R S U
V W X Y Z
Then  "bonus" is encoded as: "DTQNRY".

Problem Requirements

Write a program that encodes messages using the Wheat Square Cipher. 

You must follow this Java class definition:

public class WheatSquareCipher
{
    /** Construct a cipher from a given keyword.
     * @param keyword the keyword to use for encoding the cipher.
     * @pre keyword contains no blanks.
     */
    public WheatSquareCipher(String keyword)

    /** Encode a plaintext message using the cipher.
     * @param message  a string of characters to be encoded.
     * @return String coded message
     */
    public String encode (String message)
}

Note: Please design your own solution. While this algorithm is easy to find on the Web or in textbooks, please write your own algorithm from scratch.  Do not use the internet or other resources for any purpose related to ciphers or encryption.  

Unit Testing

You must submit execution output that demonstrates that your program can produce the correct results for data in the "discotheques" example above.  This output can be created in one of two ways:

Perform any additional tests you want to convince yourself that your solution is correct.

Acceptance Testing

In addition, your program must pass the instructor's acceptance test.  Once you are satisfied that your program is correct and is passing your unit tests, create a new time log entry.  Enter "Test" for the phase and in the comment field enter "Acceptance Testing".  (You may use "Accept Test" for the phase if you prefer.  Do not count this phase as development time in your summary).

Submit your source code using the Web-CAT grader. On this project the grader will not run your own unit tests. Web-CAT will report checkstyle errors in red and they WILL count in your total score. Each coding standard violation is a defect. The defect type is 10 for code syntax, and 80 for Javadoc errors. (Tip: To avoid getting style errors in Web-CAT, run the Checkstyle extension in BlueJ before submitting to Web-CAT.)

If Web-CAT reports any errors, tally them in a new section of the defect tally without an inject phase, with a removal phase of "ACCEPTANCE TEST". Don't add them to Injected Phase or total on the Summary form. You are allowed five Web-CAT submissions without penalty.  If you take more than five submissions, your project earns only half-credit.

When Web-CAT assigns a 100% score to your work, you should finalize your work according to the assignment directions.