lab2.2

advertisement

Topic 2 - 2D Arrays Programming Project 2

Lab Name: The Playfair Cipher

Purpose: To implement a program that encrypts and decrypts messages using a Playfair cipher. To work with 2D arrays.

Required Files:

Introduction: This lab is based on a free response questions from the 2000 APCS exam. Ciphers are secret codes and procedures used to encrypt messages. The goal of encryption is to take a

"plain text" message and put it into a form that is not understandable or readable by any undesirable party that intercepts it. The hope is that only the intended receiver of the message will be able to decipher it since they know the cipher and read the message. If you have ever purchased something over the internet on a secure site, your personal information was encrypted.

A simple, but effective cipher was created by Sir Charles Wheatstone, who named it the

"Playfair" cipher after his friend Lyon Playfair. For more information on the history of this cipher see: www.pbs.org/wgbh/nova/decoding/playfair.html

A Playfair cipher consists of a 6 x 6 grid. The 26 capital letters and the 10 digits, 0 through 9, each appear once in the grid. Here is an example grid:

B O V P U E

6 5 H A 1 0

R W 2 Q G 7

I C X K L 9

4 S M Y D J

N 3 F 8 T Z

Here is an example of how to encrypt a message using this cipher. Take the following message:

"Meet me in Pasadena, CA at 1 pm on 7 October."

Convert all letters to upper case and discard any character that are not letters or digits. This results in the following message:

"MEETMEINPASADENACAAT1PMON7OCTOBER"

Break the message up into 2 character groups.

"ME ET ME IN PA SA DE NA CA AT 1P MO N7 OC TO BE R"

Topic 2 - 2D Arrays Programming Project 1

1

Topic 2 - 2D Arrays Programming Project 2

Encrypt each 2 letter combination. To do this find the letters on the grid. The two letters are the diagonals of a box.

B O V P U E

6 5 H A 1 0

R W 2 Q G 7

I C X K L 9

4 S M Y D J

N 3 F 8 T Z

Replace each letter with the letters that are the other diagonals of the box. Each letter from the original messages is replaced by the letter in the same row as it. Thus M is replaced by J and E is replaced by V . Continue to encrypt pairs of letters in this way.

Clear: "ME ET ME IN PA SA DE NA CA AT 1P MO N7 OC TO BE R"

Coded: "JV UZ JV NI AP Y5 JU 86 K5 18 AU SV ZR CO 3U EB R"

In the final message the spaces between pairs are removed.

Final coded message: "JVUZJVNIAPY5JU86K518AUSVZRCO3UEBR"

There are some special cases.

1.

Notice the 4 th pair of characters in the original message, IN . These two characters appear in the same column, number 0. They do not form the diagonals of a box. If two character in a pair are in the same row or same column, simply swap them.

2.

There is an extra character at the end of the message. If this occurs, the last character is unchanged.

3.

This is really just a version of special case 1, but it is possible to have a character pair made up of the same character. Consider the message "MESSAGE" . This breaks up into

"ME SS AG E" which encodes to "JV SS Q1 E" . Note, how the double SS is unchanged. A letter pair made up of two of the same character will not be altered in the encrypted message.

To decrypt an encoded message the process is simply reversed.

Problem Description: Implement a class with the following methods: public class Playfair

{ //Default constructor. Creates a random cipher. public Playfair()

/* pre: message != null post: returns an encrypted version of message based on this Playfair cipher.

*/ public String encryptMessage(String message)

Topic 2 - 2D Arrays Programming Project 1

2

Topic 2 - 2D Arrays Programming Project 2

/* pre: message != null, message was encrypted using this

Playfair cipher post: returns a decrypted version of message based on this Playfair cipher.

*/ public String decryptMessage(String message)

}

Hints:

1.

Use a 2D array of characters to hold the cipher. This will be an instance variable of the

Playfair class.

2.

Break the encrypt message process into steps. a.

First convert the message to all upper case and remove all non letter and non digit characters. b.

Create a private enocdeTwo method that takes in a String of length 2 and returns the encoded version of the original 2 characters. c.

Iterate through the version of the message created in step A encoding pairs of letters using your encodeTwo method.

3.

To decrypt the message following the simply iterate through the String and decode each pair of letters. Note, you can simply use the encodeTwo method to decode pairs of letters. The process is no different whether encoding or decoding a letter pair. Note, the decrypted message will not be exactly the same as the original message because there is no way to reinsert the characters that were removed from the original, mostly white space and punctuation.

Extras: Implement the following changes to the way the Playfair cipher works for extra credit.

1.

Handling characters that appear in the same row or column of the cipher grid. If a character pair is in the same row or column do not simply swap. Them. Instead if characters appear in the same row then encrypt each character by taking the character to the right of it in the row. Thus given the pair "HA", the character to the right of H is A and the character to the right of A is 1. The pair "DM" would encrypt to JY. Note, if a character is in the last column you must "wrap around" to the first column. So the pair

"BE" would encrypt to "OB". E, which is in the last column, wraps around to the first column to become B.

B O V P U E

6 5 H A 1 0

R W 2 Q G 7

I C X K L 9

4 S M Y D J

N 3 F 8 T Z

Likewise, encrypt characters in the same column by taking the character below it in the column. The pair "RI" would encrypt to "I4". The pair "OW" would encrypt to "5C". If this extra is completed the encodeTwo method will no longer work on decoding characters that appear in the same row or column.

2.

In the standard version of the assignment a message with an odd number of letters and digits it has an unencrypted character at the end. If this is the case pad the message with

Topic 2 - 2D Arrays Programming Project 1

3

Topic 2 - 2D Arrays Programming Project 2 an "X" at the end. In the example given the last character of the message was "R". In the standard version of the assignment the "R" is unencrypted. With this extra "R" would become "RX" and would encrypt to "2I"

3.

Another problem with the cipher is pairs that consist of the same character. The algorithm for the standard version of the assignment these pairs are sent unencrypted. If extra 1 is implemented pairs consisting of the same character are still sent as a pair of letters. For example "SS" are in the same row and the same column. Assume if this occurs the letter to the right is used. For example, "SS" becomes "MM" Pairs of letters give a clue to someone trying to decrypt a message. If a pair would consist of the same character break it up with an "X" or in the case of "XX" with a Z. Consider the following example.

Given the clear message:

"Messy room"

The normal procedure for creating pairs would yield:

"ME SS YR OO M"

With the new algorithm the "SS" pair is broken up with an "X". The message becomes:

"ME SX SY RO OM"

There is no need to break up the "OO" from the original message because the addition of an "X" to break up the "SS" has caused the Os to be placed in different pairs.

Topic 2 - 2D Arrays Programming Project 1

4

Download