Search by

farizfadian / phpcrypt

farizfadian

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

Package info

github.com/farizfadian/phpcrypt

pkg:composer/farizfadian/phpcrypt

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.1 2026-11-19 02:43 UTC

This package is auto-updated.

Last update: 2026-11-19 03:03:07 UTC


README

CI Latest Version PHP Version License

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