Question: Objectives: Familiarize the student with: Practice Python tools to encrypt and decrypt files; Account for missing files; Task: Consider the Python .py file(s) provided with
Objectives:
Familiarize the student with:
- Practice Python tools to encrypt and decrypt files;
- Account for missing files;
Task:
Consider the Python .py file(s) provided with this assignment on Blackboard 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 want 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 input 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. Demonstrate both and submit screenshots of your demonstration.
source codes are
encrpt_dcrpt_sample
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)
if __name__ == '__main__':
encryption('file1.txt') #I added an option to select how the encryption will be done
decryption('new_file.txt', 'my_kkey.key')
encrpt_dycrpt_password
import from cryptography.fernet import Fernet
import sys
#####For password-based key
import base64
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
def key_gene_from_password():
password = input("Input the encryption password: ")
password = password.encode() # Convert to type bytes
salt = b'salt_'
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
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_file11.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_file11.txt', 'wb') as f:
f.write(decrypted_data)
except:
print("Your password may be wrong!")
if __name__ == '__main__':
# encryption('file11.txt') #I added an option to select how the encryption will be done
decryption('new_file11.txt')
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
