Skip to main content

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

Project description

https://travis-ci.org/marcobellaccini/pyAesCrypt.svg?branch=master

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).

The script 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
# encryption/decryption buffer size - 64K
bufferSize = 64 * 1024
password = "foopassword"
# encrypt
pyAesCrypt.encryptFile("data.txt", "data.txt.aes", password, bufferSize)
# decrypt
pyAesCrypt.decryptFile("data.txt.aes", "dataout.txt", password, bufferSize)

by calling the stream-oriented functions, you can work with binary streams too:

import pyAesCrypt
from os import stat, remove
# encryption/decryption buffer size - 64K
bufferSize = 64 * 1024
password = "foopassword"

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

# get encrypted file size
encFileSize = stat("data.txt.aes").st_size

# decrypt
with open("data.txt.aes", "rb") as fIn:
    with open("dataout.txt", "wb") as fOut:
        try:
            # decrypt file stream
            pyAesCrypt.decryptStream(fIn, fOut, password, bufferSize, encFileSize)
        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 = "foopassword"

# 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()))

# get ciphertext length
ctlen = len(fCiph.getvalue())

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

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

# 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

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-0.4.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

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

pyAesCrypt-0.4-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyAesCrypt-0.4.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.8.1 pkginfo/1.2.1 requests/2.12.4 setuptools/33.1.1 requests-toolbelt/0.7.0 clint/0.5.1 CPython/3.5.3 Linux/4.9.0-7-amd64

File hashes

Hashes for pyAesCrypt-0.4.tar.gz
Algorithm Hash digest
SHA256 852b383072ebc2c79b691b3d4ed62508c08d20f2c67ff80874c8cc786c28ea52
MD5 34ce1a42bb78c6d7d030cf35c827b170
BLAKE2b-256 f7deb3b99c7d4cdc2a0998bf9eef8c7c07afe3067a082dcbc1b891a0c290b571

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyAesCrypt-0.4-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.8.1 pkginfo/1.2.1 requests/2.12.4 setuptools/33.1.1 requests-toolbelt/0.7.0 clint/0.5.1 CPython/3.5.3 Linux/4.9.0-7-amd64

File hashes

Hashes for pyAesCrypt-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3ddf080590c004465c2a9e22ed698000cbc1f4e5e6a91a0b9a29b893191cf71a
MD5 eb988ed8fcbd3599e418754235f3e4e3
BLAKE2b-256 fada3dfc14c6409a86fdf49b903b4183372558ee996a25cf737e4817861d2944

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