Python cipher phrase decryption
Create a python program which takes a cipher word as input and returns its decrypted phrase
You have the following cipher letters with their respective keys
cipher = ['u', 'l', 'v', 'w', 'x', 'k', 'y', 'z', 'a', 'b', 'c', 'f', '!', '9', '8', '7', '6', '5', '4', '3', '2', '1', '0']
keys = ['a', 'g', 'y', 'c', 'm', 'n', 'p', 't', 'u', 'h', 'o', 'r', ' ', '1', '2', '3', '4', '5', '6', '7', '8', '0', '1']
Implement a function decrypt() that takes a cipher phrase or string as an input parameter. This function will decrypt the cipher string and return the decryption.
def decrypt(string):
return descryptedstring
You should also implement the following helper functions:
- The function getIndex( ) takes as parameter a character at a time from the cipher phrase and returns its index in the cipher list. If the character isn’t found in the cipher list, the function returns -1.
def getIndex(c):
return index
- The function getChar( ) takes as parameter a character at a time and returns the corresponding character from the keys list or otherwise ‘#’ if the character isn’t found in the keys list.
def getChar(c):
return something
Sample Input and output :
>>> Enter the phrase to decrypt: wxyaz!919
The decryption: cmput 101
>>> Enter the phrase to decrypt: yux!456
The decryption: pam 654
>>> Enter the phrase to decrypt: u!yvzbck!yfclfux
The decryption: a python program
>>> Enter the phrase to decrypt: mnop999
The decryption: ####111
Solution in Python3
cipher = ['u', 'l', 'v', 'w', 'x', 'k', 'y', 'z', 'a', 'b', 'c', 'f', '!', '9', '8', '7', '6', '5', '4', '3', '2', '1', '0']
keys = ['a', 'g', 'y', 'c', 'm', 'n', 'p', 't', 'u', 'h', 'o', 'r', ' ', '1', '2', '3', '4', '5', '6', '7', '8', '0', '1']
def getIndex(c):
try:
return cipher.index(c)
except ValueError:
return -1
def getChar(c):
index = getIndex(c)
if index==-1:
return "#"
else:
return keys[index]
def decrypt(string):
decrypted = ""
for i in string:
decrypted+=getChar(i)
return decrypted
text = input("Enter the phrase to decrypt: ")
print("The decryption:", decrypt(text))