robertogriel / crypt-array
A simple PHP library to encrypt and decrypt strings or associative arrays using OpenSSL.
2.0.0
2025-03-29 01:00 UTC
Requires
- php: >=8.0
- ext-openssl: *
Requires (Dev)
- pestphp/pest: ^3.7.4
README
Encrypt or decrypt strings and associative arrays in PHP using OpenSSL and AES-128-CBC.
โ Compatible with PHP 8.2+
๐ฆ Installation
Install via Composer:
composer require robertogriel/crypt-array
๐ Usage
1. Create a Crypto instance
use Crypto\Crypto; $crypto = new Crypto('YOUR_SECRET_KEY', 'YOUR_SECRET_IV', true); // true = use Base64
2. Encrypting an array
$data = [ 'username' => 'john', 'email' => 'john@example.com' ]; $encrypted = $crypto->encrypt($data);
3. Decrypting an array
$decrypted = $crypto->decrypt($encrypted);
4. Encrypting a string
$encrypted = $crypto->encrypt('Hello world');
5. Decrypting a string
$decrypted = $crypto->decrypt($encrypted);
โ๏ธ Options
The third parameter of the constructor is a boolean to enable Base64 encoding:
$crypto = new Crypto('key', 'iv', true); // true = enable Base64
๐ Full Example
See the full example file: sample.php
It includes:
- Input data
- Encrypted values
- Decrypted results
- Code snippets used
๐งฏ Migration Guide (v1.x โ v2.x)
Previous versions used getEncrypted()
and getDecrypted()
and required a second argument to indicate a string.
๐ Old usage:
$crypto->getEncrypted('string', 1); $crypto->getDecrypted('string', 1);
โ Now:
$crypto->encrypt('string'); $crypto->decrypt('string');
The methods are now smart enough to detect whether you're using a string or an array.
โ Requirements
- PHP >= 8.2
- OpenSSL extension enabled
๐งช Testing
Run the full test suite with Pest:
./vendor/bin/pest
๐ License
MIT ยฉ robertogriel