承接 devuri/encryption 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

devuri/encryption

Composer 安装命令:

composer require devuri/encryption

包简介

A Composer package for handling encryption and decryption operations.

README 文档

README

The Encryption package is a simple Composer package that provides functionality for encrypting and decrypting data using the Defuse PHP encryption library or encrypting and decrypting data using OpenSSL. This package simplifies the process of handling encryption keys and allows you to perform encryption and decryption operations on files and data with ease.

Requirements

  • PHP 7.4 or higher.
  • OpenSSL PHP Extension enabled.

Installation

To use this package in your PHP application, you need to have Composer installed. Once you have Composer set up, run the following command to add the package to your project:

composer require devuri/encryption

Usage

Encryption Key Generation

To encrypt and decrypt data using the library, you need to generate an encryption key. The EncryptionKey class provides a simple way to generate a new random encryption key in ASCII format:

use Urisoft\EncryptionKey;

// Generate a new encryption key
$key = EncryptionKey::generate_key();

// Store the generated key securely (e.g., in a secret key file) for future use.

Ensure you keep the generated encryption key secure and protected, as it is essential for encrypting and decrypting sensitive data.

Setting up Encryption

To use the Encryption package, you first need to set up the Encryption class by providing the necessary parameters:

use Urisoft\Encryption;
use Urisoft\Filesystem;

// Replace these with the appropriate values for your application
$rootDirPath = __DIR__;
$filesystem = new Filesystem(); // Optional, required only for encrypt_envfile()
$secretKeyPath = '/path/to/secret_key_directory';
$keyId = 'secret'; // Optional, default is 'secret'

try {
    $encryption = new Encryption($rootDirPath, $filesystem, $secretKeyPath, $keyId);
} catch (InvalidArgumentException $e) {
    // Handle exception if the secret key file is not found
}

Note: The $filesystem parameter is optional. Pass null if you don't need the encrypt_envfile() method. You can also provide your own implementation of FilesystemInterface.

Encrypting and Decrypting Files

You can use the encrypt_file() and decrypt_file() methods to encrypt and decrypt files:

try {
    // Encrypt a file
    $inputFile = '/path/to/input_file.txt';
    $outputFile = '/path/to/encrypted_file.txt';
    $encryption->encrypt_file($inputFile, $outputFile);

    // Decrypt the encrypted file
    $encryptedFile = '/path/to/encrypted_file.txt';
    $decryptedFile = '/path/to/decrypted_file.txt';
    $encryption->decrypt_file($encryptedFile, $decryptedFile);
} catch (Exception $e) {
    // Handle encryption/decryption errors
}

Encrypting Data

You can use the encrypt() method to encrypt data, such as sensitive information:

$dataToEncrypt = 'This is sensitive data';
$encryptedData = $encryption->encrypt($dataToEncrypt);

// Optionally, encode the encrypted data in base64
$base64EncodedData = $encryption->encrypt($dataToEncrypt, true);

Decrypting Data

To decrypt encrypted data, use the decrypt() method:

$encryptedData = '...'; // Replace this with the actual encrypted data
$decryptedData = $encryption->decrypt($encryptedData);

// Optionally, if the encrypted data was base64-encoded
$base64EncodedData = '...'; // Replace this with the actual base64-encoded data
$decodedDecryptedData = $encryption->decrypt($base64EncodedData, true);

if ($decryptedData === null || $decodedDecryptedData === null) {
    // Decryption failed or wrong key was loaded
    // Handle the error accordingly
}

Encrypting .env File

The package provides a method to encrypt the contents of a .env file:

try {
    // Encrypt the .env file
    $encryption->encrypt_envfile('/.env');
} catch (Exception $e) {
    // Handle encryption errors
}

The encrypted contents will be saved in a file named .env.encrypted.

Key Management

The Encryption class expects the secret key file to be stored in ASCII format. It retrieves the encryption key from the secret key file specified during initialization. Make sure to keep the secret key file secure and protected.

If you have a constant WEBAPP_ENCRYPTION_KEY defined, the class will use that as the encryption key. Otherwise, it will look for the secret key file in the provided directory with the default filename identifier 'secret'. The key file should be named as '.secret.txt' by default unless you specify a different key filename identifier during initialization.

OpenSSLEncrypt

The OpenSSLEncrypt class, provides a simple interface for encrypting and decrypting data using OpenSSL. It supports various cipher algorithms and output formats, such as base64 and hex.

Usage

use Urisoft\OpenSSLEncrypt;

$key = 'your-encryption-key'; // Replace with your secure key.
$cipher = 'AES-128-CBC'; // Optional, default is AES-128-CBC.

$encryptor = new OpenSSLEncrypt($key, $cipher);

If you don't specify a cipher, AES-128-CBC is used by default.

Encrypting Data

$plainText = "Hello, World!";
$outputFormat = 'base64'; // Optional, formats: base64, hex, binary. Default is base64.

$encryptedText = $encryptor->encrypt($plainText, $outputFormat);

Decrypting Data

$inputFormat = 'base64'; // Should match the format used in encryption. Default is base64.

$decryptedText = $encryptor->decrypt($encryptedText, $inputFormat);

Methods

  • __construct(string $key, string $cipher = 'AES-128-CBC'): Constructor to initialize the encryption key and cipher algorithm. Throws InvalidArgumentException if an unsupported cipher is provided.
  • encrypt(string $data, string $outputFormat = 'base64'): Encrypts the provided data and returns it in the specified format. Throws RuntimeException if encryption fails.
  • decrypt(string $data, string $inputFormat = 'base64'): Decrypts the provided data assuming it's in the specified format. Throws RuntimeException if decryption fails or input format is invalid.

Notes

  • It is crucial to use a secure and unique key for encryption.
  • Cipher names are case-insensitive (e.g., AES-128-CBC and aes-128-cbc are both valid).
  • The choice of cipher algorithm can be modified based on your requirements. Use openssl_get_cipher_methods() to list available ciphers.
  • The class automatically handles format conversions for input and output.

License

This package is open-source software licensed under the MIT License.

Acknowledgments

The Encryption package utilizes the Defuse PHP encryption library for encryption and decryption operations.

For more information on how to use the Defuse PHP encryption library, please refer to its documentation.

Note: Replace the ellipsis (...) in the usage examples with actual data or file paths relevant to your application. Always handle exceptions appropriately when performing encryption and decryption operations.

devuri/encryption 适用场景与选型建议

devuri/encryption 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24.47k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 07 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「security」 「encryption」 「secure」 「cryptographic」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 devuri/encryption 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 devuri/encryption 我们能提供哪些服务?
定制开发 / 二次开发

基于 devuri/encryption 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 24.47k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 14
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-07-29