Question: The Playfair Cipher is an encryption technique that performs substitution operations on pairs of letters using a keyword or phrase, which is also used to

The Playfair Cipher is an encryption technique that performs substitution operations on pairs of letters using a keyword or phrase, which is also used to decrypt the message. It has the advantage of being harder to break than other simple substitution ciphers while being easier to use for encryption and decryption. In this assignment, we introduce a modified version of Playfair Cipher.

In order to use the Playfair cipher, you need a 5 by 5 key table generated using the keyword. To create the table, it is filled out row by row from left to right using the letters of the keyword, ignoring duplicate letters. Any remaining spaces are filled with the unused letters of the alphabet in order. Since there are 26 letters in the alphabet and only 25 spaces in the table, we will replace the letter "X" with "KS" in both key phrase and message. In the following example, we use the key-phrase "Playfair example (the letters that have already been used are shown in red).

To encrypt a message, one pair of letters is operated on at a time, adding a "Z to complete the last pair if necessary, applying the following four rules in order before moving on to the next pair:

If both letters are the same, insert a "Q after the first letter to form a new pair and continue. Note: if two adjacent letters are the same, but are not both in the current pair (ie, the current pair ends with the same letter that starts the next pair), it is not necessary to insert a 'Q'.

If both letters appear in the same row of the table, replace each letter with the one immediately right (wrapping around so that a letter on the right side of a row is replaced by the letter on the left side of the same row).

If both letters appear in the same column of the table, replace each letter with the one immediately below (wrapping around so that a letter on the bottom of a column is replaced by the letter on the top of the same column).

If the letters are in a different row and column, replace each letter by the one found in the same row (not column) but different corner of the rectangle defined by the original pair. The order is important here (see the example for clarification).

The letter "X" is replaced with "KS" in both key phrase and message (as you see the letter "X" in the above-mentioned key phrase "Playfair example" was replaced with "KS" illustrated with blue color).

As an example, to encrypt the message "Hide the gold in the tree stumps with the key "Playfair example, the following steps are performed:

Step

Message

Encrypted

1. The pair HI is in a column, replace it with TM

HIDETHEGOLDINTHETREESTUMPS

TM

2. The pair DE forms a rectangle, replace it with CK

HIDETHEGOLDINTHETREESTUMPS

TMCK

3. The pair TH is in a column, replace it with PT

HIDETHEGOLDINTHETREESTUMPS

TMCKPT

4. The pair EG forms a rectangle, replace it with SC

HIDETHEGOLDINTHETREESTUMPS

TMCKPTSC

5. The pair OL forms a rectangle, replace it with JY

HIDETHEGOLDINTHETREESTUMPS

TMCKPTSCJY

6. The pair DI forms a rectangle, replace it with MK

HIDETHEGOLDINTHETREESTUMPS

TMCKPTSCJYMK

7. The pair NT forms a rectangle, replace it with HV

HIDETHEGOLDINTHETREESTUMPS

TMCKPTSCJYMKHV

8. The pair HE forms a rectangle, replace it with NI

HIDETHEGOLDINTHETREESTUMPS

TMCKPTSCJYMKHVNI

9. The pair TR forms a rectangle, replace it with UI

HIDETHEGOLDINTHETREESTUMPS

TMCKPTSCJYMKHVNIUI

10. Insert Q to split EE, the pair EQ forms a rectangle, replace it with SN

HIDETHEGOLDINTHETREQESTUMPS

TMCKPTSCJYMKHVNIUISN

11. The pair ES is in a row, replace it with KI

HIDETHEGOLDINTHETREQESTUMPS

TMCKPTSCJYMKHVNIUISNKI

12. The pair TU is in a row, replace it with UV

HIDETHEGOLDINTHETREQESTUMPS

TMCKPTSCJYMKHVNIUISNKIUV

13. The pair MP is in a column, replace it with HI

HIDETHEGOLDINTHETREQESTUMPS

TMCKPTSCJYMKHVNIUISNKIUVHI

14. The letter S is by itself, adda Z. SZ is in a column, replace it with GF. HIDETHEGOLDINTHETREQESTUMPSZ TMCKPTSCJYMKHVNIUISNKIUVHIGF

Deliverables

Your task for this assignment is to write a Python program that:

Reads in from standard input (i.e. keyboard) a list of keyword and message pairs, one pair per line. The keyword and message are each a single word, separated by a space, all in uppercase.

The first line of input is a number n, followed by n lines. Each line may have two words, the keyword and the message.

If the line contains more (or less) than two strings, the program should output "ERROR" and continue to the next line.

For each keyword/message pair, the program creates a key table using the keyword and encrypts the message using the Playfair cipher, and outputs the encrypted message.

Sample Input/output

Input:

3

GUIDE UNIVERSITY

ENGLISH

KNOW ALBERTA

Output:

EKUWDSQEVZ

ERROR

FSCFMYFA

Submission Instructions

Please follow these instructions to correctly submit your solution.

Name your solution playfair.py

Put this file in a directory called as-175-1

Zip this directory so that you have a new file called as-175-1.zip

Upload your zipped assignment to the assignment page in eClass

If you have any questions, please post to the Discussion Forum or speak with a TA in a lab.

Advice

Late submissions will not be accepted. You are able to submit your work as often as you like and only the last submission will be marked. So submit early and submit often.

Please comply with the specification of how you should submit your file. Failure to comply with the submission instructions will result in mark deductions.

Hint

To represent the key table, you may want to use a list, where each of its five elements is a five letter string representing a row. An easy way to do this is to start with a twenty-five letter string, for example:

table = [keyString[i:i+5] for i in range(0, len(keyString), 5)]

will give the following result for the table:

['PLAYF', 'IREKS', 'MBCDG', 'HJNOQ', 'TUVWZ']

To access the letter in the ith row and jth column of the table, you can use table[i][j]. In our example, table[1][3] is 'K'.

keyString = 'PLAYFIREKSMBCDGHJNOQTUVWZ'

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!