Search code examples
pythonpython-3.xencryptionfernet

How would I encrypt a list of files in Python using fernet?


I am trying to create a python script in which you run through all the files in the directory and encrypt them as an alternative to BitLocker. One of the files is just an example passkey as a proof of concept.

I got so far as to get the passkey(s) as input from the user. I downloaded, and imported cryptography.fernet, and created a list of the files in the directory.

I also generated the key that I will use for encryption but am unsure of how to do file encryption with Fernet.

#Script is named match_key.py

from cryptography.fernet import Fernet
import os

#Initialize variables; Specifically the passkey variables
passkey = input("Enter your key: ")
add_key = input("Do you want to enter another key? (y/N) ")
key_list = []
key_list.append(passkey)

encr_file_list = []

#Checks if the user wants to input another key
while add_key == "y" or add_key == "Y":
    key = input("Enter your key: ")
    key_list.append(passkey)
    add_key = input("Do you want to enter another key? (y/N) ")
    if add_key == "n" or add_key == "N":
        #Store the passwords in a file written to the disk
        with open("passwords.txt", "wb") as passwords:
            passwords.write(key_list)
        break
else:
    pass

print(key_list)

#Add each file of the directory to a list except for match_key.py and the encryption key
key = Fernet.generate_key()
for file in os.listdir():
    if file == "match_key.py":
        continue
    if os.path.isfile(file):
        encr_file_list.append(file)

print(encr_file_list)

My main question is:

How would I take each file, besides match_key.py and the key, in the directory and encrypt it? Would this be (preferably) possible with a for loop?


Solution

  • After generating the key for encryption, initialize a Fernet object with it, and call encrypt() on the file contents in a for loop. Remember that the same key is needed to decrypt() the files again, so store the key in file.

    encr_file_list = []
    for file in os.listdir():
        if file == "match_key.py":
            continue
        if os.path.isfile(file):
            encr_file_list.append(file)
    
    key = Fernet.generate_key()
    f = Fernet(key)
    # store the key to use later for decryption
    with open('encryptionkey.key', 'wb') as f:
       f.write(key)
    
    for file in encr_file_list:
        with open(file, 'rb') as f:
            file_content = f.read()
        encrypted_content = f.encrypt(file_content)
        with open(file, 'wb') as f:
            f.write(encrypted_content)
    

    Later on to decrypt, do the following

    # Get the key back from the file
    with open('encryptionkey.key', 'rb') as f:
       key = f.read()
    
    f = Fernet(key)
    
    # Store the files you want to decrypt in decr_file_list then do the following
    for file in decr_file_list:
        with open(file, 'rb') as f:
            file_content = f.read()
        decrypted_content = f.decrypt(file_content)
        with open(file, 'wb') as f:
            f.write(decrypted_content)