devuri / encryption
一个用于处理加密和解密操作的Composer包。
Requires
- php: ^7.4 || ^8.0
- defuse/php-encryption: ^v2.0
- symfony/filesystem: 5.4.38
Requires (Dev)
- 10up/phpcs-composer: dev-master
- phpstan/phpstan: ^1.8
- phpstan/phpstan-strict-rules: ^1.3
- phpunit/phpunit: ^9.5
- vimeo/psalm: ^4.24 || ^5.0
This package is auto-updated.
Last update: 2024-08-31 16:15:34 UTC
README
Encryption 包是一个简单的Composer包,它提供了使用Defuse PHP加密库加密和解密数据的函数。此包简化了处理加密密钥的过程,并允许您轻松地对文件和数据执行加密和解密操作。
安装
要在您的PHP应用程序中使用此包,您需要安装Composer。一旦您设置了Composer,请运行以下命令将包添加到您的项目中
composer require devuri/encryption
用法
加密密钥生成
要使用库加密和解密数据,您需要生成一个加密密钥。EncryptionKey类提供了一个简单的方法来生成一个新的ASCII格式随机加密密钥
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.
确保您安全并保护生成的加密密钥,因为它是加密和解密敏感数据所必需的。
设置加密
要使用Encryption包,您首先需要通过提供必要的参数设置Encryption类
use Urisoft\Encryption; use Symfony\Component\Filesystem\Filesystem; // Replace these with the appropriate values for your application $rootDirPath = __DIR__; $filesystem = new Filesystem(); $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 }
加密和解密文件
您可以使用encrypt_file()和decrypt_file()方法加密和解密文件
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 }
加密数据
您可以使用encrypt()方法加密数据,如敏感信息
$dataToEncrypt = 'This is sensitive data'; $encryptedData = $encryption->encrypt($dataToEncrypt); // Optionally, encode the encrypted data in base64 $base64EncodedData = $encryption->encrypt($dataToEncrypt, true);
解密数据
要解密加密数据,请使用decrypt()方法
$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 }
加密.env文件
该包提供了一个方法来加密.env文件的内容
try { // Encrypt the .env file $encryption->encrypt_envfile('/.env'); } catch (Exception $e) { // Handle encryption errors }
加密内容将保存在名为.env.encrypted的文件中。
密钥管理
Encryption类期望密钥文件以ASCII格式存储。它从初始化期间指定的密钥文件中检索加密密钥。请确保安全并保护密钥文件。
如果您已定义了常量WEBAPP_ENCRYPTION_KEY,则类将使用该作为加密密钥。否则,它将在提供的目录中查找默认文件名标识符'secret'的密钥文件。密钥文件默认应命名为'.secret.txt',除非您在初始化期间指定了不同的密钥文件名标识符。
许可证
此包是开源软件,许可协议为MIT许可证。
致谢
Encryption包利用Defuse PHP加密库进行加密和解密操作。
有关如何使用Defuse PHP加密库的更多信息,请参阅其文档。
注意:在实际使用示例中,用实际数据或与应用程序相关的文件路径替换省略号(...)。在执行加密和解密操作时,始终适当处理异常。