Objectives:
Familiarize the student with:
- Practice Python tools to encrypt and decrypt files;
- Account for missing files;
Task:
Consider the Python code screenshots provided with this assignment and modify the code to meet the following requirements:
-Complete the "main" function that will run whenever the module is called;
-The main function should interact with the user and allow the user to specify whether he or she wants to encrypt or decrypt;
-The user should be allowed to choose between 1) the insecure mode where the encryption key file is saved for usage in decryption or 2) a more secure way where the key is generated whenever it is used through a passphrase;
-The user should be allowed to input the file to encrypt or decrypt;
-When a user inputs the file to encrypt, the program should identify the file extension explicitly. That same extension should be added to the encrypted file and the decrypted file.
The program should be able to run from the command line interface and the python shell
encrypt_decrypt_password.py - C:/Users/Abund/OneDrive/Documents/encrypt_decrypt_password.py (3.9.1) X File Edit Format Run Options Window Help salt=salt, iterations=100000, backend=default_backend () keyy = base64. urlsafe_b64encode (kdf . derive (password) ) # Can only use kdf once print ("Key generated") return (keyy) def encryption (f2encrypt) : *get the key from the key_gene function key = key_gene from password () fu = Fernet (key) #tool with the key #get the file with open (f2encrypt, 'rb' ) as file: file data = file. read () #encrypt the file encrypted_data = fu. encrypt (file_data) with open ( 'new_filell. txt', 'wb') as f: f. write (encrypted data) #print file sizes * print (f"Size of the file to encrypt is {os. path. getsize (f2encrypt) } Bytes") # print (f"Size of the encrypted file is {os. path. getsize ('new_file. txt' ) } Bytes") #decrypt def decryption (file2decrypt) : print ( 'Trying to decrypt!') try : #Obtain key key = key gene_from password () fu = Fernet (key) #open the file to decrypt as a byte Python object data2decrypt = ! ' with open (file2decrypt, 'rb' ) as f: data2decrypt = f. read () #decryption decrypted data = fu. decrypt (data2decrypt) # Save the decrypted data to a file named 'decrypted file' with open ( 'decrypted_filell. txt', 'wb') as f: f. write (decrypted data) except : print ("Your password may be wrong!") if name == ' main ': # encryption ( 'filell. txt' ) #I added an option to select how the encryption will be done decryption ( 'new_filell. txt' ) n: 46 Col: 24 Type here to search O 6:22 PM 2/25/2021 E.encrypt_decrypt_simple.py - C:\\Users\\Abund\\ Downloads\\encrypt_decrypt_simple.py (3.9.1) X File Edit Format Run Options Window Help from cryptography . fernet import Fernet import os . path, sys def key_gene () : #create a key my_Key = Fernet . generate_key () #write the key to a file with open ("my kkey . key", "wb") as kf: kf . write (my_Key) #print the size of the key file print (f"Size of the key file is {os. path. getsize ('my_kkey. key' ) } Bytes") return (my_Key) def encryption (f2encrypt) : #get the key from the key_gene function key = key_gene () fu = Fernet (key) #tool with the key #get the file with open (f2encrypt, 'rb' ) as file: file data = file. read () #encrypt the file encrypted_data = fu. encrypt (file_data) with open ( 'new_file. txt', 'wb') as f: f. write (encrypted_data) #print file sizes print (f"Size of the file to encrypt is (os. path. getsize (f2encrypt) } Bytes") print (f"Size of the encrypted file is {os. path. getsize ('new_file. txt' )} Bytes") #decrypt def decryption (file2decrypt, key_file) : print ( 'Trying to decrypt!' ) #read the key file as a binary and buld the tool with the key key = " ' with open (key_file, 'rb') as f: key = f. read () fu = Fernet (key) #open the file to decrypt as a byte Python object data2decrypt = ' ' with open (file2decrypt, 'rb' ) as f: data2decrypt = f. read () #decryption decrypted_data = fu. decrypt (data2decrypt) #Save the decrypted data to a file named 'decrypted_file' with open ( 'decrypted file. txt', 'wb') as f: f. write (decrypted_data) Ln: 1 Col: 0 Type here to search O HABSSOONGE 6:26 PM 2/25/2021[*encrypt_decrypt_simple.py - C:\\Users\\Abund\\ Downloads\\encrypt_decrypt_simple.py (3.9.1)* X File Edit Format Run Options Window Help #print the size of the key file print (f"Size of the key file is fos. path. getsize ('my_kkey . key' ) } Bytes") return (my_Key) def encryption (f2encrypt) : #get the key from the key_gene function key = key_gene () fu = Fernet (key) #tool with the key #get the file with open (f2encrypt, 'rb' ) as file: file data = file. read () #encrypt the file encrypted_data = fu. encrypt (file_data) with open ( 'new file. txt' , 'wb') as f: f. write (encrypted_data) #print file sizes print (f"Size of the file to encrypt is {os. path. getsize (f2encrypt) } Bytes") print (f"Size of the encrypted file is {os. path. getsize ('new_file. txt' ) } Bytes") #decrypt def decryption (file2decrypt, key_file) : print ( 'Trying to decrypt!' ) #read the key file as a binary and buld the tool with the key key = with open (key_file, 'rb' ) as f: key = f. read () fu = Fernet (key) #open the file to decrypt as a byte Python object data2decrypt = ' with open (file2decrypt, 'rb' ) as f: data2decrypt = f. read () #decryption decrypted_data = fu. decrypt (data2decrypt) #Save the decrypted data to a file named 'decrypted file' with open ( 'decrypted file. txt', 'wb') as f: f. write (decrypted_data) if name_ ' main encryption ( 'filel. txt' ) #I added an option to select how the encryption will be done decryption ( 'new_file. txt', 'my_kkey. key' ) Ln: 1 Col: 1 Type here to search 6:26 PM 2/25/2021