PHP code example of kodhe / encryption

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

    

kodhe / encryption example snippets


spl_autoload_register(function ($class) {
    $prefix = 'Kodhe\\Encryption\\';
    $base_dir = APPPATH . 'libraries/Encryption/';

    if (strpos($class, $prefix) === 0) {
        $relative_class = substr($class, strlen($prefix));
        $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
        if (file_exists($file)) {
            

// In your controller or model
$this->load->library('encryption');

// Encrypt data
$encrypted = $this->encryption->encrypt('secret data');

// Decrypt data
$decrypted = $this->encryption->decrypt($encrypted);

$config = [
    'cipher' => 'aes-256',
    'mode' => 'gcm',  // or 'cbc', 'ctr'
    'key' => 'your-32-byte-secret-key-here!',
];

$this->encryption->initialize($config);
$encrypted = $this->encryption->encrypt('data');

// For BLOB columns in database
$encrypted = $this->encryption->encrypt($data, ['raw_data' => true]);
$decrypted = $this->encryption->decrypt($encrypted, ['raw_data' => true]);

$items = ['item1', 'item2', 'item3'];
$encrypted = $this->encryption->encrypt_many($items);
$decrypted = $this->encryption->decrypt_many($encrypted);

// Generate a 32-byte random key
$key = $this->encryption->create_key(32);

// Generate HKDF-derived key
$derived = $this->encryption->hkdf('master-key', 'sha256', 'salt', 32);

$config['encryption_key'] = 'your-32-byte-or-longer-secret-key';

$config['encryption'] = [
    'cipher'      => 'aes-256',
    'mode'        => 'gcm',
    'hmac_digest' => 'SHA512',
];

$params = [
    'cipher' => 'aes-256',      // aes-128, aes-256
    'mode' => 'gcm',            // cbc, gcm, ctr, ecb, ofb, cfb
    'key' => 'custom-key',      // Override default key
    'raw_data' => true,         // Return binary instead of base64
    'hmac_digest' => 'SHA512',  // sha256, sha384, sha512
    'hmac_key' => 'hmac-key',   // Override HMAC key
];

src/
├── Contracts/
│   ├── EncryptionInterface.php
│   └── HandlerInterface.php
├── Encoding/
│   ├── Base64Encoder.php
│   └── HexEncoder.php
├── Handlers/
│   └── OpenSslHandler.php
├── Key/
│   ├── KeyDeriver.php
│   └── KeyGenerator.php
├── Message/
│   └── EncryptedPayload.php
├── Support/
│   └── CipherAlgorithmResolver.php
└── Encryption.php