Skip to main content

Jasypt-like encryption library for Python - Encrypt your configuration with ENC(...) pattern

Project description

PyCrypt ๐Ÿ”

CI PyPI version Python Version License

Jasypt-like encryption library for Python - Encrypt your application configuration with the familiar ENC(...) pattern used in Spring Boot applications.

Made with โค๏ธ from Claude AI for Python developers who need Jasypt


๐ŸŽฏ Why PyCrypt?

If you're coming from Java/Spring Boot world and need to share encrypted configuration with Python applications, PyCrypt is for you! It provides:

  • โœ… Familiar ENC(...) pattern - Just like Jasypt in Spring Boot
  • โœ… Java Jasypt compatibility - Decrypt values encrypted by Java
  • โœ… Multiple encryption algorithms - From legacy Jasypt to modern AES-256-GCM
  • โœ… Config file loaders - Load .env, YAML, JSON with auto-decryption
  • โœ… CLI tool included - Encrypt/decrypt from command line
  • โœ… Cross-platform - Works with GoCrypt (Go) and Java Jasypt

๐Ÿ“ฆ Installation

pip install pycrypt-jasypt

๐Ÿš€ Quick Start

Basic Encryption/Decryption

from pycrypt import Encryptor

# Create encryptor with password
enc = Encryptor("mySecretPassword")

# Encrypt
encrypted = enc.encrypt_with_prefix("db_password_123")
print(encrypted)  # ENC(base64encodedvalue...)

# Decrypt
decrypted = enc.decrypt_prefixed(encrypted)
print(decrypted)  # db_password_123

Loading Encrypted Configuration

from pycrypt import ConfigLoader
import os

# .env file:
# DATABASE_HOST=localhost
# DATABASE_PASSWORD=ENC(AbCdEf123456...)

loader = ConfigLoader(os.getenv("PYCRYPT_PASSWORD"))
config = loader.load_env_file(".env")

print(config["DATABASE_PASSWORD"])  # actual_password

๐Ÿ” Encryption Algorithms

Encryptor Algorithm Security Use Case
Encryptor AES-256-GCM โญโญโญโญโญ Recommended for new projects
JasyptStrongEncryptor PBEWithHmacSHA256AndAES_256 โญโญโญโญ Jasypt strong compatibility
JasyptEncryptor PBEWithMD5AndDES โญโญ Legacy Jasypt compatibility

Choose the Right Algorithm

from pycrypt import Encryptor, JasyptEncryptor, JasyptStrongEncryptor

# RECOMMENDED: For new Python projects
enc = Encryptor(password)

# For compatibility with Java Jasypt (default algorithm)
enc = JasyptEncryptor(password)

# For compatibility with Java Jasypt (strong encryption)
enc = JasyptStrongEncryptor(password)

โ˜• Java Jasypt Compatibility

โš ๏ธ Important: Compatibility Matrix

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚            ENCRYPT WITH โ†’ DECRYPT WITH                          โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Java Jasypt (default)  โ†’ JasyptEncryptor        โœ… YES          โ”‚
โ”‚ Java Jasypt (strong)   โ†’ JasyptStrongEncryptor  โœ… YES          โ”‚
โ”‚ Java Jasypt (default)  โ†’ Encryptor              โŒ NO           โ”‚
โ”‚ Encryptor              โ†’ Java Jasypt            โŒ NO           โ”‚
โ”‚ JasyptEncryptor        โ†’ Java Jasypt            โœ… YES          โ”‚
โ”‚ GoCrypt JasyptEnc      โ†’ JasyptEncryptor        โœ… YES          โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Decrypt Values from Java

from pycrypt import JasyptEncryptor

# Your Java application.properties has:
# db.password=ENC(xxxFromJavaxxx)

enc = JasyptEncryptor(same_password_as_java)
decrypted = enc.decrypt_prefixed("ENC(xxxFromJavaxxx)")  # โœ… Works!

Share Config with Go (GoCrypt)

from pycrypt import JasyptEncryptor

# Use JasyptEncryptor so both Python and Go can read
enc = JasyptEncryptor(shared_password)
encrypted = enc.encrypt_with_prefix("shared_secret")

# This ENC(...) value can be decrypted by:
# - Python: using JasyptEncryptor
# - Go: using gocrypt.NewJasyptEncryptor
# - Java: using Jasypt library

๐Ÿ“– Usage Guide

Configuration Files

.env File

# config.env
DATABASE_HOST=localhost
DATABASE_PASSWORD=ENC(AbCdEf123456...)
API_KEY=ENC(XyZ789...)
from pycrypt import ConfigLoader

loader = ConfigLoader(password)
config = loader.load_env_file("config.env")
print(config["DATABASE_PASSWORD"])  # decrypted value

JSON File

config = loader.load_json("config.json")

Set to Environment Variables

loader.set_to_env(".env")
# Now use os.getenv()
db_password = os.getenv("DATABASE_PASSWORD")

Decrypt Map

config = {
    "host": "localhost",
    "password": "ENC(encrypted_value)",
}

decrypted = enc.decrypt_map(config)
print(decrypted["password"])  # plaintext

Check if Value is Encrypted

from pycrypt import is_encrypted

if is_encrypted(value):
    decrypted = enc.decrypt_prefixed(value)

๐Ÿ’ป CLI Tool

Usage

# Encrypt a value
pycrypt encrypt -p mySecret -v "database_password"
# Output: ENC(base64value...)

# Decrypt a value
pycrypt decrypt -p mySecret -v "ENC(base64value...)"
# Output: database_password

# Encrypt all values in a file
pycrypt encrypt-file -p mySecret -i .env.plain -o .env.encrypted

# Decrypt all values in a file
pycrypt decrypt-file -p mySecret -i .env.encrypted -o .env.plain

# Use Jasypt-compatible algorithm
pycrypt encrypt -p mySecret -v "secret" --jasypt

# Use environment variable for password
export PYCRYPT_PASSWORD=mySecret
pycrypt encrypt -v "secret_value"

โš™๏ธ Advanced Configuration

Custom Options

# Encryptor (AES-256-GCM)
enc = Encryptor(
    password,
    iterations=50000,  # default: 10000
    salt_size=32,      # default: 16
    key_size=32,       # 32 = AES-256
)

# Jasypt Compatible
enc = JasyptEncryptor(
    password,
    iterations=2000,   # default: 1000
)

# Jasypt Strong
enc = JasyptStrongEncryptor(
    password,
    iterations=5000,
    salt_size=32,
)

๐Ÿ›ก๏ธ Security

Best Practices

  1. Never hardcode passwords - Use environment variables
  2. Rotate passwords regularly
  3. Use strong passwords (minimum 16 characters)
  4. Use Encryptor for new projects - More secure than Jasypt algorithms

๐Ÿ“š API Reference

Encryptor

enc = Encryptor(password, iterations=10000, salt_size=16, key_size=32)

encrypted = enc.encrypt(plaintext)           # Returns base64
encrypted = enc.encrypt_with_prefix(plaintext)  # Returns ENC(base64)
plaintext = enc.decrypt(base64_string)
plaintext = enc.decrypt_prefixed("ENC(...)")
decrypted_str = enc.decrypt_all_in_string(config_string)
decrypted_map = enc.decrypt_map({"key": "ENC(...)"})

JasyptEncryptor

enc = JasyptEncryptor(password, iterations=1000)
# Same methods as Encryptor

JasyptStrongEncryptor

enc = JasyptStrongEncryptor(password, iterations=1000, salt_size=16)
# Same methods as Encryptor

ConfigLoader

loader = ConfigLoader(password)
config = loader.load_env_file(".env")
config = loader.load_yaml("config.yaml")
config = loader.load_json("config.json")
loader.set_to_env(".env")

Utility Functions

from pycrypt import is_encrypted

is_encrypted("ENC(abc)")  # True
is_encrypted("plaintext")  # False

๐Ÿ“ Project Structure

pycrypt/
โ”œโ”€โ”€ src/pycrypt/
โ”‚   โ”œโ”€โ”€ __init__.py         # Package exports
โ”‚   โ”œโ”€โ”€ encryptor.py        # AES-256-GCM encryption
โ”‚   โ”œโ”€โ”€ jasypt_compat.py    # Jasypt compatibility
โ”‚   โ”œโ”€โ”€ config_loader.py    # Config file loader
โ”‚   โ”œโ”€โ”€ cli.py              # CLI tool
โ”‚   โ””โ”€โ”€ utils.py            # Utility functions
โ”œโ”€โ”€ tests/
โ”‚   โ”œโ”€โ”€ test_encryptor.py
โ”‚   โ””โ”€โ”€ test_jasypt_compat.py
โ”œโ”€โ”€ pyproject.toml
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ LICENSE
โ””โ”€โ”€ CLAUDE.md

๐Ÿงช Testing

# Install dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Run with coverage
pytest --cov=pycrypt

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.


๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


๐Ÿ”— Related Projects

  • GoCrypt - Jasypt-like encryption for Go
  • Jasypt - Original Java library

Made with โค๏ธ from Claude AI for Python developers who need Jasypt

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

pycrypt_jasypt-1.0.0.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

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

pycrypt_jasypt-1.0.0-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file pycrypt_jasypt-1.0.0.tar.gz.

File metadata

  • Download URL: pycrypt_jasypt-1.0.0.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for pycrypt_jasypt-1.0.0.tar.gz
Algorithm Hash digest
SHA256 151da0d363ee837480304ec2b6b5615470635d1095af03d38a86694d8bb866fc
MD5 a7570a428929d521291d4df75728d075
BLAKE2b-256 2bbe69b4de8a6f0048ff9389a1313c931908d2a3393e06afb7fda5ebc6a85cfa

See more details on using hashes here.

File details

Details for the file pycrypt_jasypt-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pycrypt_jasypt-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 14.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for pycrypt_jasypt-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1736d8a8d4688f8569f7d9742e459e6a22fd77d219b23d119fd6d1cd8b0e2e77
MD5 174b20c1b5d85a8ac5e6cc674decab32
BLAKE2b-256 327d6f9fe5982f031d0387f9b9a55528508da958b47e8910e1f3661669ccea68

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