Skip to main content

Crate rustcrypt_jasypt

Crate rustcrypt_jasypt 

Source
Expand description

§rustcrypt-jasypt

Jasypt-compatible ENC(...) encryption for Rust configuration: the 🦀 Rust member of a cross-language family whose members all read each other’s values.

LibraryLanguagePackage
GoCryptGogithub.com/farizfadian/gocrypt
PyCryptPythonpycrypt-jasypt
NodeCryptNode.jsnodecrypt-jasypt
PHPCryptPHPfarizfadian/phpcrypt
RustCrypt (this crate)Rustrustcrypt-jasypt
JasyptJavathe original

One .env / YAML / JSON file containing ENC(...) values can therefore be shared by services written in any of these languages.

§Three encryptors

TypeAlgorithmReadable by Java JasyptUse when
EncryptorAES-256-GCM, PBKDF2-HMAC-SHA256noonly family members read the value; authenticated encryption
JasyptEncryptorPBEWithMD5AndDESyesthe config is shared with Java Jasypt (e.g. a JasperReport service)
JasyptStrongEncryptorPBKDF2-HMAC-SHA256, AES-256-CBCfamily format onlya stronger Jasypt-style mode shared inside the family

§Quick start

use rustcrypt_jasypt::{Encryptor, JasyptEncryptor};

// Family-internal AES-256-GCM
let enc = Encryptor::new("my-password")?;
let wrapped = enc.encrypt_with_prefix("db_password_123")?; // "ENC(...)"
assert_eq!(enc.decrypt_prefixed(&wrapped)?, "db_password_123");

// Java-compatible PBEWithMD5AndDES
let jasypt = JasyptEncryptor::new("my-password")?;
let wrapped = jasypt.encrypt_with_prefix("shared-secret")?;
assert_eq!(jasypt.decrypt_prefixed(&wrapped)?, "shared-secret");

// Builder-style options (both sides must agree)
let tuned = Encryptor::new("my-password")?
    .with_iterations(50_000)
    .with_salt_size(32);

§Helpers

use std::collections::HashMap;
use rustcrypt_jasypt::{is_encrypted, Encryptor};

let enc = Encryptor::new("my-password")?;
let secret = enc.encrypt_with_prefix("s3cr3t")?;
assert!(is_encrypted(&secret));

// Replace every ENC(...) inside a block of text
let text = format!("DB_HOST=localhost\nDB_PASS={secret}\n");
assert_eq!(
    enc.decrypt_all_in_string(&text)?,
    "DB_HOST=localhost\nDB_PASS=s3cr3t\n"
);

// Decrypt only the ENC(...) values of a map
let mut map = HashMap::new();
map.insert("host".to_string(), "localhost".to_string());
map.insert("password".to_string(), secret);
let decrypted = enc.decrypt_map(&map)?;
assert_eq!(decrypted["host"], "localhost");
assert_eq!(decrypted["password"], "s3cr3t");

§Config files (feature config, enabled by default)

use rustcrypt_jasypt::{ConfigLoader, JasyptEncryptor};

// AES-256-GCM, like GoCrypt's NewConfigLoader
let loader = ConfigLoader::new("my-password")?;
let config = loader.load_env_file(".env")?;     // HashMap<String, String>
let yaml = loader.load_yaml("config.yml")?;     // flat key: value lines
loader.set_to_env(".env")?;                     // export to std::env

// Jasypt mode for files shared with Java
let loader = ConfigLoader::with_encryptor(JasyptEncryptor::new("my-password")?);
let shared = loader.load_env_file("shared.env")?;

load_json deserialises into any serde type after decrypting every ENC(...) string at any depth; see ConfigLoader for details.

§Generic code

All three encryptors implement StringEncryptor, so code can be written once for any of them (or for Box<dyn StringEncryptor>):

use rustcrypt_jasypt::{Encryptor, JasyptEncryptor, StringEncryptor};

fn roundtrip<E: StringEncryptor>(enc: &E) -> rustcrypt_jasypt::Result<String> {
    let wrapped = enc.encrypt_with_prefix("value")?;
    enc.decrypt_prefixed(&wrapped)
}

assert_eq!(roundtrip(&Encryptor::new("pw")?)?, "value");
assert_eq!(roundtrip(&JasyptEncryptor::new("pw")?)?, "value");
let boxed: Box<dyn StringEncryptor> = Box::new(Encryptor::new("pw")?);
assert_eq!(roundtrip(&boxed)?, "value");

§Command line (feature cli, enabled by default)

cargo install rustcrypt-jasypt

rustcrypt encrypt      -p mySecret -v "db_password"            # ENC(...)
rustcrypt encrypt      -p mySecret -v "db_password" --jasypt   # Java-readable
rustcrypt decrypt      -p mySecret -v "ENC(...)"
rustcrypt encrypt-file -p mySecret -i .env.plain -o .env.encrypted
rustcrypt decrypt-file -p mySecret -i .env.encrypted -o -

The password can also come from the RUSTCRYPT_PASSWORD environment variable.

§Feature flags

FeatureDefaultAdds
configyesConfigLoader (serde, serde_json)
cliyesthe rustcrypt binary (clap); implies config

With default-features = false only the encryptors and helpers remain, backed purely by RustCrypto crates (no OpenSSL, no C code).

§Wire formats

Every payload is standard, padded base64 and is wrapped as ENC(base64) by the *_with_prefix methods. All three formats are byte-identical to GoCrypt (the reference implementation) and are verified against vectors generated by it in this crate’s test suite.

§Known issues

  • JasyptStrongEncryptor uses the family’s layout, which is not byte-identical to Java Jasypt’s real PBEWithHmacSHA256AndAES_256 (Java prepends a random IV). It is fully interoperable with GoCrypt, PyCrypt, NodeCrypt and PHPCrypt.
  • JasyptEncryptor relies on DES and MD5, which are weak by modern standards, and has no integrity check: a wrong password can occasionally yield garbage instead of an error. Use it for Java compatibility only.

Re-exports§

pub use error::Error;
pub use error::Result;

Modules§

error
Error type shared by every encryptor and loader in the crate.

Structs§

ConfigLoader
Loads configuration files and transparently decrypts every ENC(...) value with the configured encryptor.
Encryptor
AES-256-GCM encryptor with PBKDF2-HMAC-SHA256 key derivation.
JasyptEncryptor
Encryptor compatible with Java Jasypt’s default algorithm, PBEWithMD5AndDES.
JasyptStrongEncryptor
Stronger Jasypt-style encryptor: PBKDF2-HMAC-SHA256 + AES-256-CBC.

Constants§

DEFAULT_ITERATIONS
Default PBKDF2 iteration count for Encryptor.
DEFAULT_KEY_SIZE
Default AES key size in bytes for Encryptor (32 = AES-256).
DEFAULT_SALT_SIZE
Default salt size in bytes for Encryptor.
ENC_PREFIX
Prefix of a wrapped encrypted value (ENC().
ENC_SUFFIX
Suffix of a wrapped encrypted value ()).
JASYPT_DEFAULT_ITERATIONS
Default iteration count for JasyptEncryptor (Jasypt’s default).
JASYPT_SALT_SIZE
Salt size in bytes used by JasyptEncryptor (fixed at 8 for DES).
JASYPT_STRONG_DEFAULT_ITERATIONS
Default iteration count for JasyptStrongEncryptor.
JASYPT_STRONG_DEFAULT_SALT_SIZE
Default salt size in bytes for JasyptStrongEncryptor.

Traits§

StringEncryptor
Common interface implemented by Encryptor, JasyptEncryptor and JasyptStrongEncryptor.

Functions§

is_encrypted
Returns true when value (ignoring surrounding whitespace) looks like an ENC(...) wrapped value.