nawawi/base64-encryption

基于 MIME base64 的双向加密。

v1.0.2 2023-06-11 14:29 UTC

This package is auto-updated.

Last update: 2024-09-11 17:15:17 UTC


README

Base64Encryption 类提供了一个基于 MIME base64 的简单双向加密。

安装

composer require nawawi/base64-encryption

示例

<?php
if ( file_exists(__DIR__.'/vendor/autoload.php') ) {
    require_once __DIR__.'/vendor/autoload.php';
}

use Nawawi\Utils\Base64Encryption;

// With default key
$encryption = new Base64Encryption();
$encrypted = $encryption->encrypt('test123');
$decrypted = $encryption->decrypt($encrypted);

echo "\$encrypted = $encrypted\n";
echo "\$decrypted = $decrypted\n";

// Set key on initiation
$encryption = new Base64Encryption('encryption_key');
$encrypted = $encryption->encrypt('test123');
$decrypted = $encryption->decrypt($encrypted);

echo "\$encrypted = $encrypted\n";
echo "\$decrypted = $decrypted\n";

// Set key with set_key method
$encryption = new Base64Encryption();
$encryption->set_key('encryption_key');
$encrypted = $encryption->encrypt('test123');
$decrypted = $encryption->decrypt($encrypted);

echo "\$encrypted = $encrypted\n";
echo "\$decrypted = $decrypted\n";

// Set key on call
$encryption = new Base64Encryption();
$encrypted = $encryption->encrypt('test123', 'encryption_key');
$decrypted = $encryption->decrypt($encrypted, 'encryption_key');

echo "\$encrypted = $encrypted\n";
echo "\$decrypted = $decrypted\n";

加密密钥

您可以使用环境变量或常量来设置加密密钥。

# env
BASE64_ENCRYPTION_KEY = "encryption_key"
// Constant
define('BASE64_ENCRYPTION_KEY', 'encryption_key');