Enhance Your Computer Security with Python: Simple File Encryption

Code Breakers Convention At Bletchley Park
Code Breakers Convention At Bletchley Park / Ian Waldie/GettyImages
facebooktwitterreddit

Computer security is a serious issue, and until blockchain computing becomes more common, our files are vulnerable to a hacking attack, and sensitive information might get “leaked” despite our best efforts to prevent it. However, with the help of Python and a few lines of code, you can encrypt the files you want to keep secret, creating an extra layer of protection.

Is Encrypting Files Difficult?

No. With Python, it’s easy to encrypt and decrypt your files with a single script, and we’ll show you how to write it. If you are new to Python, we recommend checking out our guide to why it’s the best for DIYers, along with our guide to installing and trying it out.

Building Your Python Encryption Script             

Install the Cryptography Library in Python

Before you can encrypt any files, you will need to install the “ cryptography” library. You only need to do this once by typing the following line of code at the command prompt.

pip install cryptography

Generate a Key

With the cryptography library installed, you can create a key file that will have the information needed to encrypt and decrypt your files. This script generates a key and saves it to a file named “secret.key.” Once you create the following file, put it in a safe place. We recommend using a USB drive or something similar that you can remove from the computer when you are not using it.

from cryptography.fernet import Fernet

def generate_key():

key = Fernet.generate_key()

with open("secret.key", "wb") as key_file:

key_file.write(key)

generate_key()

In the complete script below, you will be able to create new keys whenever you want, but you will still need to keep them secure.

Load the Key

You need to load the key from the file whenever you want to encrypt or decrypt files using the following code.

def load_key():

return open("secret.key", "rb").read()

Encrypt a File

The following code is what we need to encrypt our files. It reads the contents of your file, encrypts it using the key, and saves it as a new file with the .enc extension.

def encrypt_file(file_name):

key = load_key()

f = Fernet(key)

with open(file_name, "rb") as file:

file_data = file.read()

encrypted_data = f.encrypt(file_data)

with open(file_name + ".enc", "wb") as file:

file.write(encrypted_data)

# Example usage:

encrypt_file("example.txt")

Decrypt a File

This function reads the encrypted file, decrypts it using the key, and writes the decrypted data back to a new file with the original name.

def decrypt_file(file_name):

key = load_key()

f = Fernet(key)

with open(file_name, "rb") as file:

encrypted_data = file.read()

decrypted_data = f.decrypt(encrypted_data)

with open(file_name[:-4], "wb") as file: # Remove the .enc extension

file.write(decrypted_data)

# Example usage:

decrypt_file("example.txt.enc")

Full Program

The following code puts it all together, and using it, you can create keys and save and decrypt files at any time.

from cryptography.fernet import Fernet

def generate_key():

key = Fernet.generate_key()

with open("secret.key", "wb") as key_file:

key_file.write(key)

def load_key():

return open("secret.key", "rb").read()

def encrypt_file(file_name):

key = load_key()

f = Fernet(key)

with open(file_name, "rb") as file:

file_data = file.read()

encrypted_data = f.encrypt(file_data)

with open(file_name + ".enc", "wb") as file:

file.write(encrypted_data)

def decrypt_file(file_name):

key = load_key()

f = Fernet(key)

with open(file_name, "rb") as file:

encrypted_data = file.read()

decrypted_data = f.decrypt(encrypted_data)

with open(file_name[:-4], "wb") as file:

file.write(decrypted_data)

# Generate a key (do this once)

generate_key()

# Encrypt a file

encrypt_file("example.txt")

# Decrypt the file

decrypt_file("example.txt.enc")

Command To Generate a Key

python file_encryptor.py generate_key


Command To Encrypt a File

python file_encryptor.py encrypt example.txt


Command To Decrypt a File

python file_encryptor.py decrypt example.txt.enc

Keep following Geeksided for more simple but useful Python scripts.

feed