please discuss the function of this code: # Importing the Fernet class from the cryptography libaray for both the encryption
Fernet is a symmetric encryption method that ensures that the encrypted data using the Fernet key
[...] can only be decrypted by means of the same exact key
from cryptography.fernet import Fernet
Define file paths for the input file (to be encrypted), the output file (encrypted result)
[...] the final decrypted file, and finally where to store the encryption key
input_file_path = r"C:\Users\adam\Desktop\note1.txt" # The original file
encrypted_file_path = r"C:\Users\adam\Desktop\note2.txt" # The encrypted version of the original file
decrypted_file_path = r"C:\Users\adam\Desktop\note3.txt" # The decrypted version of the encrypted fiile
key_file_path = r"C:\Users\adam\Desktop\key.key" # The file in which the encryption key is stored
This function generates an encryption key and saves it to its own file
def generate_and_save_key():
key = Fernet.generate_key() # The generation of a secure and random encryption key suitable for Fernet
with open(key_file_path, "wb") as key_file: # Opens/ creates the (key.key) file to write binary data (the key)
key_file.write(key) # Writes the generated key to the corresponding file (key.key), enabling usage for encryption and decryption
return key # Accordingly returns the generated key
This function loads the encryption key from its respective file path (C:\Users\adam\Desktop\key.key)
def load_key():
return open(key_file_path, "rb").read() # Reads the key in which the key file is opened in read-binary mode
This function encrypts the file. It requiress paths to both the input and the output files,
[...] and the encryption key
def encrypt_file(file_path, encrypted_file_path, key):
f = Fernet(key) # The initialisation of Fernet with the designated key
with open(file_path, "rb") as file: # Opens the input file in read-binary mode
file_data = file.read() # Then reads the data from the file
encrypted_data = f.encrypt(file_data) # Encrypts the data that's been read from the file
with open(encrypted_file_path, "wb") as file: # Opens/ creates the output file in binary-write mode
file.write(encrypted_data) # Then proceeds to write the encrypted data to the output file
This function decrypts the encrypted file. It requires paths to both the encrypted file,
[...] as well as the output file for decrypted content, and the encryption key
def decrypt_file(encrypted_file_path, decrypted_file_path, key):
f = Fernet(key) # The initialisation of Fernet with the designated decryption key
with open(encrypted_file_path, "rb") as file: # Opens the encrypted file in binary-read mode
encrypted_data = file.read() # Then reads the encrypted data
decrypted_data = f.decrypt(encrypted_data) # Sequentially decrypts the content, then reverses the encryption
with open(decrypted_file_path, "wb") as file: # Opens/ creates the file which to write the decrypted content in binary-write mode
file.write(decrypted_data) # Then proceedss to write the decrypted data restoring its original content
The encryption/ decryption process commences
The generate_and_save_key() step is crucial and must only be executed once only, to maintain data consistency
generate_and_save_key()
The previously saved key is loaded for the use in the encryption and decryption process
key = load_key()
Encrypt the file at ‘input_file_path’ and save the encrypted data to (encrypted_file_path), using the loaded key
encrypt_file(input_file_path, encrypted_file_path, key)
print(f"{input_file_path} has been encrypted.")
Decrypt the file ‘encrypted_file_path’ and save the decrypted output to (decrypted_file_path), using the same key
decrypt_file(encrypted_file_path, decrypted_file_path, key)
print(f"{decrypted_file_path} has been decrypted.")