ITI 1220 Assignment 6

Available:  Friday October 28

Due:  Monday November 7, noon

Instructions

This assignment is to be done in TEAMS OF TWO PEOPLE.  The assignment should be submitted only once by a member of your team, but please ensure that the identification material contains the information for both team members.  Follow the assignment submission instructions and coding guidelines from the course lab manual.

For this assignment, some of the requirements for file names are changed.  This assignment involves creating several classes, and it is important that the classes and .class files have the correct names for the program to compile.  In this assignment, your programs should be in the files Caesar.java, Frequency.java, Vigenere.java, and A6Q4.java.

It is also REQUIRED that the method headers be used exactly as shown.  During the grading of this assignment, a series of JUnit tests may be run on your library classes to call and test the methods.  It is recommended that you set up your own set of JUnit tests to ensure that your classes work correctly.

Marking scheme:

·                    Regulations and standards: 10 marks

·                    Question 1: 20 marks

·                    Question 2: 10 marks

·                    Question 3: 40 marks

·                    Question 4: 20 marks

 

Question 1 (20 marks total)

A simple method of encoding messages is known as a "substitution cipher".  In this type of cipher, each character in a message is replaced by character determined through an encryption algorithm that includes a parameter called a “key”.

Normally, it is assumed that an unauthorized person who would attempt to read an encoded message knows the algorithm that is used to replace the characters, but does not know the particular value of the key – which is kept secret between the sender and the receiver of the message.

One of the simplest substitution ciphers is known as a Caesar cipher, as it was supposedly used by Julius Caesar to communicate secretly with his armies.  In this form of cipher, each letter of the alphabet is replaced by the letter that is key positions further along in the alphabet.  If we consider ‘a’ as character 0 up to ‘z’ as character 25, then a character ch is replaced by ( ch + key ) MOD 26.  For example, if key = 3:

a → d

b → e

c → f

w → z

x → a

y → b

z → c

To decode a message, each letter is shifted key positions backward in the alphabet.  Again, assuming key = 3:

a → x

b → y

c → z

d → a

e → b

f → c

z → w

 

For different values of the key, the second alphabet will be shifted by different amounts.

In this question, you will create a class called Caesar that will encode and decode strings using a Caesar cipher.

Part a) (10 marks)

Create a method in the class Caesar that will encode a message using a specified key that is in the range 0 through 25.  The header of the method should be as follows:

public static String encode( String message, int key )

The message may include spaces and punctuation characters, but only the lower case characters ‘a’ through ‘z’ should be encoded.

Part b) (10 marks)

Create a method in the class Caesar that will decode a message using a specified key that is in the range 0 through 25.  The header of the method should be as follows:

public static String decode( String codedText, int key )

The coded text may include spaces and punctuation characters, but only the lower case characters ‘a’ through ‘z’ should be decoded.

 

Question 2: (10 marks)

Messages encoded using Caesar ciphers are not very secure; the key can often be guessed easily.  It is well known that letters in text occur with varying frequency; in English ‘e’ and ‘t’ are very common letters, while ‘q’ and ‘z’ are used infrequently.

In a message encoded with a Caesar cipher, the same patterns will occur, but for different letters.  If letters most often appearing in a coded message are ‘j’ and ‘y’, one might guess that these letters represent ‘e’ and ‘t’ because these are the most common letters in plain English.  The evidence is particularly strong because ‘j’ and ‘y’ are the same distance apart in the alphabet as ‘e’ and ‘t’, but shifted 5 letters.

In a class called Frequency, write a method that will count the occurrences of each letter in a string representing a message.  The method should have the following header:

public static int[] createFrequencyTable( String text )

In the array that is returned, position 0 should have a count of the number of occurrences of the letter ‘a’ in the text, position 1 should have a count of the number of occurrences of ‘b’, and so on.

Only lower case letters in text should be counted; other characters should be ignored.

 

Question 3 (40 marks total)

A more secure method of encoding messages was proposed in the sixteenth century by Blaise de Vigenère, for use by the court of the king of France.  The idea is essentially to encode a message by using several Caesar ciphers in the same message, while still having a key that is easy to remember.  The Vigenère cipher was considered unbreakable for nearly 300 years until a method of cracking it was developed in 1863 by a Prussian army officer named Kasiski.

The Vigenère encoding method uses a key word or phrase.  Suppose that the key word is relations, and the message to be sent is to be or not to be that is the question. 

First, all of the spaces and non-letter characters are removed from both the key phrase and the original message.  The letters of the key word then are matched up with the letters of the message by repeating the key phrase as needed, as shown below. 

Message:  tobeornottobethatisthequestion

Key word: relationsrelationsrelationsrel

The key word letters are used to select different keys for Caesar encoding.  If the key word letter is ‘b’, the corresponding message letter is encoded using a Caesar cipher with key = 1; ‘c’ means key =2, and so on.  Key letter ‘a’ means a shift of 0.  Note that letters matching up with key letter ‘a’ remain unchanged.

 

For example, the first letter in the message is t.  The corresponding letter in the key word is r, which is the 18th letter of the alphabet.  This means that we are to use the Caesar cipher (but only for this letter) with key = 17.  Letter ‘t’ shifted forward 17 positions (mod 26) is ‘k’, and so this is the first letter of our encoded message.

The second letter in the message is o.  The corresponding letter in the key word is e, which is the 5th letter of the alphabet.  This means that we use the Caesar cipher with key = 4 for this letter.  The letter o shifted forward 4 positions is s, and so this becomes the second letter of the encoded message.

This continues until the end of the message.

 

Message:  tobeornottobethatisthequestion

Key word: relationsrelationsrelationsrel

Encoded:  ksmehzbblksmempogajxsejcsflzsy

Essentially, the key word selects a set of Caesar ciphers that are used for encoding the message.  Notice that the first letter t encoded with r becomes k, while the next occurrence of t is encoded by s and becomes l.  Therefore, one cannot use frequency analysis directly to read a message encoded with the Vigenère cipher, and this is why it is more secure.

The final message is broken up into five letter “words”, so that one cannot use the size of words to guess parts of the original message. 

Encoded message:  ksmeh zbblk smemp ogajx sejcs flzsy

 

The same process is used in reverse to decode the message.  However, this time, the Caesar shifts are backward in the alphabet instead of forwards.

Encoded:  ksmehzbblksmempogajxsejcsflzsy

Key word: relationsrelationsrelationsrel

Message:  tobeornottobethatisthequestion

For example, the code letter k is shifted backwards by 17 positions (key letter r represents a shift of 17) in the alphabet (modulo 26) to the letter t.

Part a) (5 marks)

Write a method in a class called Vigenere that will remove all characters from a string except for lower case letters ‘a’ through ‘z’.  The method header should be:

public static String compress( String text )

Part b) (5 marks)

Write a method in the Vigenere class that will take a string and convert it to five letter “words” by inserting spaces at appropriate points.  The text is assumed to consist of only lower case letters.

public static String fiveLetterWords( String text )

If the length of text is not divisible by 5, the last “word” will not have five letters.  There should be no extra space characters at the end of the returned string.

Part c) (15 marks)

Create a method in the class Vigenere that will encode a message using a specified key phrase.  The header of the method should be as follows:

public static String encode( String message, String key )

Both the message and the key may contain spaces and punctuation characters but will otherwise contain only the lower case characters ‘a’ through ‘z’.  The resulting encoded message should have all words to be five letters in length except for the final word.

Part d) (15 marks)

Create a method in the class Vigenere that will decode a message using a specified key phrase.  The header of the method should be as follows:

public static String decode( String codedText, String key )

Both the message and the key may contain spaces but will otherwise contain only the lower case characters ‘a’ through ‘z’.  The resulting decoded message should have all blanks and punctuation removed.

Question 4 (20 marks)

Write a main() method in class A6Q4 that will call methods in the classes Caesar, Frequency, and Vigenere to do the following:

 

1.              Ask the user to enter some text.

2.              Print the frequency table for the text.

3.              Ask the user for a Caesar cipher key (0 to 25).

4.              Print the encoded text.

5.              Print the frequency table for the encoded text.

6.              Print the result of decoding the encoded text (which should be the original text).

7.              Ask the user for a Vigenère key word.

8.              Encode the same text using the Vigenère cipher.

9.              Print the frequency table for the Vigenère encoded text.

10.          Decode the encoded text (this won’t have the spaces in the same places, but it should otherwise be the same words as the original text).