Goals In this assignment you will get experience using some simple data encryption techniques. To complete this assignment, you will need to read and write files, write functions, manipulate ASCII characters, and use collections such as list and dictionary. You are not allowed to use any global variables in your program solution for this assignment. Overview The project folder contains a text file named const.txt. This file contains the text of the United States Constitution. The main objective of this project is to write a program that will encrypt and decrypt the constitution. More specifically you will complete the following steps: Step 1. read the constitution from const.txt and gather information needed to generate an encryption key Step 2. generate an encryption key and store it in a dictionary Step 3. read the constitution again from const.txt and use the encryption key to encrypt the constitution and write the encrypted version to a file named encrypt.txt. Step 4. write the encryption key to a file named key.dat Step 5. read the encryption key from file key.dat, create a decryption key, and store the decryption key in a dictionary Step 6. read the file encrypt.txt, use the decryption key to decrypt the contents of the file, and write the decrypted text to a file name decrypted.txt The contents of the file decrypted.txt must be identical to the contents of const.txt. Each of these steps must be accomplished by a unique function in your program. You may also add additional functions if you want. Details of Each Step Step 1 Create function def read_characters(in_file): # returns the list of unique characters found in in_file This function will read every character from const.txt and store a single occurrence of each character in a list. After all the unique characters are put in a list, sort the list. The function will return this sorted list. The filename is a parameter to this function. CSCI 120 Computer Science 1 Fall 2016 #page You are storing every character the occurs in the input file in this list, including printable and not-printable. That includes upper and lower case characters (these are distinct), blank spaces, tab, end-of-line, punctuation, digits, etc. Any and all ASCII characters that are in the input file will be put into the list of characters you are creating. Step 2 Create function def create_key(list): # create and return the encryption key dictionary This function will receive the sorted list of characters generated in Step 1 and will return a dictionary that contains an encryption key. To accomplish this, you will need to generate a random permutation of the sorted list of characters. The following code snippet shows how a random permutation of a list of character can be generated. The list permutation is a reordering of the characters in data. Your dictionary will use the characters in data as the key value and will map the ith character in data to the ith character in permutation. import numpy data = [“t”, “n”, “4”, “B”, “a”] permutation = [] permutation = numpy.random.permutation(data) print(data) print(permutation) Step 3 Create function def encrypt(in_file, out_file, encrypt_dict) # encrypt the text in in_file using encrypt_dict and write encrypted text to out_file, This function will receive two filenames and the dictionary created in Step 2 as parameters. One of the filenames is the name of the file that contains the text to be encrypted. The other filename contains the name of the file that encrypted text will be written. There is no return value from this function. For each character in the input file the function will use the character as a key and write to the output file the associated character stored in the encryption dictionary. CSCI 120 Computer Science 1 Fall 2016 #page Step 4 Create function def store_encrypt_dict(out_file, encrypt_dict) # stores the encryption dictionary into a comma delimited out_file This function will receive a filename and the dictionary used in Step 3 as parameters. The function has no return value. The function will write the dictionary to the file. Each line of the file is to contain a key:data character pair from the dictionary separated by a comma. This is a little trickier than it may seem. Since we are using all ASCII characters in the encryption, you will need to convert each character to its ASCII index using the ord() function. Suppose your dictionary has the following entries: {‘A’: ‘t’, ‘n’: ‘4’} Then the output file will contain the following data: 65,9 10,52 Note that ord(‘A’) is 65, ord(‘t’) is 9, ord(‘n’) is 10, and ord(‘’4′) is 52. Step 5 Create function def invert_dict(in_file) # read the encryption key in in_file and return an inverted decryption key as a dictionary This function will have a filename as a parameter and will return a dictionary. The filename is the name of the file that contains the encryption key. The function will read the encryption key from the file and create a decryption key in a dictionary. The decryption key is the inverse of the encryption key. So if ‘A’:’t’ was in the encryption key, then ‘t’:’A’ is in the decryption key. Since the ordinal values of the characters in the encryption key were written to the file, the function will need to get the associated character for each value. The char() function will do this as the following two examples show. char(65) returns ‘A’ char(9) returns ‘t’ CSCI 120 Computer Science 1 Fall 2016 #page Step 6 Create function def decrypt(in_file, out_file, decrypt_dict) # decrypt the text in in_file using decrypt_dict and store the result in out_file This function will receive two filenames and a dictionary as parameters. It has no return value. One filename is the name of the file containing encrypted text. The second filename is the name of a file where the decrypted text will be written. The dictionary parameter is the decryption key produced by Step 5. The function will read the text in the encrypted file and write the decrypted text to the file containing the decrypted text. If done correctly, the file containing the decrypted text should be identical to the originally encrypted file. If you are clever, this should be an extremely short and easy function to write!
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more