Encrypt or decrypt strings and associative arrays in PHP using OpenSSL and AES-128-CBC.
✅ Compatible with PHP 8.2+
Install via Composer:
composer require robertogriel/crypt-array
use Crypto\Crypto;
$crypto = new Crypto('YOUR_SECRET_KEY', 'YOUR_SECRET_IV', true); // true = use Base64
$data = [
'username' => 'john',
'email' => 'john@example.com'
];
$encrypted = $crypto->encrypt($data);
$decrypted = $crypto->decrypt($encrypted);
$encrypted = $crypto->encrypt('Hello world');
$decrypted = $crypto->decrypt($encrypted);
The third parameter of the constructor is a boolean to enable Base64 encoding:
$crypto = new Crypto('key', 'iv', true); // true = enable Base64
See the full example file: sample.php
It includes:
- Input data
- Encrypted values
- Decrypted results
- Code snippets used
Previous versions used getEncrypted()
and getDecrypted()
and required a second argument to indicate a string.
$crypto->getEncrypted('string', 1);
$crypto->getDecrypted('string', 1);
$crypto->encrypt('string');
$crypto->decrypt('string');
The methods are now smart enough to detect whether you're using a string or an array.
- PHP >= 8.2
- OpenSSL extension enabled
Run the full test suite with Pest:
./vendor/bin/pest
MIT © robertogriel