w3resource

Python Project - Encrypt and Decrypt files with Symmetric Encryption


File Encryption/Decryption: Create a program that encrypts and decrypts files.

Input values:

User interacts with the program by selecting files to encrypt or decrypt and providing encryption/decryption keys.

Output value:

Encrypted or decrypted versions of the selected files based on user input.

Example:

Input values:
1. Select a file to encrypt: "test.txt"
2. Enter the encryption key: "secretkey123"
Output value:
The program encrypts the file "test.txt" using the provided encryption key "secretkey123" and saves the encrypted version as "test_encrypted.txt".
Input values:
3. Select a file to decrypt: "test_encrypted.txt"
4. Enter the decryption key: "secretkey123"
Output value:
The program decrypts the file "test_encrypted.txt" using the provided decryption key "secretkey123" and saves the decrypted version as "document_decrypted.txt".

Solution: Using Fernet (Symmetric Encryption with Cryptography Library)

Code:

# Import necessary modules from cryptography library
from cryptography.fernet import Fernet

# Function to generate an encryption key and save it
def generate_key():
    key = Fernet.generate_key()  # Generate a new Fernet encryption key
    with open("secret.key", "wb") as key_file:  # Save the key to a file
        key_file.write(key)

# Function to load an existing encryption key from file
def load_key():
    return open("secret.key", "rb").read()  # Read and return the encryption key

# Function to encrypt a file using a provided key
def encrypt_file(file_path):
    key = load_key()  # Load the encryption key
    fernet = Fernet(key)  # Create a Fernet instance with the key
    
    # Read the file content
    with open(file_path, "rb") as file:
        file_data = file.read()
    
    # Encrypt the file data
    encrypted_data = fernet.encrypt(file_data)
    
    # Write the encrypted data to a new file
    with open(f"{file_path}_encrypted", "wb") as file:
        file.write(encrypted_data)

# Function to decrypt an encrypted file using the key
def decrypt_file(file_path):
    key = load_key()  # Load the encryption key
    fernet = Fernet(key)  # Create a Fernet instance with the key
    
    # Read the encrypted file content
    with open(file_path, "rb") as file:
        encrypted_data = file.read()
    
    # Decrypt the file data
    decrypted_data = fernet.decrypt(encrypted_data)
    
    # Write the decrypted data to a new file
    with open(f"{file_path}_decrypted", "wb") as file:
        file.write(decrypted_data)

# Example usage:
generate_key()  # Only run once to generate the key
encrypt_file("test.txt")  # Encrypt the file
decrypt_file("test.txt_encrypted")  # Decrypt the file

Explanation:

  • Generate Encryption Key: Creates a secure key using Fernet and saves it.
  • Load Encryption Key: Reads the key from file for encryption or decryption.
  • Encrypt File: Encrypts file content using Fernet with the loaded key.
  • Decrypt File: Decrypts the encrypted file with the same key.


Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/projects/python/python-file-encryption-decryption-project.php