Skip to main content

Encrypt and decrypt files and streams in AES Crypt format (version 2)

Project description

https://github.com/marcobellaccini/pyaescrypt/actions/workflows/main.yml/badge.svg https://pepy.tech/badge/pyaescrypt

About pyAesCrypt

pyAesCrypt is a Python 3 file-encryption module and script that uses AES256-CBC to encrypt/decrypt files and binary streams.

pyAesCrypt is compatible with the AES Crypt file format (version 2).

It is Free Software, released under the Apache License, Version 2.0.

pyAesCrypt is brought to you by Marco Bellaccini - marco.bellaccini(at!)gmail.com.

IMPORTANT SECURITY NOTE: version 2 of the AES Crypt file format does not authenticate the “file size modulo 16” byte. This implies that an attacker with write access to the encrypted file may alter the corresponding plaintext file size by up to 15 bytes.

NOTE: there is no low-level memory management in Python, hence it is not possible to wipe memory areas were sensitive information was stored.

Module usage example

Here is an example showing encryption and decryption of a file:

import pyAesCrypt
password = "please-use-a-long-and-random-password"
# encrypt
pyAesCrypt.encryptFile("data.txt", "data.txt.aes", password)
# decrypt
pyAesCrypt.decryptFile("data.txt.aes", "dataout.txt", password)

This is the most straightforward way to use pyAesCrypt, and should be preferred.

If you need to specify a custom buffer size (default is 64KB), you can pass it as an optional argument:

import pyAesCrypt
# custom encryption/decryption buffer size (default is 64KB)
bufferSize = 128 * 1024
password = "please-use-a-long-and-random-password"
# encrypt
pyAesCrypt.encryptFile("data.txt", "data.txt.aes", password, bufferSize)
# decrypt
pyAesCrypt.decryptFile("data.txt.aes", "dataout.txt", password, bufferSize)

In case you need it, you can work with binary streams too:

import pyAesCrypt
from os import stat, remove
# encryption/decryption buffer size - 64K
# with stream-oriented functions, setting buffer size is mandatory
bufferSize = 64 * 1024
password = "please-use-a-long-and-random-password"

# encrypt
with open("data.txt", "rb") as fIn:
    with open("data.txt.aes", "wb") as fOut:
        pyAesCrypt.encryptStream(fIn, fOut, password, bufferSize)

# decrypt
with open("data.txt.aes", "rb") as fIn:
    try:
        with open("dataout.txt", "wb") as fOut:
            # decrypt file stream
            pyAesCrypt.decryptStream(fIn, fOut, password, bufferSize)
    except ValueError:
        # remove output file on error
        remove("dataout.txt")

you can also perform in-memory encryption/decryption (using BytesIO):

import pyAesCrypt
import io

bufferSize = 64 * 1024
password = "please-use-a-long-and-random-password"

# binary data to be encrypted
pbdata = b"This is binary plaintext \x00\x01"

# input plaintext binary stream
fIn = io.BytesIO(pbdata)

# initialize ciphertext binary stream
fCiph = io.BytesIO()

# initialize decrypted binary stream
fDec = io.BytesIO()

# encrypt stream
pyAesCrypt.encryptStream(fIn, fCiph, password, bufferSize)

# print encrypted data
print("This is the ciphertext:\n" + str(fCiph.getvalue()))

# go back to the start of the ciphertext stream
fCiph.seek(0)

# decrypt stream
pyAesCrypt.decryptStream(fCiph, fDec, password, bufferSize)

# print decrypted data
print("Decrypted data:\n" + str(fDec.getvalue()))

Script usage examples

Encrypt file test.txt in test.txt.aes:

pyAesCrypt -e test.txt

Decrypt file test.txt.aes in test.txt:

pyAesCrypt -d test.txt.aes

Encrypt file test.txt in test2.txt.aes:

pyAesCrypt -e test.txt -o test2.txt.aes

Decrypt file test.txt.aes in test2.txt:

pyAesCrypt -d test.txt.aes -o test2.txt

FAQs

  • Is pyAesCrypt malware?

    NO! Of course it isn’t!

    Nevertheless, being a module, it can be used by any other software, including malware.

    In fact, it has been reported that it is used as crypto library by some ransomware.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyAesCrypt-6.1.0.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyAesCrypt-6.1.0-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file pyAesCrypt-6.1.0.tar.gz.

File metadata

  • Download URL: pyAesCrypt-6.1.0.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for pyAesCrypt-6.1.0.tar.gz
Algorithm Hash digest
SHA256 05752551812d255976199c225db25bfbf53bd7d363f3389dfea1980bd04e53b5
MD5 28899a618d0cd4cc38b70e9f2a254d0c
BLAKE2b-256 2877f221cb2c7d3e06f4d739bda774f87067a6fb297e0da86d6b38d4e90b68b1

See more details on using hashes here.

File details

Details for the file pyAesCrypt-6.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyAesCrypt-6.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for pyAesCrypt-6.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4260360e591be66544db9656b55159e4ec1be32e456bd29459ca605ea68931bc
MD5 d30ae0a945cfdd3a30c646ac7986f692
BLAKE2b-256 05fa8c7a62aeaa1d0334feae38ec211e66aaf7c3085d321448e46511a94ae7e5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page