Question: Please use python to do this files are in this link: https://drive.google.com/file/d/0BzoFdHKM0HX5RjZjR1ViSC16XzA/view?usp=sharing CMPUT 175 LAB 04 You must complete a program the reads a

Please use python to do this

files are in this link: https://drive.google.com/file/d/0BzoFdHKM0HX5RjZjR1ViSC16XzA/view?usp=sharing

""" CMPUT 175 LAB 04

You must complete a program the reads a file. It must then encrypt the file using a combination of the book cipher and substitution cipher. If a word in the file exists in the book provided, you must use the book cipher otherwise use the substitution cipher. The key for the cipher is produced using a password that should also be provided by the user. The encrypted text must then be written to file. Decryption is done in a similar way and the decrypted text must also be written to file.

For example, if the file to be encrypted is sample.txt, your code should produce the files sample.txt.enc and sample.txt.dec containing the encrypted and decrypted texts respectively.

You are provided with five (5) empty functions. You should write code inside these functions so that they perform as required. Each function also contains instructions in the form of comments on the how to complete them. Please read the comments carefully.

The functions have a default return value so that the main runs without error without the functions being implemented. Be sure to change the return values.

The subcipher module is imported for you. You should briefly go through this file to become familiar with how the functions work.

* Note that you are NOT allowed to import any other modules. * Note that you need to CLOSE files after reading/writing. """

import subcipher import os

bookname = "wordlist.txt"

def get_file_key():

""" This function should prompt the user to input a filename and a password. The prompt for file name should repeat as long as the user does not enter a valid file name.

The function should then use the password and the subcipher module to produce a key. You can find the appropriate function the subcipher module that will do the task

Return: fname - the file name entered by the user Return: key - key produced by the subcipher module using the password """

return "filename", "key"

def book_cipher_encrypt(word):

""" This function should take a word and return the appropriate line number from the book given in bookname. That is, if word appears in bookname, then the function should return the line number it appears in. It should return an empty string otherwise.

For example, in the file at bookname, wordlist.txt, the word 'computer' is at line 6914. So calling book_cipher_encrypt('computer') would return 6914. Remember, line numbers start at 1.

Parameter: word - a string, the word to be encrypted

Return: a string, the line number if the word exists in bookname or an empty string """

return ""

def book_cipher_decrypt(line_number):

""" This function should take a line number and return the word from the book given in bookname at that line number.

Parameter: line_number - a string, the line number of the word to retrieve

Return: a string, the word in bookname at the line_number """

return "word"

def encrypt_file(file_name, key):

""" This function should read the contents of the file specified in file_name. Then it should encrypt each word of the file. If the word exists in bookname, then the encryption scheme to be used is the book cipher. Otherwise use the substitution cipher from the subcipher module. This function need not return any values.

Parameter: file_name - a string, the name of the file to be read Parameter: key - a string, the key to be used for the substitution cipher """

pass

def decrypt_file(file_name, key):

""" This function should read the contents of the file specified in file_name. Then it should decrypt each word of the file. If the word is a line number, then the decryption scheme to be used is the book cipher decryption. Otherwise use the substitution decipher from the subcipher module. This function need not return any values.

Parameter: file_name - a string, the name of the file to be read Parameter: key - a string, the key to be used for the substitution decipher """

pass

if __name__ == "__main__":

""" The main code here reads a file name and password from the user and gets a key using the password. Then the contents of the file is encrypted and decrypted into the appropriate output folders.

You may comment out these lines to test your code in parts if you want to. However, this specific block of code must run correctly during your demo.

You are also provided a set of sample files to test your program. """

fname, key = get_file_key() encrypt_file(fname, key); decrypt_file(fname + '.enc', key);

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!