PHP code example of eloquent / lockbox

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

    

eloquent / lockbox example snippets


use Eloquent\Lockbox\Key\Generator\KeyGenerator;
use Eloquent\Lockbox\Key\Persistence\KeyWriter;

$keyGenerator = new KeyGenerator;
$key = $keyGenerator->generateKey();

$keyPath = '/path/to/lockbox.key';
$keyWriter = new KeyWriter;
$keyWriter->writeFile($key, $keyPath);

use Eloquent\Lockbox\Encrypter;
use Eloquent\Lockbox\Key\Persistence\KeyReader;

$keyPath = '/path/to/lockbox.key';
$keyReader = new KeyReader;
$key = $keyReader->readFile($keyPath);

$encrypter = new Encrypter;
echo $encrypter->encrypt($key, 'Super secret data.');

use Eloquent\Lockbox\Bound\BoundEncrypter;
use Eloquent\Lockbox\Key\Persistence\KeyReader;

$keyPath = '/path/to/lockbox.key';
$keyReader = new KeyReader;
$key = $keyReader->readFile($keyPath);

$encrypter = new BoundEncrypter($key);

echo $encrypter->encrypt('Super secret data.');
echo $encrypter->encrypt('Extra secret data.');
echo $encrypter->encrypt('Mega secret data.');

use Eloquent\Lockbox\Decrypter;
use Eloquent\Lockbox\Key\Persistence\KeyReader;

$keyPath = '/path/to/lockbox.key';
$keyReader = new KeyReader;
$key = $keyReader->readFile($keyPath);

$decrypter = new Decrypter;

$encrypted =
    'AQF_VjJS9sAL75uuUP_HTu9Do_3itIDaHLLXmh_JLOBRqQeZ_hnDwht4WtEkz3io' .
    'iW0WIHb3lANyKqpShyiPcVdj_DbfYiIPEWab8e3vqwEUvoqFFNo';

$result = $decrypter->decrypt($key, $encrypted);
if ($result->isSuccessful()) {
    echo $result->data();
} else {
    echo 'Decryption failed.';
}

use Eloquent\Lockbox\Bound\BoundDecrypter;
use Eloquent\Lockbox\Key\Persistence\KeyReader;

$keyPath = '/path/to/lockbox.key';
$keyReader = new KeyReader;
$key = $keyReader->readFile($keyPath);

$decrypter = new BoundDecrypter($key);

$encrypted = array(
    'AQF_VjJS9sAL75uuUP_HTu9Do_3itIDaHLLXmh_JLOBRqQeZ_hnDwht4WtEkz3io' .
    'iW0WIHb3lANyKqpShyiPcVdj_DbfYiIPEWab8e3vqwEUvoqFFNo',
    'AQH44yTs7va1cDoBpX0xVLqIRow5fs8Jj5-DYDJ1R3YY9udBCexmvDs9BH1qJDjC' .
    'RSqcGriKi_UkL5per1WHwdxWuPq8QsYiBqeC9e9zypl0Xi1QT3s',
    'AQGg0MsYtH0Rboyqssivssupb_GKlBotCpdFtc6WpnMaji8_ZvmGUTRu2DKkxFhA' .
    'dk_s0FWZ7NYFjSDt1puIrr7MlB7owNuR5KhUIj04Can0zDCYjJY',
);

foreach ($encrypted as $string) {
    $result = $decrypter->decrypt($string);
    if ($result->isSuccessful()) {
        echo $result->data();
    } else {
        echo 'Decryption failed.';
    }
}