PHP code example of philiprehberger / php-crypt

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

    

philiprehberger / php-crypt example snippets


use PhilipRehberger\Crypt\Crypt;

$key = Crypt::generateKey();

$encrypted = Crypt::encrypt('sensitive data', $key);
$decrypted = Crypt::decrypt($encrypted, $key);

$encrypted = Crypt::encrypt('data', $key, aad: 'user:42');
$decrypted = Crypt::decrypt($encrypted, $key, aad: 'user:42');

$rotated = Crypt::rotate($encrypted, $oldKey, $newKey);

$encrypted = Crypt::encryptArray(['name' => 'Alice', 'role' => 'admin'], $key);
$data = Crypt::decryptArray($encrypted, $key);

$data = [
    'user' => ['name' => 'Alice', 'email' => '[email protected]'],
    'roles' => ['admin', 'editor'],
];

$encrypted = Crypt::encryptJson($data, $key);
$decrypted = Crypt::decryptJson($encrypted, $key); // returns the original structure

$result = Crypt::validateKeyStrength($key);
// ['valid' => true, 'bits' => 256, 'recommendation' => null]

$weak = Crypt::validateKeyStrength(base64_encode('short'));
// ['valid' => false, 'bits' => 40, 'recommendation' => 'Key is 40-bit. A minimum of ...']

use PhilipRehberger\Crypt\KeyChain;

$chain = new KeyChain($newKey, $oldKey1, $oldKey2);

$encrypted = $chain->encrypt('data');
$decrypted = $chain->decrypt($encryptedWithAnyKey);

// Re-encrypt all ciphertexts with the current key
$rotated = $chain->rotateAll($ciphertexts);
bash
composer