For instance, for a file where the content is ONKKB123 the output file content will be HELLO123 (using the default key).
thanks for the question, here is the complete python program. Let me know in case you have any doubts or question.
===========================================================================================
import string
# function that returns the dictionary given a
sentence
def substitution_code(sentence):
alphabets_only=''
for letter
in sentence.upper():
if
letter in string.ascii_uppercase:
alphabets_only+=letter
for uppercase_ascii
in range(65,91):
alphabets_only+=chr(uppercase_ascii)
unique_letter_list=[]
key=''
for letter
in alphabets_only:
if
letter not in unique_letter_list:
key+=letter
unique_letter_list.append(letter)
substitution_dict={}
for i in
range(0,26):
substitution_dict[chr(i+65)]=key[i]
return substitution_dict
# function reverses the key value to value key pair
def reverse_dict(key_dict):
reverse_dictionary={}
for key,value
in key_dict.items():
reverse_dictionary[value]=key
return reverse_dictionary
# function takes in inputfile, outputfile and
key_dictionary
# reads from the encrypted file and writes to the output file using
the key dictionary
def
decrypt(input_file_name,output_file_name,key_dict):
reverse_dictionary=reverse_dict(key_dict)
with
open(input_file_name,'r') as
infile:
with
open(output_file_name,'w') as
outfile:
for
line in infile.readlines():
write_line=''
for letter in line.upper():
if reverse_dictionary.get(letter) is
None:
write_line+=letter
else:write_line+=reverse_dictionary[letter]
print(write_line)
outfile.write(write_line)
def main():
key_dict=substitution_code('IT RAINS A
LOT IN MILWAUKEE IN THE WINTER, BUT SOMETIMES IT
SNOWS.')
input_file_name='D:\\plain_text.txt'
# encrypted file that needs to decrypted
output_file_name='D:\\decrypt.txt'
# output file which contain the original text
decrypt(input_file_name,output_file_name,key_dict)
main()
=====================================================================================
Get Answers For Free
Most questions answered within 1 hours.