Objectives
To gain experience
- using the Scanner object for console input
- using System.out for console output
- writing a class in Java
- using top-down design
- with 2-D arrays
Description
In the modern world of computers and the internet, encrypting (and then decrypting) messages is of
vital importance to insure the privacy of the message's content. Rulers and military leaders have been
encrypting messages since before the time of Julius Caesar who used a simple substitution cipher.
Today computers use cipher keys which are hundreds of bits long. In this project you will implement a cipher that is somewhere in-between.
The Wheatstone-Playfair cipher (or simply the Playfair cipher) was invented in 1854 by Charles Wheatstone
and was popularized by his friend Lord Playfair. The Playfair cipher uses a keyword or phrase and a 5 x 5 matrix
to encrypt a message two characters at a time. There are several variations of the Playfair cipher due to
the need to handle special cases involving double letters (e.g. LL), messages of odd length, and whichever
letter is not present in the matrix. A Google search for "Playfair cipher" will find many pages
that describe the cipher and give examples. You will be implementing the CMSC 202 version of the
Playfair cipher as described below.
Using the Playfair Cipher
To use the Playfair cipher, a keyword or phrase is used to initialize a 5 x 5 key table.
To generate the key table, first fill in the spaces in the table (left-to-right, top-to-bottom) with the letters
of the keyword or phrase (dropping any duplicate letters or spaces), then fill the remaining spaces (again left-to-right, top-to-bottom) with the rest of the letters of the alphabet in order (omitting 'X' to reduce the alphabet to fit).
To encrypt a message, remove the spaces, change the message to upper-case, and if the message has odd length, append an 'X' to the end.
For example, "HelloWorld" becomes "HELLOWORLD", and "Hi There Mary" becomes "HITHEREMARYX".
The message is encrypted 2 letters at a time. Find the 2 letters to be encrypted in the key table and then apply one of the following rules.
- If both letters are the same or one of them is an 'X', don't change them.
- If the letters appear on the same row of your table, replace them with the letters to their immediate right respectively (wrapping around to the first column of the row if a letter in the original pair was in last column of the row).
- If the letters appear on the same column of your table, replace them with the letters immediately below respectively (wrapping around to the top row of the column if a letter in the original pair was in the bottom row of the column).
- If letters are neither in the same row nor the same column then replace the first letter,
by looking along its row until you reach the column containing the second letter; the letter at this intersection replaces the first letter. To encode the second letter, look along its row
until you reach the column containing the first letter; the letter at the intersection replaces the second letter.
To decrypt, use the inverse of these 4 rules.
An example of using the cipher is provided.
Your program will have the following interaction with the user
- Prompt the user for a keyword/phrase with which to initialize the key table.
- Ask the user if he would like to encode or decode a message
- Input the message to be encoded or decoded
- Output the encoded or decoded message
Project Requirements and Specification
- The package name for this project should be proj1.
- The user interface class (that contains main) must be called Project1 and the Playfair Ciphor must be in a class named PlayfairCipher.
- In keeping with the principle of separation of user interface and applications code, the Encryptor class must NOT perform any user input or output. All user input/output should be performed by main() or one of main's helper methods.
- The user may input the keyword/phrase in any combination of upper- and lower-case letters.
Since a phrase may be used, the user's input may contain spaces.
- In the general case the keyword/phrase may contain duplicate letters (only the first appearance of the duplicate is used). To make this project a little easier, we guarantee
that the keyword/phrase will not contain duplicates and will not contain the letter 'X'.
- When asked whether the user wants to encode or decode a message, the user will respond with
either "Encode" or "Decode" in any combination of upper- and lower-case letters.
- The message to be encoded or decoded may be in any combination of upper- and lower-case letters,
is guaranteed to contain only alphabetic characters and spaces, but may be of any length.
- Encoded and decoded messages must be output as digraphs (2-character groups), separated
by a space, 10 digraphs per line.
- Project1.java may only contain 1 PlayfairCipher Object.
Project Hints and Notes
- Use the Scanner class for user input.
To have access to the class definition for Scanner, your code must include the statement
import java.util.Scanner;
- Since main is by definition a static method, all methods and constants that are also defined within the
Project1 class and used by main must also be defined as
static. Variables defined within main
are of course local variables, so this requirement does not apply to them.
- Subtracting 'A' from a character variable is possible and results in the char's index into
the alphabet.
E.g. if char myChar = 'D';
then the expression mychar - 'A'
evaluates to 3.
- To help with debugging you may find it useful to write methods that prints
the contents of the key table so that you can manually encode and decode messages.
- Complete the encoding code first. The code required to decode a message requires only a few changes to the code required for encoding
- This project requires an estimated 200 - 250 lines of code, not counting comments. Don't procrastinate.
- A discussion board on Blackboard has been created for this project to get clarification on
project requirements, hints, etc. DO NOT post code on the discussion board. Instructors will be monitoring the forum and any inappropriate posts will be deleted.
Please enter the cipher keyword/phrase: computer
Encode or Decode?encode
Enter message: now is the time
KP ZG CD FA BD OR
Please enter the cipher keyword/phrase: computer
Encode or Decode?encode
Enter message: computer science is fun
OM PU CB RA CT FB JP BF VD PQ
Please enter the cipher keyword/phrase: computer
Encode or Decode?decode
Enter message: OM PU CB RA CT FB JP BF VD PQ
CO MP UT ER SC IE NC EI SF UN
Please enter the cipher keyword/phrase: jumble
Encode or Decode?encode
Enter message: CO MP UT ER SC IE NC EI SF UN
EQ UQ JV DO QF GC IF CG ZN LH
Grading
See the course website for a description of
how your project will be graded.
Project Submission
Before submitting your project, be sure to compile and test your code on the GL system.
See the
Project Compiling section of the course projects page
for details on how to run and execute your project on GL.
Assuming you've used the recommended file name, then to submit your project, type the command
submit cs202s10 Proj1 Project1.java PlayfairCipher.java
See the
Project Submission page for detailed instructions.
You may resubmit your files as often as you like, but only the last submittal will be graded and
will be used to determine if your project is late. For more information,
see the projects page on the course web site.
More complete documentation for submit and related commands can be found
here.
Remember -- if you make any change to your program, no matter how
insignificant it may seem, you should recompile and retest your program before
submitting it. Even the smallest typo can cause errors and a reduction
in your grade.