Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your task for this assignment is to write a Python3 program that: Reads in from

ID: 3585062 • Letter: Y

Question

Your task for this assignment is to write a Python3 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, thekeyword 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

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'

Explanation / Answer

def CipherTable(key):
    Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWYZ"
    keyString = ""

    for element in key:
        if (element not in keyString):
            keyString += element

    for element in Alphabet:
        if (element not in keyString):
            keyString += element

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

    return table


def EncryptionMessage(key, UserMessage):
    if ("X" in key):
        key = key.replace("X", "KS")

    if ("X" in UserMessage):
        UserMessage = UserMessage.replace("X", "KS")

    table = CipherTable(key)

    for element in UserMessage:
        if (ord(element) < 65 or ord(element) > 90):
            UserMessage = UserMessage.replace(element, "")

    EncriptionText = ""

    i = 0
    while (True):
        if (i + 2 > len(UserMessage)):
            break
        pair = UserMessage[i:i + 2]
        if (pair[0] == pair[1]):
            UserMessage = UserMessage[:i + 1] + "Q" + UserMessage[i + 1:]
        i += 2

    if (len(UserMessage) % 2 == 1):
        UserMessage += "Z"

    for i in range(0, len(UserMessage), 2):
        pair = UserMessage[i:i + 2]

        for i in range(5):
            for j in range(5):
                if (pair[0] == table[i][j]):
                    Letter1Index = [i, j]
                if (pair[1] == table[i][j]):
                    Letter2Index = [i, j]

        if (Letter1Index[0] == Letter2Index[0]):
            encrychar1 = table[Letter1Index[0]][(Letter1Index[1] + 1) % 5]
            encrychar2 = table[Letter2Index[0]][(Letter2Index[1] + 1) % 5]

        elif (Letter1Index[1] == Letter2Index[1]):
            encrychar1 = table[(Letter1Index[0] + 1) % 5][Letter1Index[1]]
            encrychar2 = table[(Letter2Index[0] + 1) % 5][Letter2Index[1]]

        else:
            encrychar1 = table[Letter2Index[0]][Letter1Index[1]]
            encrychar2 = table[Letter1Index[0]][Letter2Index[1]]

        EncriptionText += encrychar1 + encrychar2

    return EncriptionText


def main():
    print("Enter a list of keyword and message:")

    InputList = []
    while (True):
        UserInput = input().upper()
        if (UserInput == ""):
            break
        InputList.append(UserInput)

    for i in InputList:
        temp = i.split()
        if (len(temp) != 2):
            print("ERROR")
            continue

        keyword = temp[0]
        message = temp[1]

        print(EncryptionMessage(keyword, message))

    return


main()