Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 code is

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

OCA Oracle Database SQL Exam Guide Exam 1Z0-071

Authors: Steve O'Hearn

1st Edition

1259585492, 978-1259585494

More Books

Students also viewed these Databases questions