Python cipher phrase decryption
1 min read

Python cipher phrase decryption

Create a python program which takes a cipher word as input and returns its decrypted phrase
Python cipher phrase decryption

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))

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×