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
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) )
Comments
Leave a comment