RustCrypt ๐ฆ๐
Jasypt-like encryption library for Rust - Encrypt your application configuration with the familiar ENC(...) pattern used in Spring Boot applications, and share it with Go, Python, Node.js, PHP and Java.
Made with โค๏ธ from Claude AI for Rust developers who need Jasypt
๐ฏ Why RustCrypt?
If you are coming from the Java/Spring Boot world, or you run services in several languages that must read the same encrypted configuration, RustCrypt is for you! It provides:
- โ
Familiar
ENC(...)pattern - Just like Jasypt in Spring Boot - โ Cross-language - Byte-compatible with GoCrypt, PyCrypt, NodeCrypt, PHPCrypt and Java Jasypt
- โ Multiple algorithms - From legacy Jasypt compatibility to modern AES-256-GCM
- โ Pure Rust - RustCrypto crates only; no OpenSSL, no C build step, DES works everywhere
- โ
Config loaders -
.env, YAML and JSON with automatic decryption - โ
CLI tool included -
rustcryptmirrorsgocrypt-cli - โ Tested against GoCrypt - golden vectors generated by the reference implementation
๐ Cross-Language Family (ALL SAME ENC() FORMAT!)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Jasypt Multi-Language Encryption Family โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ ๐น GoCrypt github.com/farizfadian/gocrypt (Go) โ
โ ๐ PyCrypt github.com/farizfadian/pycrypt (Python) โ
โ ๐ฆ NodeCrypt github.com/farizfadian/nodecrypt (Node.js) โ
โ ๐ PHPCrypt github.com/farizfadian/phpcrypt (PHP) โ
โ ๐ฆ RustCrypt github.com/farizfadian/rustcrypt (Rust) โ
โ โ Jasypt jasypt.org (Java) โ
โ โ
โ ALL USE THE SAME ENC(...) FORMAT! โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
| Library | Language | Install |
|---|---|---|
| GoCrypt | Go 1.21+ | go get github.com/farizfadian/gocrypt |
| PyCrypt | Python 3.8+ | pip install pycrypt-jasypt |
| NodeCrypt | Node 18+ | npm i nodecrypt-jasypt |
| PHPCrypt | PHP 8.1+ | composer require farizfadian/phpcrypt |
| RustCrypt | Rust 1.75+ | cargo add rustcrypt-jasypt |
| Jasypt | Java | Maven org.jasypt:jasypt |
๐ฆ Installation
Or in Cargo.toml:
[]
= "1.0"
# Library only (no ConfigLoader, no CLI dependencies):
= { = "1.0", = false }
The crate name on crates.io is rustcrypt-jasypt; in code it is rustcrypt_jasypt.
Install the CLI:
๐ Quick Start
Basic Encryption/Decryption
use Encryptor;
Loading Encrypted Configuration
use ConfigLoader;
// .env file:
// DATABASE_HOST=localhost
// DATABASE_PASSWORD=ENC(AbCdEf123456...)
let loader = new?;
let config = loader.load_env_file?;
println!; // actual_password
๐ Encryption Algorithms
RustCrypt provides three encryptors for different use cases:
| Encryptor | Algorithm | Security | Java compatible? | Use Case |
|---|---|---|---|---|
Encryptor |
AES-256-GCM + PBKDF2-HMAC-SHA256 | โญโญโญโญโญ | โ No | Recommended when only family members read the value |
JasyptStrongEncryptor |
PBKDF2-HMAC-SHA256 + AES-256-CBC | โญโญโญโญ | family format | Stronger Jasypt-style mode shared inside the family |
JasyptEncryptor |
PBEWithMD5AndDES | โญโญ | โ Yes | Legacy Jasypt compatibility (Java) |
Which encryptor should I use?
use ;
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// RECOMMENDED: only Rust/Go/Python/Node/PHP services read the value
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
let enc = new?;
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// The value is shared with Java Jasypt (default algorithm), e.g. a JasperReport service
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
let enc = new?;
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// Stronger Jasypt-style mode, shared inside the family
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
let enc = new?;
Rule of thumb: when a Java service (for example JasperReport) reads the same .env, always use JasyptEncryptor. Encryptor values cannot be read by Java.
๐ Usage Guide
1. Configuration Files
.env File
# config.env
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_PASSWORD=ENC(AbCdEf123456...)
API_KEY=ENC(XyZ789...)
let loader = new?;
let config = loader.load_env_file?;
println!; // decrypted value
YAML File (flat, same limitation as GoCrypt)
# config.yaml
database:
host: localhost
password: ENC(AbCdEf123456...)
let config = loader.load_yaml?;
println!; // keys are flattened
JSON File (serde, nested objects and arrays)
use Deserialize;
let config: Config = loader.load_json?;
println!;
Every ENC(...) string at any depth is decrypted; strings that fail to decrypt are kept as-is.
Jasypt mode for files shared with Java
use ;
let loader = with_encryptor;
let config = loader.load_env_file?;
2. Set to Environment Variables
let loader = new?;
loader.set_to_env?; // load and export every pair (call during start-up)
let db_password = var?;
3. Decrypt Map
use HashMap;
let mut config = new;
config.insert;
config.insert;
let decrypted = enc.decrypt_map?;
println!; // plaintext
4. Decrypt All in String
let input = "\
DATABASE_HOST=localhost
DATABASE_PASSWORD=ENC(encrypted_value)
API_KEY=ENC(another_encrypted_value)
";
let decrypted = enc.decrypt_all_in_string?; // Err on the first failure (GoCrypt semantics)
let best_effort = enc.decrypt_all_in_string_lossy; // keeps failing ENC(...) verbatim
5. Check if Value is Encrypted
use is_encrypted;
if is_encrypted
6. Generic code
All encryptors implement the StringEncryptor trait:
use StringEncryptor;
let boxed: = Boxnew;
๐ป CLI Tool
# or download a release archive from GitHub Releases
# Encrypt a value
# Output: ENC(base64value...)
# Decrypt a value
# Output: database_password
# Encrypt all KEY=VALUE values in a file
# Decrypt all ENC(...) values in a file (omit -o or use "-" for stdout)
# Use the Java Jasypt compatible algorithm
# Use the strong algorithm
# Raw base64 without the ENC(...) wrapper
# Use an environment variable for the password
The commands, flags, messages and exit codes mirror gocrypt-cli, pycrypt and nodecrypt.
โ Cross-Language Compatibility
Compatibility Matrix
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ENCRYPT WITH โ DECRYPT WITH โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Java Jasypt (default) โ JasyptEncryptor โ
YES โ
โ GoCrypt JasyptEnc โ JasyptEncryptor โ
YES โ
โ PyCrypt JasyptEnc โ JasyptEncryptor โ
YES โ
โ NodeCrypt JasyptEnc โ JasyptEncryptor โ
YES โ
โ PHPCrypt JasyptEnc โ JasyptEncryptor โ
YES โ
โ GoCrypt Encryptor โ Encryptor โ
YES โ
โ GoCrypt StrongEnc โ JasyptStrongEncryptor โ
YES โ
โ JasyptEncryptor โ Java Jasypt โ
YES โ
โ Encryptor โ Java Jasypt โ NO โ
โ Java Jasypt (default) โ Encryptor โ NO โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Example: encrypt in Go, decrypt in Rust
// Go (GoCrypt)
enc, _ := gocrypt.NewJasyptEncryptor("sharedPassword")
value, _ := enc.EncryptWithPrefix("hello") // ENC(sIAlbZnxY3KGqbnsHk+t2w==)
// Rust (RustCrypt)
use JasyptEncryptor;
let enc = new?;
let plaintext = enc.decrypt_prefixed?; // "hello"
# Python (PyCrypt)
# "hello"
// Java (Jasypt)
StandardPBEStringEncryptor enc ;
enc.;
enc.;
enc.; // "hello"
Requirement: every side must use the same password and the same algorithm (and the same iteration count / salt size when customised).
How compatibility is verified
tests/fixtures/gocrypt_vectors.jsonholds values produced by GoCrypt v1.0.0 for all three modes (ASCII, punctuation, 200 characters, UTF-8 with emoji), plus fixed-salt vectors computed with GoCrypt's own KDF and cipher code.cargo testasserts that RustCrypt decrypts every one of them and reproduces the fixed-salt outputs byte-for-byte.cargo test -- --ignored(withgocrypt-clionPATHorGOCRYPT_CLI=/path/to/gocrypt-cli) round-trips live in both directions.scripts/cross-check.ps1runs the same round trips againstgocrypt-cli,pycryptandnodecryptwhen they are installed.
โ๏ธ Advanced Configuration
// AES-256-GCM
let enc = new?
.with_iterations // default: 10_000
.with_salt_size // default: 16
.with_key_size?; // 16 = AES-128, 24 = AES-192, 32 = AES-256 (default)
// Jasypt compatible: match the Java side's keyObtentionIterations
let enc = new?
.with_iterations; // default: 1_000
// Jasypt strong
let enc = new?
.with_iterations // default: 1_000
.with_salt_size; // default: 16
Parameters are not stored in the ciphertext, so both sides must use the same values.
๐ก๏ธ Security
Algorithm Comparison
| Algorithm | Cipher | Key Derivation | Integrity | Security |
|---|---|---|---|---|
Encryptor |
AES-256-GCM | PBKDF2-HMAC-SHA256 (10 000) | AEAD tag | โ Strong |
JasyptStrongEncryptor |
AES-256-CBC | PBKDF2-HMAC-SHA256 (1 000) | none (padding only) | โ Good |
JasyptEncryptor |
DES-CBC | PBKDF1-MD5 (1 000) | none (padding only) | โ ๏ธ Legacy |
Best Practices
- Never hardcode passwords - Use environment variables (
RUSTCRYPT_PASSWORD) - Rotate passwords regularly
- Use strong passwords (minimum 16 characters)
- Store encrypted config in version control, not plaintext
- Use
Encryptorunless Java must read the value - it is the only mode with authentication
Please report vulnerabilities as described in SECURITY.md.
โ ๏ธ Known Issues
JasyptStrongEncryptoris not byte-identical to Java Jasypt'sPBEWithHmacSHA256AndAES_256. Real Jasypt prepends a random IV (salt โ iv โ ciphertext); the family format derives the IV from PBKDF2 (salt โ ciphertext). It is fully interoperable with GoCrypt, PyCrypt, NodeCrypt and PHPCrypt, and will only change in a coordinated release of all five libraries.JasyptEncryptoruses DES and MD5, which are weak by modern standards, and DES-CBC has no integrity check: decrypting with a wrong password usually fails withError::DecryptionFailedbut can occasionally return garbage. Use it for Java compatibility only.ConfigLoader::set_to_envmutates the process environment. Call it during single-threaded start-up, before other threads read environment variables.load_yamlis a flat line parser (key: value), the same simplification as GoCrypt. Use a YAML crate plusdecrypt_mapfor complex documents.
๐ API Reference
Full documentation: https://docs.rs/rustcrypt-jasypt
// Constructors (all return Result; empty password -> Error::EmptyPassword)
new .with_iterations .with_salt_size .with_key_size?
new .with_iterations
new .with_iterations .with_salt_size
// Methods (inherent on every encryptor and on the StringEncryptor trait)
: &str = "ENC(";
const ENC_SUFFIX: &str = ")";
// ConfigLoader (feature "config", default)
new // Errors (thiserror, #[non_exhaustive])
๐ Project Structure
rustcrypt/
โโโ Cargo.toml # crate: rustcrypt-jasypt, bin: rustcrypt
โโโ src/
โ โโโ lib.rs # crate docs + re-exports
โ โโโ error.rs # Error / Result
โ โโโ kdf.rs # PBKDF2-HMAC-SHA256, PBKDF1-MD5
โ โโโ prefix.rs # ENC(...) helpers + StringEncryptor trait
โ โโโ encryptor.rs # Encryptor (AES-256-GCM)
โ โโโ jasypt.rs # JasyptEncryptor + JasyptStrongEncryptor
โ โโโ config_loader.rs # ConfigLoader (feature "config")
โ โโโ golden.rs # fixed-salt tests against GoCrypt vectors
โ โโโ bin/rustcrypt.rs # CLI (feature "cli")
โโโ tests/
โ โโโ roundtrip.rs # round trips, tampering, error paths
โ โโโ cross_language.rs # GoCrypt golden vectors + live gocrypt-cli
โ โโโ config_loader.rs
โ โโโ cli.rs
โ โโโ fixtures/gocrypt_vectors.json
โโโ examples/ # basic, jasypt_compat, config_loader
โโโ scripts/
โ โโโ gen-vectors/ # Go program that regenerates the fixture
โ โโโ cross-check.ps1 # round trips against sibling CLIs
โโโ .github/workflows/ # ci.yml, release.yml
๐งช Testing
# Regenerate the GoCrypt fixture (needs Go)
&&
๐ค Contributing
Contributions are welcome! Please read CONTRIBUTING.md first.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'feat: add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Anything that changes a wire format must stay byte-compatible with GoCrypt.
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
- Inspired by Jasypt - Java Simplified Encryption
- Built on the pure-Rust RustCrypto crates
- Sibling libraries: GoCrypt, PyCrypt, NodeCrypt, PHPCrypt