pyAesCrypt

6.1.1 · active · verified Thu Apr 16

pyAesCrypt is a Python library (version 6.1.1) for encrypting and decrypting files and streams using the AES Crypt format (version 2). It provides a straightforward API for secure file and stream operations with AES encryption, often seeing patch and minor releases and occasional major updates that introduce breaking changes or significant features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to encrypt and decrypt a file using `pyAesCrypt.encryptFile` and `pyAesCrypt.decryptFile`. It creates a dummy text file, encrypts it, decrypts it, prints the decrypted content, and then cleans up the temporary files. Remember to use a strong password and manage it securely.

import pyAesCrypt
import os

# --- Setup (create dummy file) ---
input_file = "data.txt"
encrypted_file = "data.aes"
decrypted_file = "data.txt.out"
password = os.environ.get('AESCRYPT_PASSWORD', 'mySuperSecretPassword123') # Replace with strong password
buffer_size = 64 * 1024 # 64KB

with open(input_file, 'w') as f:
    f.write('This is a test message to be encrypted.')

print(f"Original file '{input_file}' created.")

# --- Encryption ---
try:
    pyAesCrypt.encryptFile(input_file, encrypted_file, password, buffer_size)
    print(f"File '{input_file}' encrypted to '{encrypted_file}'.")

    # --- Decryption ---
    pyAesCrypt.decryptFile(encrypted_file, decrypted_file, password, buffer_size)
    print(f"File '{encrypted_file}' decrypted to '{decrypted_file}'.")

    with open(decrypted_file, 'r') as f:
        content = f.read()
        print(f"Decrypted content: '{content}'")

    # Verify content
    with open(input_file, 'r') as f_orig, open(decrypted_file, 'r') as f_dec:
        if f_orig.read() == f_dec.read():
            print("Decryption successful and content matches original.")
        else:
            print("WARNING: Decrypted content does NOT match original!")

except pyAesCrypt.AESCryptException as e:
    print(f"An encryption/decryption error occurred: {e}")
except FileNotFoundError as e:
    print(f"File not found error: {e}")
except PermissionError as e:
    print(f"Permission error: {e}")
finally:
    # --- Cleanup ---
    for f in [input_file, encrypted_file, decrypted_file]:
        if os.path.exists(f):
            os.remove(f)
            print(f"Cleaned up '{f}'.")

view raw JSON →