Question: Consider the Python .py file(s) code provided with this assignment and modify the code to meet the following requirements: -Complete the main function that will
Consider the Python .py file(s) code 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.
encrypt_decrypt_simple_py code
from cryptography. fernet import Fernet
import sys
#####For password-based key
import base64
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')
encrypt_decrpyt_password py code
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')
please create comments explaining how you wrote the code
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
