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

PlainKey = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890.,\'\" EnigmaKey = \"T\'60US7,

ID: 3830041 • Letter: P

Question

PlainKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890.,'"

EnigmaKey = "T'60US7,ORW 89HC3YJMKEIB1QVDXFP52ZN4ALG."

Cypher = "OV HEUV6TMJGV'KMVOV6T9.MVUTMVTVI,H UVH9UL"

I have this program which decrypts Cypher using the character's index in EngimaKey and subsituting the character at the same index in PlainKey (in Python):

def findPos(string,c):
    for i in range(0, len(string)):
        if string[i] == c:
            return i
    return -1

def decrypt(cypher, plainkey, enigmakey):
    result = ""
    for i in range(0, len(cypher)):
        pos = findPos(enigmakey, cypher[i])
        if (pos != -1):
            result += plainkey[pos];
    return result;

def main():
    plainkey = "ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890.,'"
    enigmakey = "T'60US7,ORW 89HC3YJMKEIB1QVDXFP52ZN4ALG."

    cypher = "OV HEUV6TMJGV'KMVOV6T9.MVUTMVTVI,H UVH9UL"
    print("Cypher : " + cypher)
    print("Message: " + decrypt(cypher, plainkey, enigmakey))

main()

Now (still in Python) I want to use the same keys for translation, construct a dictionary where the key is the encrypted character, and the corresponding value is the plain text character. Basically I want this dictionary to perform the same decryption. Please Help! Thank You!

Explanation / Answer

def encrypt(text, encrypt_dict):
result = ""
for i in text:
result += encrypt_dict[i]
return result;

def decrypt(cypher, decrypt_dict):
result = ""
for i in cypher:
result += decrypt_dict[i]
return result;

def main():
plainKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890.,'"
enigmakey = "T'60US7,ORW 89HC3YJMKEIB1QVDXFP52ZN4ALG."
encrypt_dict = {}
decrypt_dict = {}
for i in range(0, len(plainKey)):
encrypt_dict[plainKey[i]] = enigmakey[i]
for i in range(0, len(enigmakey)):
decrypt_dict[enigmakey[i]] = plainKey[i]
cypher = "OV HEUV6TMJGV'KMVOV6T9.MVUTMVTVI,H UVH9UL"
print("Cypher : " + cypher)
print("Message: " + decrypt(cypher, decrypt_dict))

main()

# code:

def encrypt(text, encrypt_dict):
result = ""
for i in text:
result += encrypt_dict[i]
return result;

def decrypt(cypher, decrypt_dict):
result = ""
for i in cypher:
result += decrypt_dict[i]
return result;

def main():
plainKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890.,'"
enigmakey = "T'60US7,ORW 89HC3YJMKEIB1QVDXFP52ZN4ALG."
encrypt_dict = {}
decrypt_dict = {}
for i in range(0, len(plainKey)):
encrypt_dict[plainKey[i]] = enigmakey[i]
for i in range(0, len(enigmakey)):
decrypt_dict[enigmakey[i]] = plainKey[i]
cypher = "OV HEUV6TMJGV'KMVOV6T9.MVUTMVTVI,H UVH9UL"
print("Cypher : " + cypher)
print("Message: " + decrypt(cypher, decrypt_dict))

main()

# code: https://pastebin.com/F7jcfEqh