CMSC 202 Project 1
The Playfair Cipher
Assigned Mon Feb 11
Program Due 9:00AM Mon Feb 25
Design Due 9:00AM Mon Feb 18
Weight 6%
Updates 13 Feb 2008
Please be sure to include the statement package proj1; as the first line of your Project1.java file. The Project1.java file in Mr. Frey's public directory has this statement included already. We'll talk more about Java "packages" as the course progresses.
Objectives
To gain experience
  1. using the Scanner object for console input
  2. using System.out for console output
  3. writing methods in Java
  4. using objects/classes written by other programmers
  5. using top-down design
  6. 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.

  1. If both letters are the same or one of them is an 'X', don't change them.
  2. 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).
  3. 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).
  4. 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

  1. Prompt the user for a keyword/phrase with which to initialize the key table.
  2. Ask the user if he would like to encode or decode a message
  3. Input the message to be encoded or decoded
  4. Output the encoded or decoded message

Project Policy
This project is considered an OPEN project. Please review the open project policy before beginning your project.
Project Requirements and Specification
  1. 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.
  2. 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'.
  3. 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.
  4. 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.
  5. Encoded and decoded messages must be output as digraphs (2-character groups), separated by a space, 10 digraphs per line.
  6. This project does not require that you define any class, only that you use objects from the Java library (e.g. Scanner). No class definitions (other than Project1 ) are permitted for this project. Your task is to write the necessary helper methods required by main using good top-down design techniques.
Project Hints and Notes
  1. Mr. Frey's public directory for this project is /afs/umbc.edu/users/f/r/frey/pub/202/Proj1
  2. 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;
    
  3. A skeleton of Project1.java is available in Mr. Frey's public directory. Feel free to copy this file and use it as a starting point for your project. This file can be imported into Eclipse, or simply use cut/paste to copy its contents.
  4. 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.
  5. 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.
  6. To help with debugging you may find it useful to write a temporary method that prints the contents of the key table so that you can manually encode and decode messages
  7. Complete the encoding code first. The code required to decode a message requires only a few changes to the code required for encoding
  8. This project requires 200 - 250 lines of code, not counting comments. Don't procrastinate.
  9. 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.

Sample Output

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 

Project Design

Copy the file p1design.txt from Mr. Frey's public directory /afs/umbc.edu/users/f/r/frey/pub/202/Proj1 then edit the file as indicated. Submit this file by the project design due date.

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 cs202 Proj1 Project1.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.

Sorry! We could not find what you were looking for

Try going back to the main page here

 
4
4
0
0
0
0