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
Implement a python program in file named tarvel.py. Create an empty dictionary named responses. Implement while...
Implement a python program in file named tarvel.py. Create an empty dictionary named responses. Implement while loop to take in user's name and desired destination for as long as there are user inputs. Prompt user to input yes to continue and no to quit. Prompt for user's name. Receive the name into the program and save it as the value of name variable. Prompt user for their desired vacation destination. Receive response and save it as the value of a...
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...
Python (Using for reference please comment which is which!) Exercise 1: Store Unique Words as Dictionary...
Python (Using for reference please comment which is which!) Exercise 1: Store Unique Words as Dictionary Keys Write a program that stores unique words as keys in a dictionary. Don't worry about upper/lowercase for now (i.e., it's ok for you to have separate entries for "The" and "the"), and just assign 1 as the value for each key. Use an input loop (while loop with an input function) to accepts values putting them into the dictionary until the user enters...
WRITE IN PYTHON Using any large text file or any literature English book in .txt format....
WRITE IN PYTHON Using any large text file or any literature English book in .txt format. The program will read a .txt file and process the information Write a module called, “book_digest”. The module must have the following functions: ● digest_book(file_path: str) -> None ○ This function collects and stores the required information into global dictionaries, lists, and variables. The file (book) is read and parsed only one time then closed ○ This function should raise a FileNotFoundError exception if...
Using C++, write the following program: The Point.h file declares the class and you will create...
Using C++, write the following program: The Point.h file declares the class and you will create a Point.cpp that contains the implementation and a main() that instantiates the Point class to add additional tests to cover the various overloaded operators Use the Point.h file that is found below (This class contains a point on a plane and this class is going to contain a X coordinate and Y coordinate. The class is also going to contain a member function that...
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...
CSC 322 Systems Programming Fall 2019 Lab Assignment L1: Cipher-machine Due: Monday, September 23 1 Goal...
CSC 322 Systems Programming Fall 2019 Lab Assignment L1: Cipher-machine Due: Monday, September 23 1 Goal In the first lab, we will develop a system program (called cipher-machine) which can encrypt or decrypt a code using C language. It must be an errorless program, in which user repeatedly executes pre-defined commands and quits when he or she wants to exit. For C beginners, this project will be a good initiator to learn a new programming language. Students who already know...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT