farizfadian / phpcrypt
Jasypt-like encryption library for PHP - Encrypt your configuration with ENC(...) pattern
1.0.0
2026-02-26 20:30 UTC
Requires
- php: >=8.1
- ext-openssl: *
Requires (Dev)
- phpunit/phpunit: ^10.0
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 - Decrypt values encrypted by Java
- โ 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-platform - Works with GoCrypt, PyCrypt, NodeCrypt, and Java Jasypt
๐ฆ 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 | 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
<?php use PHPCrypt\Encryptor; use PHPCrypt\JasyptEncryptor; use PHPCrypt\JasyptStrongEncryptor; // RECOMMENDED: For new PHP projects $enc = new Encryptor($password); // For compatibility with Java Jasypt (default algorithm) $enc = new JasyptEncryptor($password); // For compatibility with Java Jasypt (strong encryption) $enc = new 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 โ
โ PyCrypt JasyptEnc โ JasyptEncryptor โ
YES โ
โ NodeCrypt JasyptEnc โ JasyptEncryptor โ
YES โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
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
๐ 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 ); // 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
๐ 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
โโโ 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
- Jasypt - Original Java library
Made with โค๏ธ from Claude AI for PHP developers who need Jasypt