spomky-labs/php-aes-gcm

AES GCM (Galois Counter Mode) 的 PHP 实现。

v1.2.1 2018-11-07 14:39 UTC

This package is auto-updated.

Last update: 2024-09-17 07:53:54 UTC


README

Scrutinizer Code Quality Coverage Status

Build Status HHVM Status PHP 7 ready

SensioLabsInsight

Latest Stable Version Total Downloads Latest Unstable Version License GuardRails badge

发布流程

发布流程 在此描述

先决条件

此库需要至少 PHP 5.4+

它已经成功地在 PHP 5.4PHP 7.1HHVM 和夜间分支上进行测试。

如果您使用 PHP 7.1+,此库具有非常好的性能。 如果您不使用 PHP 7.1+,我们强烈建议您安装 PHP Crypto 扩展 此扩展显著提高了此库的性能。使用我们的纯 PHP 方法,您将获得较低的性能。

安装

安装此库的首选方式是依赖 Composer

composer require "spomky-labs/php-aes-gcm"

如何使用

<?php

use AESGCM\AESGCM;

// The Key Encryption Key
$K = hex2bin('feffe9928665731c6d6a8f9467308308feffe9928665731c');

// The data to encrypt (can be null for authentication)
$P = hex2bin('d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39');

// Additional Authenticated Data
$A = hex2bin('feedfacedeadbeeffeedfacedeadbeefabaddad2');

// Initialization Vector
$IV = hex2bin('cafebabefacedbaddecaf888');

// $C is the encrypted data ($C is null if $P is null)
// $T is the associated tag
list($C, $T) = AESGCM::encrypt($K, $IV, $P, $A);
// The value of $C should be hex2bin('3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda2710')
// The value of $T should be hex2bin('2519498e80f1478f37ba55bd6d27618c')

$P = AESGCM::decrypt($K, $IV, $C, $A, $T);
// The value of $P should be hex2bin('d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39')

附加标签

此密码的一些实现可能会在密文的末尾附加标签。例如,Java 实现通常使用此方法。

此库提供了一种简单的方法来生成此类密文并读取它。

<?php

use AESGCM\AESGCM;

// The values $K, $P, $A, $IV hereafter have the same meaning as above

// $C is the encrypted data with the appended tag
$C = AESGCM::encryptAndAppendTag($K, $IV, $P, $A);
// The value of $C should be hex2bin('3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda27102519498e80f1478f37ba55bd6d27618c')

$P = AESGCM::decryptWithAppendedTag($K, $IV, $C, $A);
// The value of $P should be hex2bin('d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b392519498e80f1478f37ba55bd6d27618c')

标签长度

默认标签长度为 128 位。此值强烈推荐,但您可能需要使用另一个标签长度。根据密码规格,标签长度可以是 128、120、112、104 或 96 位。

<?php

use AESGCM\AESGCM;

// The values $K, $P, $A, $IV hereafter have the same meaning as above
$TL = 96; // In this example the tag length will be 96 bits

list($C, $T) = AESGCM::encrypt($K, $IV, $P, $A, $TL);
// The value of $C should be hex2bin('3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda2710')
// The value of $T should be hex2bin('2519498e80f1478f37ba55bd')

在解密操作期间,使用 AESGCM::decrypt 方法自动计算标签长度。然而,如果标签附加在密文的末尾并且不是 128 位,则必须设置

<?php

// The values $K, $IV, $C, $A hereafter have the same meaning as above
$TL = 96; // In this example the tag length will be 96 bits

$P = AESGCM::decryptWithAppendedTag($K, $IV, $C, $A, $TL);

支持

我为您的问题提供解决方案并回答您的问题。

如果您真的喜欢该项目和我所做的工作,或者如果您想让我优先处理您的问题,那么您可以通过购买几杯 🍻 或更多来帮助我!

Become a Patreon

贡献

对新功能的请求、错误修复以及使此库有用的所有其他想法都欢迎。您可以提供的最佳贡献是修复 已打开的请求帮助问题

请确保 遵循这些最佳实践

基准测试

test 文件夹中,有一个用于运行加密和解密基准测试的小脚本。您可以在您的环境中运行它,以检查加密/解密操作需要多少时间。

php ./tests/Benchmark.php

许可

此库在 MIT 许可证 下发布。