sop / gcm
用于AES的Galois/Counter Mode加密的PHP库。
3.0.0
2019-05-23 10:37 UTC
Requires
- php: >=7.2
- ext-gmp: *
- ext-hash: *
- ext-openssl: *
Requires (Dev)
- phpunit/phpunit: ^8.1
README
用于Galois/Counter Mode加密(GCM)的PHP库。
支持128位、192位和256位密钥大小以及指定的认证标签长度。
要求
- PHP >=7.2
- openssl
- hash
- gmp
安装
此库可在Packagist上找到。
composer require sop/gcm
代码示例
以下是一些简单的使用示例。为了简洁,省略了命名空间。
加密
使用128位密钥对包含附加认证数据的消息进行加密。
[$ciphertext, $auth_tag] = AESGCM::encrypt( 'Meet me at the pier at midnight.', 'Additional info', 'some 128 bit key', 'random iv-string'); echo bin2hex($ciphertext) . "\n" . bin2hex($auth_tag);
输出
5a24cfccf2e6c7763f71cd2ef6bcaa78385b16328593a93a43146d587e314ed8
389cc23f815d453686915530937d2053
详见/examples
的详细版本。
解密
解密上述创建的密文。附加认证数据必须相同,否则认证失败并将抛出异常。
$plaintext = AESGCM::decrypt($ciphertext, $auth_tag, 'Additional info', 'some 128 bit key', 'random iv-string'); echo $plaintext;
输出
Meet me at the pier at midnight.
详见/examples
的详细版本。
使用显式的加密方法和标签长度
加密不包含附加认证数据的消息,使用AES-192作为底层加密算法并生成104位(13字节)的认证标签。
$key = '012345678901234567890123'; // 192-bit encryption key $iv = hex2bin('beadfacebadc0fee'); // random initialization vector $gcm = new GCM(new AES192Cipher(), 13); [$ciphertext, $auth_tag] = $gcm->encrypt('Secret message.', '', $key, $iv); echo bin2hex($ciphertext) . "\n" . bin2hex($auth_tag);
输出
7bcd4e423016213c60a3c0a3e3fc0c
027b14cfea0a2307649fc67b1d
解密上述输出。
$plaintext = $gcm->decrypt($ciphertext, $auth_tag, '', $key, $iv); echo $plaintext;
输出
Secret message.
参考资料
许可证
本项目采用MIT许可证。