Answer the question
In order to leave comments, you need to log in
Python problem - decryption and encryption?
python course assignment
Write a program that can encrypt and decrypt a substitution cipher. The program takes two strings of the same length as input, the first line contains the characters of the source alphabet, the second line contains the characters of the final alphabet, after which there is a string that needs to be encrypted with the passed key, and another line that needs to be decrypted.
Let, for example, the following input be passed to the program:
abcd
*d%#
abacabadaba
#*%*d*% This means that the character a of the original message is replaced by the character * in the cipher, b is replaced by d, c is replaced by % and d is replaced by #.
You need to encrypt the string abacabadaba and decrypt the string #*%*d*% using this cipher. We get the following lines, which we pass to the output of the program:
*d*%*d*#*d*
dacabac
in_chars = input()
out_chars = input()
message = input()
d_message = input()
for index, char in enumerate(in_chars):
message = message.replace(char, out_chars[index])
print (message)
for index, char in enumerate(out_chars):
d_message = d_message.replace(char, in_chars[index])
print (d_message)
Answer the question
In order to leave comments, you need to log in
in general, the solution turned out to be simpler:
alphabet, cipher, encode, decode = input(), input(), input(), input()
enc_dict = {letter: char for letter, char in zip(alphabet, cipher)}
dec_dict = {char: letter for char, letter in zip(cipher, alphabet)}
print("".join([enc_dict[letter] for letter in encode]))
print("".join([dec_dict[char] for char in decode]))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question