You are on page 1of 7

CIS 200: Project 7 (50 + 6 points)

Due Friday, Apr 14th by 11:59pm

Reminder: Programs submitted after the due date/time will be penalized 10% for each day the project is
late (not accepted after 3 days, i.e. Midnight, Mon, Apr 17 th)
Reminder: ALL projects are intended to be done individually. Do NOT share your code with anyone. If it is
not your work, dont submit it as yours. Refer to the policy within the course syllabus which states, If two (or
more) students are involved in ANY violation of this policy, at a minimum, ALL students involved receive a
zero for the assignment and the offense is officially reported to the KSU Honor Council.
Note: If you use code that YOU did not develop (i.e. from another text or found it on the internet) you must
cite your source in a comment above the line(s) of code that is not your own work, otherwise it will be
considered plagiarism (representing others' work, whether copyrighted or not, as one's own).

In cryptography, plaintext is the original message and ciphertext is the encrypted message. A cipher is an
algorithm that converts plaintext into ciphertext.

The four-square cipher is an encryption method that uses four matrices to convert pairs of plaintext. This
technique involves two keys that are used to encrypt messages and to decrypt messages. We use the two
keys to construct the 5x5 matrices plain1, plain2, cipher1, and cipher2 as follows:

1) Construct plain1 and plain2 by filling in all the letters in the alphabet one row at a time. To simplify,
skip Q since we only have 25 spaces. A different matrix could be created for plain2, but to keep it
simple, both plain1 and plain2 will look like this:


2) Construct cipher1 by first filling in the matrix with the unique letters from the first key. For example, if
the key is testkey, then fill in the key with the letters T, E, S, K, Y. Do not fill in repeated
characters from the key. Then fill the matrix with the remaining characters of the alphabet (again,
skipping Q). Do not repeat any characters that appeared in the key. So, for testkey, cipher1 looks
like this:

3) Construct cipher2 in the same manner as cipher1, except use the second key. So if the second key is
secondkey, then cipher2 looks like this:


Suppose we want to encrypt the string HELLO WORLD. First, divide the message into groups of two:
HE LL OW OR LD (ignore spaces). We will encrypt each group of two separately. (This is known as a
block cipher, which are usually much larger blocks)

To encrypt HE, find the row and column of H in plain1: row [1], column [2] and the row and column
of E in plain2: row [0], column [4].

The first letter in the encrypted string is at row [1] (from the first letter H) and column [4] (from the
second letter E) in cipher1 F. The second letter in the encrypted string is at row [0] (from letter 2) and
column [2] (from letter 1) in cipher2 C. Therefore, HE encrypted becomes FC.

When we repeat this process for every pair of characters in HELLO WORLD, we get the encrypted

Decryption works in the same manner as encryption but backwards. To decrypt FCHGHZHTJE, first
break the string up into groups of two FC HG HZ HT JE and decrypt each group separately.

To decrypt FC, find the row and column of F in cipher1: row [1], column [4] and find the row and
column of C in cipher2: row [0], column [2].

The first letter in the decrypted string is at row [1] and column [2] in plain1 H. The second letter in the
decrypted string is at row [0] and column [4] in plain2 E. Therefore, FC becomes HE.

When we repeat this process for every pair of characters in FCHGHZHTJE, we get the encrypted

Notice that it would be difficult for an outsider to decrypt this message without having the two keys to build
cipher1 and cipher2. This prevents outsiders who do not know the keys from decrypting messages.

Assignment Description:
In this project, you will implement the four-square cipher algorithm. You will ask the user for both keys and
whether they want to encrypt or decrypt. If they want to encrypt, ask for the original message and then
display the encrypted result. If they want to decrypt, ask for the encrypted message and then display the
decrypted result. Ignore all spaces (remove them from the input string) for both keys and the message. Your
program should work for upper and/or lowercase characters.
Implementation Requirements: Your program must implement the following four classes
Square (Model)
Cipher (Model)
IO (View)
Proj7 (Controller)
All of these classes have been started for you and are available for download from Canvas. You may
ADD additional methods to these classes, if desired, but ALL methods currently given in the starter files
MUST be defined and implemented in your project to receive full credit.

1) The Square (Model) class will represent one of the 5x5 matrices. It will have a 5x5 array of characters
as an instance variable. It will also have two constructors one for creating plain1 and plain2 that fills
the matrix with all the letters of the alphabet and one for creating cipher1 and cipher2 that takes the key
and initializes the matrix as discussed in the background section. Square will also contain the
following methods:
getChar take a row and column and return the character at that position in the matrix
getPos take a character and return an integer array that holds the row and column positions of
that character in the matrix
These 2 methods are suggested but not required. (There is a contains method in the
String class, but it requires a String as a parameter rather than a single char)
strContains take a string and char and return true if string contains the character (else false)
removeDups take a string, remove all duplicate letters and return the string w/o duplicates

2) The Cipher (Model) class will handle the encryption and decryption. It will contain four Square
objects as instance variables one each for plain1, plain2, cipher1, and cipher2. It will also have a
constructor that takes the two keys and creates the four Square objects. Finally, it will have the
following methods (Both encrypt and decrypt will use the four-square cipher algorithm):
encrypt takes a string message and returns the encrypted string
decrypt takes an encrypted string and returns the decrypted string

3) The IO (View) class handles all console input and output. IO will contain the following methods:
firstKey ask the user for the first key and return the result
secondKey ask the user for the second key and return the result
encryptOrDecrypt ask the user whether they want to encrypt or decrypt and return a char
(assume either e or d is enterederror checking is not required)
message ask the user for the message (encrypted or original) and return the result
printResults takes the encrypted message or decrypted message, and prints the results to the
printError takes an error message and prints it to the console
4) The Proj7 (controller) class should only contain a main method that will create an IO object and a
Cipher object. It will call methods in IO to get input and display results and will call methods in
Cipher to do the encryption and decryption.
Unexpected Cases: (See extra credit for further modifications)
The algorithm will not work in certain cases, such as when:
The user includes the letter Q in either key or in the original message (5 x 5 Matrix)
The original message has an odd number of characters, not including spaces. (Parsing/Encoding
TWO letters at a time)
If the user either includes a Q or uses an odd number of characters, you should display an appropriate
error message and exit without performing the encryption/decryption. You do not need to check any other
error conditions, but you are welcome to add additional error checks if you want.
Traditionally, data validation goes in the MODEL that is storing the data. For this Project, you can choose
were data validation is implemented.
While you can assume only characters are entered for the keys or messages, your program will not make
any assumptions about spaces or the capitalizations of characters in the keys or messages. (The easiest way
to handle this is to remove all spaces and convert all words to uppercase before encrypting or decrypting. I
might suggest adding a removeSpaces method in your Cipher class)

Here are examples of encrypting and decrypting with the keys testkey and secondkey and the original
message Hello world. (This was originally Project 8, but is Project 7 for this section)
When encrypting or decrypting, keys and messages can be entered in upper and/or lower case, with or
without spaces.

Reminder that Q is not allowed in keys or messages nor uneven number of characters, once all spaces are
removed. Screenshots of this test case are not shown.

You must put a description of the project at the top of the file and at the top of each method.

Please use this template for the top of the file:

* (description of the project)
* @author (your name)
* @version (which number project this is)

Please use this template for the top of each method:

* (description of the method)
* @param (describe first parameter)
* @param (describe second parameter)
* (list all parameters, one per line)
* @return (describe what is being returned)

Important: ALL files must compile at the command-line with the statement: javac
Project must run at the command-line with the statement: java Proj7.
Make sure and test this before submitting. Submit ALL needed files and submit the CORRECT files (i.e.
the ones that compile). It is very important that you learn to submit what is needed to your client.
Extra Credit (+6 / +12%): Make the following modifications to add the functionally to Project 7. You will
submit a SINGLE version of this Project to Canvas. If doing the extra credit, simply add the
functionality (i.e. do not submit 4 files for the Project and separate files for the Extra Credit.) You can
do any or all extra credit below. Please indicate when you submit the assignment if you did the extra credit
(#1, #2, #3 or all) so that the GTA knows to test your program for these options.
1) (+2) Allow ALL letters of the alphabet in the original message, including Q and distinguish between
upper/lowercase. This will increase the size of your matrices. Remove the error message from the
original program. Decrypted message should include proper upper/lowercase distinction (i.e. John Q
Public decrypted would be JohnQPublic)
2) (+2) Allow original messages with any number of characters, even or odd and allow spaces. Remove
the error message from the original program. Decrypted message should include proper spacing (Hello
to all of you decrypted should be Hello to all of you)
3) (+2) Instead of using the same matrix for plain1 and plain2, create a different matrix for plain2 and use
in your encryption/decryption.

Submission read these instructions carefully or you may lose points

To submit your project, first create a folder called Proj7 and move your FOUR completed files into that
folder. Then, right-click on that folder and select Send To->Compressed (zipped) folder. This will create
Log-in to Canvas and upload your file. Only a .zip file will be accepted for this assignment in
Canvas. Put your full name and Project 7 in the comments box. If you did either parts of the extra
credit (or both) please indicate this in your comments so GTA will grade and give you credit.
Important: It is the students responsibility to verify that ALL correct files are properly submitted. If
ALL correct files are not submitted, they will not be accepted after the 3-day late periodno exceptions.
Grading: Programs that do not compile will receive a grade of 0. Programs that do compile will be
graded according to the following rubric:
You must submit all 4 classes to be considered for partial credit. For example, submitting only the IO
class would not earn you any points.
Requirement Points
Proj7 class (Code) (7 pts.) -7-
Should contains ONLY a main method. Contains NO print or input statements. Properly creates 6
objects of the Cipher and IO classes and properly utilizes the methods defined in these TWO
Documentation updated 1

Square class (Code) (7 pts.) -7-

2 constructors that properly create the matrices 2
Properly defines getChar and getPos methods (suggested but not required strContains and 4
Documentation updated 1

Cipher class (Code) (7 pts.) -7-

Properly creates four Square objects in constructor 2
Properly defines encrypt and decrypt methods 4
Documentation updated 1

IO class (Code) contains only the Input-Output portion of the program. (7 pts.) -7-
Properly defines the 6 given methods in the provided file 6
Documentation updated 1

Execution (22 pts.) -22-

Read in first and second key plus the original message and correctly ENCRYPTS / works with 10
both upper and/or lowercase keys and messages with or without spaces
Read in first and second key plus the encrypted message and correctly DECRYPTS / works with 10
both upper and/or lowercase keys and messages with or without spaces
Produces an error message if message contains a Q or an odd number of characters (no error msg 2
if doing the extra credit)

Extra Credit (+6 pts. / +12%) Can do any or all extra credit
Allows the character Q in the original message and distinguish between upper/lowercase. (must +2
correctly encrypt/decrypt for credit, distinguishing between upper/lowercase)
Allows odd or even number of chars AND spaces in original msg (must correctly encrypt/decrypt +2
correctly for credit, including spaces)
Create a different matrix for plain2 and use in your encryption/decryption (must correctly +2
encrypt/decrypt for credit)

Minus Late Penalty (10% per day)

Total 50+6