Answer to Question #346019 in Python for Chandu

Question #346019

Ajith is a raw agent and he is going on a secret mission.He needs your help but you have yo learn how to encode a secret message yo communicate safely with other agents




1
Expert's answer
2022-05-30T15:14:50-0400
def cipherDictionary(key):  # case sensetive version
    alphabet = "ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,.-!?"
    cipherDict = dict()
    cipherDict[" "] = " " #remain spaces
    added = set()
    i = 0
    for letter in alphabet:
        index = ord(key[i])%len(alphabet)   # ord() gets the ASCII number of a letter in key
        while(index in added):
            index = ( index+5 )%len(alphabet)
        added.add(index)
        cipherDict[letter] = alphabet[index]
        i = (i+1)%len(key)  #move to next letter in key
    return cipherDict
    
def encrypt(text, key):
    cipher = cipherDictionary(key)
    encrypted = ""
    for i in text:
        encrypted+=(cipher[i])
    return encrypted


def decrypt(text, key):
    decipher = {v: k for k, v in cipherDictionary(key).items()}
    decrypted = ""
    for i in text:
        decrypted+=(decipher[i])
    return decrypted


while True:
    print("Encrypt - 1, dectypt - 2, exit - q: ")
    mode = input()
    if(mode == 'q'):
        break
    if(mode!='1'and mode!='2'):
        continue


    print("Enter the message: ")
    message = input()
    print("Enter the keywoard: ")
    keyword = input()
    if (mode == '1'):
        print(encrypt(message,keyword) )
    elif (mode == '2'):
        print(decrypt(message,keyword) )

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS