Skip to content

Encript or Decript arrays using OPENSSL_ENC/DEC with PHP. Read the documentation or see the sample file.

License

Notifications You must be signed in to change notification settings

robertogriel/crypt-array

Repository files navigation

🔐 Crypt Array

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

About

Encript or Decript arrays using OPENSSL_ENC/DEC with PHP. Read the documentation or see the sample file.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages