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

This package is auto-updated.

Last update: 2025-06-29 02:08:07 UTC


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