Question

Using Python, Implement a decryption function: Implement a function named decrypt that opens a file named...

  1. Using Python, Implement a decryption function: Implement a function named decrypt that opens a file named input_file and decrypts its content using the opposite of the dictionary created using a key (which is a parameter of the function) and stores the decrypted file to a file named output_file. The input and output file names will be positional parameters. The key parameter should be a keyword-only optional parameter where the default value is “IT RAINS A LOT IN MILWAUKEE IN THE WINTER, BUT SOMETIMES IT SNOWS." This function should create the substition dictionary using the create_substition_dic() function and create the opposite of the substitution dictionary using the opposite() function then use this opposite dictionary to generate the decrypted text.

For instance, for a file where the content is ONKKB123 the output file content will be HELLO123 (using the default key).

Homework Answers

Answer #1

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

=====================================================================================

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please create a python module named homework.py and implement the functions outlined below. Below you will...
Please create a python module named homework.py and implement the functions outlined below. Below you will find an explanation for each function you need to implement. When you are done please upload the file homework.py to Grader Than. Please get started as soon as possible on this assignment. This assignment has many problems, it may take you longer than normal to complete this assignment. This assignment is supposed to test you on your understanding of reading and writing to a...
Curve-Fit Function USING MATLAB Using the top-down design approach, develop a MATLAB function A8P2RAlastname.m that reads...
Curve-Fit Function USING MATLAB Using the top-down design approach, develop a MATLAB function A8P2RAlastname.m that reads data from a file and performs regression analysis using polyfit and polyval. The function shall have the following features: The input arguments shall include the file name (string), a vector of integers for the degrees of polynomial fits to be determined, and an optional plot type specifier (‘m’ for multiple plots, ‘s’ for a single plot - default). The data files will be text...
PYTHON : Create a Email Address Parser (* Please do make comments*) Often times, you may...
PYTHON : Create a Email Address Parser (* Please do make comments*) Often times, you may be given a list of raw email addresses and be asked to generate meaningful information from such a list. This project involves parsing such a list and generating names and summary information from that list. The script, eparser.py, should: Open the file specified as the first argument to the script (see below) Read the file one line at a time (i.e., for line in...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT