PHP code example of initphp / encryption

1. Go to this page and download the library: Download initphp/encryption 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/ */

    

initphp / encryption example snippets




nitPHP\Encryption\Encrypt;
use InitPHP\Encryption\OpenSSL;

$handler = Encrypt::use(OpenSSL::class, [
    'key' => getenv('APP_ENCRYPTION_KEY'),
]);

$ciphertext = $handler->encrypt(['user_id' => 42, 'role' => 'admin']);
// → "02006f1c…": hex string, safe to store in cookies / DBs / URL params

$plaintext = $handler->decrypt($ciphertext);
// → ['user_id' => 42, 'role' => 'admin']

use InitPHP\Encryption\Encrypt;
use InitPHP\Encryption\Sodium;

$handler = Encrypt::use(Sodium::class, [
    'key' => getenv('APP_ENCRYPTION_KEY'),
]);

$ciphertext = $handler->encrypt('a secret message');
$plaintext  = $handler->decrypt($ciphertext);

// 1) Per-call override
$handler->encrypt($data, ['cipher' => 'AES-256-GCM']);

// 2) Mutated on the handler
$handler->setOption('cipher', 'AES-256-GCM');
$handler->setOptions(['cipher' => 'AES-256-GCM', 'algo' => 'SHA512']);

// 3) Constructor / factory
$handler = Encrypt::use(OpenSSL::class, ['cipher' => 'AES-256-GCM']);

namespace App\Crypto;

use InitPHP\Encryption\BaseHandler;
use InitPHP\Encryption\Exceptions\EncryptionException;

final class MyHandler extends BaseHandler
{
    public function encrypt(mixed $data, array $options = []): string
    {
        $options       = $this->resolveOptions($options);
        $key           = $this->solveOptions($options);
        $key           = $this->

use InitPHP\Encryption\Exceptions\EncryptionException;

try {
    $plaintext = $handler->decrypt($incoming);
} catch (EncryptionException $e) {
    // Bad input, tampered ciphertext, missing key, unsupported format
    // version, unknown cipher or hash algorithm, …
    $logger->warning('decrypt failed', ['reason' => $e->getMessage()]);
}