pyAesCrypt

repository·master·Indexed 42 days ago

https://github.com/marcobellaccini/pyaescrypt

A Python 3 module and CLI tool providing AES256-CBC encryption for files and binary streams. It maintains compatibility with the AES Crypt (version 2) file format and includes functions for file-based encryption (encryptFile, decryptFile) and stream-based encryption (encryptStream, decryptStream).

Tokens
1.4K
Snippets
4
Records
5
Agent score
4%

What's inside pyAesCrypt

  1. Encrypt and decrypt files with pyAesCrypt

    master

    The simplest way to use pyAesCrypt is via the encryptFile and decryptFile functions. These functions handle file I/O automatically. You can optionally provide a bufferSize (in bytes) to control memory usage during the process; the default is 64KB.

    Security Note: pyAesCrypt is compatible with AES Crypt file format (version 2). Note that version 2 does not authenticate the "file size modulo 16" byte, meaning an attacker with write access to the encrypted file could potentially alter the plaintext file size by up to 15 bytes.

    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)
  2. Encrypt and decrypt binary streams with pyAesCrypt

    master

    For more control or when working with non-file objects (like io.BytesIO), use encryptStream and decryptStream.

    Important: When using stream-oriented functions, providing a bufferSize is mandatory.

    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)
    
    # go back to the start of the ciphertext stream for decryption
    fCiph.seek(0)
    
    # decrypt stream
    pyAesCrypt.decryptStream(fCiph, fDec, password, bufferSize)
  3. Use the pyAesCrypt CLI for file encryption

    master

    You can use pyAesCrypt directly from the command line to encrypt or decrypt files.

    • -e: Encrypt a file.
    • -d: Decrypt a file.
    • -o: Specify an output filename (defaults to the input filename without the .aes extension for encryption, or the input filename for decryption).
    # Encrypt file test.txt into test.txt.aes
    pyAesCrypt -e test.txt
    
    # Decrypt file test.txt.aes into test.txt
    pyAesCrypt -d test.txt.aes
    
    # Encrypt file test.txt into a specific output file test2.txt.aes
    pyAesCrypt -e test.txt -o test2.txt.aes
    
    # Decrypt file test.txt.aes into a specific output file test2.txt
    pyAesCrypt -d test.txt.aes -o test2.txt
  4. Reference the pyAesCrypt CLI flags and arguments

    master

    The following arguments and flags are available for the pyAesCrypt CLI tool:

    Argument/FlagTypeDescription
    filenamepositionalThe file to be encrypted or decrypted
    -e, --encryptflagEncrypt the input file
    -d, --decryptflagDecrypt the input file
    -o, --outoptionSpecify the output file path
    -p, --passwordoptionSpecify the password (use with caution)
  5. Use the pyAesCrypt CLI to encrypt or decrypt files

    master

    The pyAesCrypt command-line utility allows you to encrypt or decrypt files using AES256-CBC. It is compatible with the AES Crypt (version 2) file format.

    Encryption

    To encrypt a file, use the -e or --encrypt flag. If you do not specify an output filename using -o, the utility will automatically append .aes to the original filename.

    Decryption

    To decrypt a file, use the -d or --decrypt flag. If the input file does not have a .aes extension, you must provide an output filename using the -o flag.

    Security Notes

    • Password Input: If you do not provide a password via the -p flag, the CLI will securely prompt you for the password and ask for confirmation during encryption.
    • Plaintext Passwords: Providing a password via the -p flag may be unsafe as it can appear in command-line history.
    • File Integrity: Version 2 of the AES Crypt format does not authenticate the "file size modulo 16" byte, meaning an attacker could potentially alter the plaintext file size by up to 15 bytes.
    # Encrypt a file (creates file.txt.aes)
    pyAesCrypt file.txt -e
    
    # Encrypt a file with a specific output name
    pyAesCrypt file.txt -e -o secret.aes
    
    # Decrypt a file (creates file.txt)
    pyAesCrypt file.txt.aes -d
    
    # Decrypt a file with a specific output name
    pyAesCrypt file.txt.aes -d -o restored_file.txt
    
    # Decrypt a file that doesn't have the .aes extension (requires -o)
    pyAesCrypt my_encrypted_file -d -o output.txt