noprotocol/php-mysql-aes-crypt

在PHP中加密/解密数据,使其与MySQL的AES_ENCRYPT & AES_DECRYPT函数兼容。

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

This package is auto-updated.

Last update: 2024-09-05 03:40:03 UTC


README

在PHP中加密/解密与MySQL的aes_encrypt() & aes_decrypt()函数兼容的值。1

Build Status Scrutinizer Code Quality Code Coverage

变更日志

安装

使用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;

不使用Composer

请使用Composer。如果您需要手动安装,请从仓库下载Crypter.php并将其保存到您的项目路径中。

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

use NoProtocol\Encryption\MySQL\AES\Crypter;

使用方法

使用用作加密过程密钥的字符串创建类的实例。运行encrypt()decrypt()来加密/解密您的数据。

<?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);

也可以根据需要使用不同的加密方法。

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

注意:此方法仅测试了AES-128-ECB(默认)、AES-192-ECB和AES-256-ECB

性能测试

/benchmarks/benchmarks.php中提供了一个性能测试。您可以传递一个数字作为参数来设置要运行的项数,例如。

php benchmarks/benchmarks.php 20000

运行20000项。如果没有指定数字,默认为10000项。

您还可以可选地设置所需的加密方法,例如

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

测试

/tests中提供了PHPunit测试用例。

1http://www.smashingmagazine.com/2012/05/replicating-mysql-aes-encryption-methods-with-php/所述