noprotocol/php-mysql-aes-crypt

Encrypt/decrypt data in PHP to a format compatible with MySQL AES_ENCRYPT & AES_DECRYPT functions.

v2.0.2 2019-12-02 14:43 UTC

This package is auto-updated.

Last update: 2024-11-05 04:04:25 UTC


README

Encrypt/decrypt values in PHP which are compatible with MySQL's aes_encrypt() & aes_decrypt() functions. 1

Build Status Scrutinizer Code Quality Code Coverage

Change log

Installation

With Composer

$ composer require noprotocol/php-mysql-aes-crypt
{
    "require": {
        "noprotocol/php-mysql-aes-crypt": "^2.0.0"
    }
}
<?php
require 'vendor/autoload.php';

use NoProtocol\Encryption\MySQL\AES\Crypter;

Without Composer

Please use Composer. If you need to install manually, download Crypter.php from the repository and save the file into your project path.

<?php
require 'path/to/Cryper.php';

use NoProtocol\Encryption\MySQL\AES\Crypter;

Usage

Create a new instance of the class with a string which will be used as the key for the crypting process. Run encrypt() or decrypt() to encrypt/decrypt your data.

<?php
use NoProtocol\Encryption\MySQL\AES\Crypter;

// create a new instance
$crypter = new Crypter('mykeystring');

// encrypt a piece of data
$encrypted = $crypter->encrypt('foobar');

// decrypt a piece of data
$decrypted = $crypter->decrypt($encrypted);

Using a different encryption method is possible too when so desired.

$crypter = new Crypter('mykeystring', 'AES-256-ECB');

NB: This is only tested for AES-128-ECB (default), AES-192-ECB and AES-256-ECB

Benchmark

A benchmark is provided in /benchmarks/benchmarks.php. You can set the number of items to run by passing a number as an argument, e.g.:

php benchmarks/benchmarks.php 20000

to run 20000 items. If no number is given, it defaults to 10000 items.

You can also optionally set the desired encryption method for example:

php benchmarks/benchmarks.php 20000 AES-256-ECB

Testing

PHPunit test cases are provided in /tests.

1As outlined in http://www.smashingmagazine.com/2012/05/replicating-mysql-aes-encryption-methods-with-php/