randomphp/gcm
Composer 安装命令:
composer require randomphp/gcm
包简介
Package for encrypting strings with AES256-GCM
README 文档
README

RandomPHP GCM
Secure string encryption/decryption using AES-256-GCM (authenticated encryption) built on phpseclib v3.
Requirements
- PHP >= 8.1
phpseclib/phpseclib ~3.0
Installation
composer require randomphp/gcm
Usage
Encrypt
<?php
use RandomPHP\GCM\Encryptor;
use RandomPHP\GCM\EncryptionVersion;
$key = random_bytes(32); // 32 bytes (256-bit) recommended for AES-256
$cipherText = Encryptor::encrypt(
plainText: "Hello World!",
key: $key,
version: EncryptionVersion::V2 // optional (default: V2)
);
echo $cipherText;
Decrypt
decrypt() automatically detects the cipher format/version.
<?php
use RandomPHP\GCM\Encryptor;
$key = /* the same key used for encryption */;
$plainText = Encryptor::decrypt(
cipherText: $cipherText,
key: $key
);
echo $plainText;
Decode (inspect parts)
If you want to inspect the nonce/tag/ciphertext components without decrypting:
<?php
use RandomPHP\GCM\Encryptor;
$decoded = Encryptor::decode($cipherText);
var_dump($decoded->version); // EncryptionVersion::V1 or V2
var_dump($decoded->nonce); // raw bytes
var_dump($decoded->cipherText); // raw bytes
var_dump($decoded->tag); // raw bytes
Note: the values returned by
decode()are raw binary strings (bytes). Convert to hex/base64 if you want them readable.
Error Handling
The library throws dedicated exceptions under RandomPHP\GCM\Exceptions:
EncryptionExceptionDecryptionExceptionDecodingException
Example:
<?php
use RandomPHP\GCM\Encryptor;
use RandomPHP\GCM\Exceptions\EncryptionException;
use RandomPHP\GCM\Exceptions\DecryptionException;
use RandomPHP\GCM\Exceptions\DecodingException;
try {
$key = random_bytes(32);
$cipherText = Encryptor::encrypt("Secret data", $key);
$plainText = Encryptor::decrypt($cipherText, $key);
} catch (EncryptionException | DecryptionException | DecodingException $e) {
// Handle error (log, fail request, etc.)
echo $e->getMessage();
}
Security Notes
- Use a strong, secret key. 32 bytes is recommended for AES-256 (
random_bytes(32)). - Do not reuse nonces (this library generates a fresh nonce per encryption).
- Protect keys (env vars / secrets manager). Never commit keys to source control.
Tests
composer install
vendor/bin/phpunit
randomphp/gcm 适用场景与选型建议
randomphp/gcm 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 06 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 randomphp/gcm 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 randomphp/gcm 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: UNLICENSE
- 更新时间: 2026-06-03