-2
message=str(input("Please enter a message :"))
dic={ "a":"i", "e":"o", "i":"u", "o":"a", "u":"e", "b":"m", "d":"t","g":"b","m":"d","t":"g","1":"5","3":"5","5":"9","7":"1","9":"3"}
decrypted= ""
encrypted = ""
for letter in message:
        if letter in dic:
                encrypted+=dic[letter]

        else:
                encrypted+=letter

I am doing an assignment to encrypt and decrypt the message. I know how to encrypt it using keys. But the problem is that I am struggling to decrypt it without using any library as it is in my restriction. So basically my question is how we can decrypt it without any library ?

Code_001
  • 13
  • 5

1 Answers1

0

just traverse the dict like this (assuming the current code is encrypting well) :

message=str(input("Please enter a message :"))

dic={ "a":"i", "e":"o", "i":"u", "o":"a", "u":"e", "b":"m", "d":"t","g":"b","m":"d","t":"g","1":"5","3":"5","5":"9","7":"1","9":"3"}

t_dic  = dict([item[::-1]for item in dic.items()]) # {'i': 'a', 'o': 'e', 'u': 'i', 'a': 'o', 'e': 'u', 'm': 'b', 't': 'd', 'b': 'g', 'd': 'm', 'g': 't', '5': '3', '9': '5', '1': '7', '3': '9'}
# or t_dic = {v:k for k,v in dic.items()}
for letter in message:
        if letter in dic:
                encrypted+=t_dic[letter]

        else:
                encrypted+=letter   
adir abargil
  • 5,495
  • 3
  • 19
  • 29