PHP code example of farizfadian / phpcrypt

1. Go to this page and download the library: Download farizfadian/phpcrypt library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

farizfadian / phpcrypt example snippets




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



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



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



use PHPCrypt\JasyptEncryptor;

// Your Java application.properties has:
// db.password=ENC(xxxFromJavaxxx)

$enc = new JasyptEncryptor($samePasswordAsJava);
$decrypted = $enc->decryptPrefixed('ENC(xxxFromJavaxxx)'); // ✅ Works!



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



use PHPCrypt\ConfigLoader;

$loader = new ConfigLoader($password);
$config = $loader->loadEnvFile('config.env');
echo $config['DATABASE_PASSWORD']; // decrypted value

$config = $loader->loadJson('config.json');

$loader->setToEnv('.env');
// Now use getenv()
echo getenv('DATABASE_PASSWORD');

$config = [
    'host' => 'localhost',
    'password' => 'ENC(encrypted_value)',
];

$decrypted = $enc->decryptMap($config);
echo $decrypted['password']; // plaintext



use PHPCrypt\Utils;

if (Utils::isEncrypted($value)) {
    $decrypted = $enc->decryptPrefixed($value);
}

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

// In services.yaml
services:
    PHPCrypt\JasyptEncryptor:
        arguments:
            $password: '%env(PHPCRYPT_PASSWORD)%'

// 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
);

$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);

$enc = new JasyptEncryptor($password, $iterations);
// Same methods as Encryptor

$enc = new JasyptStrongEncryptor($password, $iterations, $saltSize);
// Same methods as Encryptor

$loader = new ConfigLoader($password, $iterations);

$loader->loadEnvFile($filepath);
$loader->loadJson($filepath);
$loader->setToEnv($filepath);

use PHPCrypt\Utils;

Utils::isEncrypted('ENC(abc)');  // true
Utils::isEncrypted('plaintext'); // false

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