Skip to content

farizfadian/phpcrypt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

PHPCrypt πŸ”

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 - 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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages