In: Computer Science
Using the provided dictionary, develop an encryption algorithm that will convert a user-entered string into an encrypted string. Print the user inputted text and the corresponding encrypted text.
cipher = {"A": "T", "B": "D", "C": "L","D": "O", "E": "F","F": "A", \ "G": "G","H": "J", "I": "K", "J": "R", "K": "I","L": "C", "M": "V", \ "N": "P", "O": "W","P": "U", "Q": "X", "R": "Y", "S": "B","T": "E", \ "U": "Z", "V": "Q", "W": "S","X": "N", "Y": "M", "Z": "H"}
b) Create a corresponding decryption algorithm that utilizes the same dictionary. Print the user inputted encryption text and the corresponding decrypted text.
could I recieve help on this python problem thanks!
Part a Code for encryption:
# cipher dictionary used for encryption
cipher = {"A": "T", "B": "D", "C": "L","D": "O", "E": "F","F": "A", \
"G": "G","H": "J", "I": "K", "J": "R", "K": "I","L": "C", "M": "V", \
"N": "P", "O": "W","P": "U", "Q": "X", "R": "Y", "S": "B","T": "E", \
"U": "Z", "V": "Q", "W": "S","X": "N", "Y": "M", "Z": "H"}
# function that performs encryption
def encrypt(text):
res = ""
#find encrypted char for each char in input and add to result
for i in text:
res = res + cipher[i]
# return encrypted text
return res
print("Enter the text to be encrypted: ",end="")
text = input()
encrypted = encrypt(text)
print("Input text is : ",text)
print("Encrypted text is : ",encrypted)
Code screenshot:
Code output:
==========================
Part B) code for decryption:
# cipher dictionary used for encryption
cipher = {"A": "T", "B": "D", "C": "L","D": "O", "E": "F","F": "A", \
"G": "G","H": "J", "I": "K", "J": "R", "K": "I","L": "C", "M": "V", \
"N": "P", "O": "W","P": "U", "Q": "X", "R": "Y", "S": "B","T": "E", \
"U": "Z", "V": "Q", "W": "S","X": "N", "Y": "M", "Z": "H"}
# function to find key associated with given value in cipher
def get_key(val):
for key, value in cipher.items():
if(value == val):
return key
# function that performs encryption
def decrypt(text):
res = ""
# for each char find the corresponding decrpyted char and add to result
for i in text:
res = res + get_key(i)
return res
# take user input and perform decryption
print("Enter the text to be decrypted: ",end="")
text = input()
decrypted = decrypt(text)
print("Input text is : ",text)
print("Decrypted text is : ",decrypted)
Code Screenshot:
Code output:
====================
Proper comments and screenshots have been added to help you understand