Skip to main content

bytecrypt

PyPI version Python versions CI License: MIT

bytecrypt encrypts and decrypts bytes, strings, files and full directories with a password. It is a Python library and a command line tool.

bytecrypt derives the key with scrypt. It then seals the data with Fernet, which is AES-128-CBC with an HMAC-SHA256 tag. The package has one dependency and runs on any system with Python 3.11 or later. A standalone executable is also available for users who do not have Python.

pip install bytecrypt
bytecrypt -e -f secrets.txt          # encrypt, bytecrypt asks for the password
bytecrypt -d -f secrets.txt          # decrypt

Contents

Features

  • Password only. bytecrypt needs no key file and no configuration. The password can be a str or bytes.
  • Many targets. bytecrypt encrypts bytes, strings, single files and full directory trees. It can also encrypt the file names.
  • Modern cryptography. bytecrypt derives the key with scrypt (n = 2^17, r = 8, p = 1) and seals the data with authenticated encryption. Each salt comes from a cryptographically secure random number generator.
  • Versioned format. Each encrypted file starts with a header that identifies its format. New releases continue to read the files that old releases wrote. The format specification gives the details.
  • Safe writes. bytecrypt writes each file atomically. An incorrect password or an interrupted run does not corrupt or truncate the file.
  • Three ways to use it. Import the package, run the bytecrypt command, or use the standalone executable.

Installation

From PyPI

pip install bytecrypt

bytecrypt requires Python 3.11 or later. The only dependency is cryptography.

Standalone executable

If you do not have Python, download the latest single file executable:

Platform Download
Windows bytecrypt.exe
Linux bytecrypt

Quick start

If you do not give -p, bytecrypt asks for the password. It asks two times when it encrypts.

bytecrypt -e -f report.pdf                 # encrypt, bytecrypt asks for the password
bytecrypt -d -f report.pdf -p mypassword   # decrypt with a password on the command line

The same operations in Python:

from bytecrypt import encrypt_bytes, decrypt_bytes

token = encrypt_bytes(b"top secret", "my password")
assert decrypt_bytes(token, "my password") == b"top secret"

Python API

Each function accepts the password as a str or as bytes.

Bytes

from bytecrypt import encrypt_bytes, decrypt_bytes

blob = encrypt_bytes(b"secret data", "password")   # -> bytes in the versioned format
data = decrypt_bytes(blob, "password")             # -> b"secret data"

Strings

encrypt_string returns a short base64 token.

from bytecrypt import encrypt_string, decrypt_string

token = encrypt_string("secret text", "password")  # -> "AEJDWQHk..." (str)
text = decrypt_string(token, "password")           # -> "secret text"

Files

bytecrypt encrypts a file in place and writes it atomically. It can also encrypt the file name.

from bytecrypt import encrypt_file, decrypt_file

encrypt_file("report.docx", "password")
decrypt_file("report.docx", "password")

# encrypt and decrypt the file name too
encrypt_file("report.docx", "password", encrypt_filename=True)
decrypt_file("<encrypted-name>", "password", decrypt_filename=True)

Directories

from bytecrypt import encrypt_directory, decrypt_directory

# all files in the directory; recursive=True includes the subdirectories
encrypt_directory("my/directory", "password", recursive=True)
decrypt_directory("my/directory", "password", recursive=True)

# include the file names
encrypt_directory("my/directory", "password", encrypt_filename=True, recursive=True)

Migration and password change

The reencrypt_* functions decrypt the data and encrypt it again in memory. The plaintext does not go to disk, and the write is atomic.

from bytecrypt import reencrypt_file, reencrypt_directory

reencrypt_file("old.txt", "password")                            # upgrade a legacy file
reencrypt_file("old.txt", "password", new_password="newpass")    # change the password
reencrypt_directory("my/directory", "password", recursive=True)  # the full tree

Errors

The library functions raise typed exceptions. The CLI prints a message to stderr and exits with a non-zero code. All exceptions inherit from BytecryptError.

Exception Cause
InvalidPasswordError The password is incorrect, or the data is damaged
NotBytecryptFileError The input is not a bytecrypt blob
UnsupportedFormatError The format version is unknown, so the file needs a newer bytecrypt
AlreadyEncryptedError The data is already encrypted. Use force=True to encrypt it again
FileNameTooLongError The encrypted file name is longer than the file system limit

Command line

# files
bytecrypt -e -f secret.txt -p mypassword
bytecrypt -d -f secret.txt                       # bytecrypt asks for the password

# file name and contents
bytecrypt -e -f secret.txt -efn -p mypassword
bytecrypt -d -f <encrypted-name> -dfn -p mypassword

# strings
bytecrypt -e -str "secret text" -p mypassword
bytecrypt -d -str "<token>" -p mypassword

# directories; -r includes the subdirectories
bytecrypt -e -dir my/directory -r -p mypassword
bytecrypt -d -dir my/directory -r -p mypassword

# write the data again in the current format, or change the password
bytecrypt --reencrypt -f old.txt -p oldpw
bytecrypt --reencrypt -dir my/directory -r -p oldpw -np newpw

Arguments

Short Long Description
-e --encrypt Encrypt the target
-d --decrypt Decrypt the target
--reencrypt Write the target again in the current format, or change the password
-f --file Use a file as the target
-dir --directory Use a directory as the target
-str --string Use a string as the target
-efn --encrypt-filename Encrypt the file name too
-dfn --decrypt-filename Decrypt the file name too
-p --password The password. bytecrypt asks for it if you omit this option
-np --new-password The new password, with --reencrypt
-r --recursive Include the subdirectories
-F --force Encrypt the data again, also when it is already encrypted

How it works

  • Key derivation. bytecrypt uses scrypt with n = 2^17, r = 8 and p = 1. These are the OWASP parameters for interactive use. The inputs are your password and a new 16-byte salt from a secure random number generator.
  • Encryption. Fernet gives AES-128-CBC with an HMAC-SHA256 authentication tag. bytecrypt therefore detects a change to the encrypted data.
  • Format. Each encrypted blob starts with a small header: magic || version || salt || token. The version byte selects the key derivation parameters from a fixed table. bytecrypt does not read the cost parameters from the file, so a crafted file cannot cause a large memory allocation. FORMAT.md gives the full byte layout.

Important notes

  • There is no password recovery. If you forget the password, the data stays encrypted. bytecrypt has no backdoor and no reset function.
  • bytecrypt encrypts a file in place. The write is atomic, so a half-written file does not occur. bytecrypt does not keep a backup copy. Keep your own backup of important data.
  • scrypt is slow, and this is intentional. One operation takes approximately 0.5 s and 128 MiB of memory. This cost makes an attack with many password guesses expensive. A large batch of files therefore takes time.

Compatibility and migration

bytecrypt reads the files that the 0.x releases wrote. That legacy format uses PBKDF2 and is weaker, but bytecrypt decrypts it without an extra step. Your existing data continues to work. bytecrypt writes new data only in the current scrypt format.

To move old files to the stronger format, encrypt them again:

bytecrypt --reencrypt -f old.txt -p mypassword
bytecrypt --reencrypt -dir my/directory -r -p mypassword

Development

git clone https://github.com/milarditch/bytecrypt
cd bytecrypt
python -m pip install -e ".[test]"
python -m pytest                   # run the test suite
python -m yapf -i -r src tests     # format the code

The test suite contains regression vectors in tests/vectors/. They prove that bytecrypt still decrypts the files of the old releases. FORMAT.md is the format specification.

License

bytecrypt uses the MIT license. Copyright 2024-2026 Stjepan Milardić.

Metadata

Release files for bytecrypt 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bytecrypt 1.0.0
File Size Uploaded Provenance Actions
bytecrypt-1.0.0.tar.gz 26.3 kB With provenance Details for bytecrypt-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bytecrypt 1.0.0
File Interpreter ABI Platform Actions
bytecrypt-1.0.0-py3-none-any.whl Python 3 none any Details for bytecrypt-1.0.0-py3-none-any.whl

Total release size: 38.5 kB

Release files / bytecrypt-1.0.0.tar.gz

Download URL bytecrypt-1.0.0.tar.gz
Size 26.3 kB
Tags Source
SHA-256 checksum
How to use checksums
c833d82dbaf5fdf796ae8469c38084d2dd12e159cc2e0eec5f7e6f3d397c25ac
BLAKE2b-256 checksum
How to use checksums
c7eef8a028b1185deb977bcc280fc4f89589990350da28563d1d5775a018de85
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 22, 2026.

Transparency log

Release files / bytecrypt-1.0.0-py3-none-any.whl

Download URL bytecrypt-1.0.0-py3-none-any.whl
Size 12.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f29f72c5b7f5cbf44bf41dcd9e79baec393a275f346bcc7304dc67e3fbfb84dd
BLAKE2b-256 checksum
How to use checksums
5f8b1d518321771f8d0f52db80c7787b5c29c9e54cab40f3ccf401f5e36c4c23
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 22, 2026.

Transparency log

Security for bytecrypt 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for bytecrypt-1.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 22, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page