farizfadian / phpcrypt
Jasypt-like encryption library for PHP - Encrypt your configuration with ENC(...) pattern
Requires
- php: >=8.1
- ext-openssl: *
Requires (Dev)
- phpunit/phpunit: ^10.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Jasypt-like encryption library for PHP - Encrypt your application configuration with the familiar ENC(...) pattern used in Spring Boot applications.
Made with โค๏ธ from Claude AI for PHP developers who need Jasypt
๐ฏ Why PHPCrypt?
If you're coming from Java/Spring Boot world and need to share encrypted configuration across multiple languages, PHPCrypt is for you! It provides:
- โ
Familiar
ENC(...)pattern - Just like Jasypt in Spring Boot - โ
Java Jasypt compatibility - Reads and writes Jasypt's default algorithm (
PBEWithMD5AndDES) - โ Multiple algorithms - From legacy Jasypt to modern AES-256-GCM
- โ Laravel/Symfony ready - Easy integration with PHP frameworks
- โ CLI tool included - Encrypt/decrypt from command line
- โ Cross-language - The same values as GoCrypt, RustCrypt, NodeCrypt and PyCrypt, tested in CI against all four
๐ฆ Installation
composer require farizfadian/phpcrypt
๐ Quick Start
Basic Encryption/Decryption
<?php use PHPCrypt\Encryptor; // Create encryptor with password $enc = new Encryptor('mySecretPassword'); // Encrypt $encrypted = $enc->encryptWithPrefix('db_password_123'); echo $encrypted; // ENC(base64encodedvalue...) // Decrypt $decrypted = $enc->decryptPrefixed($encrypted); echo $decrypted; // db_password_123
Loading Encrypted Configuration
<?php use PHPCrypt\ConfigLoader; // .env file: // DATABASE_HOST=localhost // DATABASE_PASSWORD=ENC(AbCdEf123456...) $loader = new ConfigLoader(getenv('PHPCRYPT_PASSWORD')); $config = $loader->loadEnvFile('.env'); echo $config['DATABASE_PASSWORD']; // actual_password
๐ Encryption Algorithms
| Encryptor | Algorithm | Security | Java Jasypt can read it? | Use Case |
|---|---|---|---|---|
Encryptor |
AES-256-GCM + PBKDF2-HMAC-SHA256 | โญโญโญโญโญ | โ No | Recommended when only family libraries read the value |
JasyptStrongEncryptor |
AES-256-CBC + PBKDF2-HMAC-SHA256 | โญโญโญโญ | โ No (family format) | Stronger Jasypt-style mode shared inside the family |
JasyptEncryptor |
PBEWithMD5AndDES | โญโญ | โ Yes | Values shared with Java Jasypt |
Choose the Right Algorithm
<?php use PHPCrypt\Encryptor; use PHPCrypt\JasyptEncryptor; use PHPCrypt\JasyptStrongEncryptor; // RECOMMENDED: when only PHP/Go/Rust/Node/Python services read the value $enc = new Encryptor($password); // When Java Jasypt must read the value (Jasypt's default algorithm) $enc = new JasyptEncryptor($password); // Stronger CBC mode shared inside the family. Java Jasypt can NOT read it. $enc = new JasyptStrongEncryptor($password);
Rule of thumb: when a Java service reads the same configuration, always use JasyptEncryptor.
โ Java Jasypt Compatibility
โ ๏ธ Important: Compatibility Matrix
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ENCRYPT WITH โ DECRYPT WITH โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Java Jasypt PBEWithMD5AndDES โ JasyptEncryptor โ
YES โ
โ JasyptEncryptor โ Java Jasypt PBEWithMD5AndDES โ
YES โ
โ Java Jasypt (any algorithm) โ Encryptor โ NO โ
โ Encryptor โ Java Jasypt โ NO โ
โ Java Jasypt AES algorithms โ JasyptStrongEncryptor โ NO โ
โ JasyptStrongEncryptor โ Java Jasypt โ NO โ
โ GoCrypt / RustCrypt / NodeCrypt / PyCrypt, same mode โ
YES โ
โ (all three modes, both directions) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
JasyptStrongEncryptor is not Java compatible. It is the family's own strong format,
salt โ ciphertext with the IV derived by PBKDF2. Jasypt's PBEWithHmacSHA256AndAES_256
stores a random IV in the payload (salt โ iv โ ciphertext), so neither side reads the other.
It does not even fail loudly: CBC has no integrity check, and decrypting a real Jasypt value
with JasyptStrongEncryptor returned 16 bytes of garbage followed by the plaintext.
Which algorithm does your Java side use? JasyptEncryptor matches Jasypt 1.9.3 with
PBEWithMD5AndDES and no IV generator, which is what StandardPBEStringEncryptor and
BasicTextEncryptor do when left alone. jasypt-spring-boot 3.0.5 defaults to
PBEWITHHMACSHA512ANDAES_256 with a random IV instead, and no library in this family reads
that. To share values, the Java side has to use PBEWithMD5AndDES with
org.jasypt.iv.NoIvGenerator (in jasypt-spring-boot: jasypt.encryptor.algorithm and
jasypt.encryptor.iv-generator-classname). This was tested against the Jasypt library itself,
not inside a Spring Boot application.
Decrypt Values from Java
<?php use PHPCrypt\JasyptEncryptor; // Your Java application.properties has: // db.password=ENC(xxxFromJavaxxx) $enc = new JasyptEncryptor($samePasswordAsJava); $decrypted = $enc->decryptPrefixed('ENC(xxxFromJavaxxx)'); // โ Works!
Share Config with Go/Python/Node.js
<?php use PHPCrypt\JasyptEncryptor; // Use JasyptEncryptor so all languages can read $enc = new JasyptEncryptor($sharedPassword); $encrypted = $enc->encryptWithPrefix('shared_secret'); // This ENC(...) value can be decrypted by: // - PHP: using JasyptEncryptor // - Go: using gocrypt.NewJasyptEncryptor // - Python: using pycrypt.JasyptEncryptor // - Node.js: using nodecrypt.JasyptEncryptor // - Java: using Jasypt library
โ What has actually been tested
Only the rows below were executed. A format that matches on paper is not a test.
| Counterpart | Mode | Direction | Result |
|---|---|---|---|
| GoCrypt v1.0.0, golden vectors | default, --jasypt, --jasypt-strong |
GoCrypt โ PHPCrypt, and byte-identical output for a fixed salt | โ every CI job (DES vectors skipped on Windows with PHP 8.2/8.3) |
GoCrypt v1.0.0, Encryptor key sizes 16, 24 and 32 |
default | GoCrypt โ PHPCrypt, and byte-identical output for a fixed salt | โ every CI job |
| GoCrypt v1.0.0 | default, --jasypt, --jasypt-strong |
both | โ
CI interop job |
| RustCrypt v1.0.0 | default, --jasypt, --jasypt-strong |
both | โ
CI interop job |
| NodeCrypt 1.0.1 | default, --jasypt, --jasypt-strong |
both | โ
CI interop job |
| PyCrypt 1.0.1 | default, --jasypt, --jasypt-strong |
both | โ
CI interop job |
Java Jasypt 1.9.3 on JDK 25, PBEWithMD5AndDES |
--jasypt |
both | โ
CI interop job |
Java Jasypt 1.9.3, PBEWithHmacSHA256AndAES_256 |
--jasypt-strong |
both | โ not compatible, by design of the family format |
Each live round trip covers ASCII, punctuation, a 200-character value, non-ASCII text with an
emoji, and a non-ASCII password. The interop job runs on Linux with the OpenSSL legacy provider
enabled and fails if any test is skipped. The same tests passed on Windows (PHP 8.5.12,
OpenSSL 3.5.9, JDK 25) on 2026-11-19. Do not use PyCrypt 1.0.0 with the default mode: its values
cannot be read by the other libraries (fixed in PyCrypt 1.0.1).
Non-ASCII passwords and Java. PHPCrypt and the rest of the family use the UTF-8 bytes of the
password. Java decides for itself: with PBEWithMD5AndDES, JDK 21 rejects a non-ASCII password
outright (Password is not ASCII), while JDK 25 accepts it and agrees with the family. If a Java
service on an older JDK must read the value, use an ASCII password.
๐ JasyptEncryptor needs the OpenSSL legacy provider
PBEWithMD5AndDES uses DES, which OpenSSL 3 only offers through its legacy provider. Without it
JasyptEncryptor throws: Encryption failed from encrypt(), and from decrypt() the misleading
Decryption failed: invalid password or corrupted data. If you see that with a password you know
is right, check the provider first. Point OPENSSL_CONF at a file like this before PHP starts:
openssl_conf = openssl_init [openssl_init] providers = provider_sect [provider_sect] default = default_sect legacy = legacy_sect [default_sect] activate = 1 [legacy_sect] activate = 1
On Windows also set OPENSSL_MODULES to the folder that holds legacy.dll (the official PHP
builds ship it in extras\ssl). Encryptor and JasyptStrongEncryptor do not need any of this.
๐ Usage Guide
Configuration Files
.env File
# config.env DATABASE_HOST=localhost DATABASE_PASSWORD=ENC(AbCdEf123456...) API_KEY=ENC(XyZ789...)
<?php use PHPCrypt\ConfigLoader; $loader = new ConfigLoader($password); $config = $loader->loadEnvFile('config.env'); echo $config['DATABASE_PASSWORD']; // decrypted value
JSON File
$config = $loader->loadJson('config.json');
Set to Environment Variables
$loader->setToEnv('.env'); // Now use getenv() echo getenv('DATABASE_PASSWORD');
Decrypt Map
$config = [ 'host' => 'localhost', 'password' => 'ENC(encrypted_value)', ]; $decrypted = $enc->decryptMap($config); echo $decrypted['password']; // plaintext
Check if Value is Encrypted
<?php use PHPCrypt\Utils; if (Utils::isEncrypted($value)) { $decrypted = $enc->decryptPrefixed($value); }
๐ง Framework Integration
Laravel
// In config/app.php or a service provider use PHPCrypt\JasyptEncryptor; $enc = new JasyptEncryptor(env('PHPCRYPT_PASSWORD')); $dbPassword = $enc->decryptPrefixed(env('DATABASE_PASSWORD')); // Or create a facade/service
Symfony
// In services.yaml services: PHPCrypt\JasyptEncryptor: arguments: $password: '%env(PHPCRYPT_PASSWORD)%'
๐ป CLI Tool
Usage
# Encrypt a value ./vendor/bin/phpcrypt encrypt -p mySecret -v "database_password" # Output: ENC(base64value...) # Decrypt a value ./vendor/bin/phpcrypt decrypt -p mySecret -v "ENC(base64value...)" # Output: database_password # Encrypt all values in a file ./vendor/bin/phpcrypt encrypt-file -p mySecret -i .env.plain -o .env.encrypted # Decrypt all values in a file ./vendor/bin/phpcrypt decrypt-file -p mySecret -i .env.encrypted -o .env.plain # Use Jasypt-compatible algorithm ./vendor/bin/phpcrypt encrypt -p mySecret -v "secret" --jasypt # Use environment variable for password export PHPCRYPT_PASSWORD=mySecret ./vendor/bin/phpcrypt encrypt -v "secret_value"
โ๏ธ Advanced Configuration
Custom Options
// Encryptor (AES-256-GCM) $enc = new Encryptor( password: $password, iterations: 50000, // default: 10000 saltSize: 32, // default: 16 keySize: 32 // 32 = AES-256 (default), 24 = AES-192, 16 = AES-128 ); // Jasypt Compatible $enc = new JasyptEncryptor( password: $password, iterations: 2000 // default: 1000 ); // Jasypt Strong $enc = new JasyptStrongEncryptor( password: $password, iterations: 5000, saltSize: 32 );
๐ API Reference
Encryptor
$enc = new Encryptor($password, $iterations, $saltSize, $keySize); $enc->encrypt($plaintext); // Returns base64 $enc->encryptWithPrefix($plaintext); // Returns ENC(base64) $enc->decrypt($base64); $enc->decryptPrefixed($value); $enc->decryptAllInString($input); $enc->decryptMap($config);
JasyptEncryptor
$enc = new JasyptEncryptor($password, $iterations); // Same methods as Encryptor
JasyptStrongEncryptor
$enc = new JasyptStrongEncryptor($password, $iterations, $saltSize); // Same methods as Encryptor
ConfigLoader
$loader = new ConfigLoader($password, $iterations); $loader->loadEnvFile($filepath); $loader->loadJson($filepath); $loader->setToEnv($filepath);
Utility Functions
use PHPCrypt\Utils; Utils::isEncrypted('ENC(abc)'); // true Utils::isEncrypted('plaintext'); // false
๐งช Testing
# Install dependencies composer install # Run tests composer test # Run with coverage composer test-coverage
Tests that need DES are skipped when the OpenSSL legacy provider is not loaded, and the live cross-language tests are skipped unless you tell them where the other tools are:
export GOCRYPT_CLI=/path/to/gocrypt-cli # github.com/farizfadian/gocrypt releases export RUSTCRYPT_CLI=/path/to/rustcrypt # github.com/farizfadian/rustcrypt releases export NODECRYPT_CLI=/path/to/nodecrypt # npm install -g nodecrypt-jasypt export NODE_OPTIONS=--openssl-legacy-provider # NodeCrypt refuses --jasypt without it export PYCRYPT_CLI=/path/to/pycrypt # pip install pycrypt-jasypt export JASYPT_JAR=/path/to/jasypt-1.9.3.jar # org.jasypt:jasypt on Maven Central, needs java on PATH # Fails instead of skipping when something is missing vendor/bin/phpunit --fail-on-skipped
๐ Project Structure
phpcrypt/
โโโ src/
โ โโโ Encryptor.php # AES-256-GCM encryption
โ โโโ JasyptEncryptor.php # Jasypt compatibility
โ โโโ JasyptStrongEncryptor.php
โ โโโ ConfigLoader.php # Config file loader
โ โโโ Utils.php # Utility functions
โโโ bin/
โ โโโ phpcrypt # CLI tool
โโโ tests/
โ โโโ EncryptorTest.php
โ โโโ JasyptCompatTest.php
โ โโโ UtilsTest.php
โ โโโ CliTest.php # bin/phpcrypt end to end
โ โโโ GoldenVectorTest.php # GoCrypt golden vectors
โ โโโ BackwardCompatibilityTest.php # values written by PHPCrypt 1.0.0
โ โโโ LiveInteropTest.php # live round trips with the other libraries
โ โโโ interop/JasyptTool.java # drives the real Jasypt jar
โ โโโ fixtures/ # GoCrypt vectors, and values written by PHPCrypt 1.0.0
โโโ composer.json
โโโ phpunit.xml
โโโ README.md
โโโ LICENSE
๐ค 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
- PyCrypt - Jasypt-like encryption for Python
- NodeCrypt - Jasypt-like encryption for Node.js
- RustCrypt - Jasypt-like encryption for Rust
- Jasypt - Original Java library
Made with โค๏ธ from Claude AI for PHP developers who need Jasypt