davodm/dgcrypt-php

该包最新版本(v0.0.2)没有可用的许可信息。

一个简单的PHP库,用于使用AES-256-CBC加密和解密,可在不同的语言/设备中使用。

v0.0.2 2024-08-04 21:41 UTC

This package is auto-updated.

Last update: 2024-09-04 21:55:39 UTC


README

Dgcrypt是一个简单的PHP库,用于使用多种加密方法进行加密和解密,包括 AES-256-CBCAES-256-GCMChaCha20-Poly1305。它旨在在后台安全地加密数据,然后可以轻松地在客户端(例如Android和iOS设备)上解密,从而确保数据在传输过程中保持安全,并且在传输过程中不容易被破解。

平台兼容性

Dgcrypt库设计在多个平台上无缝工作。您可以在以下平台上找到相应的库

这些库允许您轻松解密使用此PHP库在后台加密的数据,确保后端和客户端应用程序之间的安全通信。

支持的加密方法

Dgcrypt支持以下加密方法

  • AES-256-CBC:提供保密性的标准加密方法。
  • AES-256-GCM:提供加密和认证。
  • ChaCha20-Poly1305:以性能和安全著称的现代加密方法。

安装

您可以通过Composer安装该包

composer require davodm/dgcrypt-php

用法

设置

use Dgcrypt\Dgcrypt;

// Initialize the Dgcrypt class with the desired encryption method
$dgcrypt = new Dgcrypt('aes-256-gcm'); // Can be 'aes-256-cbc', 'aes-256-gcm', or 'chacha20-poly1305'

// Set a secret key
$dgcrypt->setKey('your-secret key');

在后台加密数据

// Encrypt a string
$plainText = 'Hello, World!';
$encrypted = $dgcrypt->encrypt($plainText);

// Output the encrypted string
echo $encrypted;

在后台解密数据

// Decrypt the previously encrypted string
$decrypted = $dgcrypt->setCipherMethod('aes-256-cbc')->decrypt($encrypted);

// Output the decrypted string
echo $decrypted;

自动生成密钥

// Generate a secure random key
$generatedKey = $dgcrypt->generateKey();

// Output the generated key
echo $generatedKey; // Display the hexadecimal key

设置初始化向量(IV)

// Optionally, set a custom IV (12 bytes for GCM or ChaCha20, 16 bytes for CBC)
// If no IV is set, a secure random IV will be generated automatically
$dgcrypt->setIV();

许可

本项目采用MIT许可 - 请参阅LICENSE文件获取详细信息。

作者

Davod Mozafari - Twitter