bayfrontmedia/encryptor

一个使用 OpenSSL 的快速、简单的双向加密库。

v2.0.0 2023-01-26 18:19 UTC

This package is auto-updated.

Last update: 2024-08-26 21:40:40 UTC


README

一个使用 OpenSSL 的快速、简单的双向加密库。

许可

此项目是开源的,可在MIT 许可证下使用。

作者

Bayfront Media

需求

  • PHP ^8.0
  • OpenSSL PHP 扩展
  • JSON PHP 扩展

安装

composer require bayfrontmedia/encryptor

用法

开始使用 Encryptor

必须将一个私有的可重复的密钥传递给构造函数。加密和解密时必须使用相同的密钥。如果加密值所用的密钥丢失,将无法解密。

可选的第二个构造函数参数允许您指定要使用的加密方法。默认情况下,Encryptor 使用 AES-256-CBC

如果使用了无效的加密方法,将抛出 Bayfront\Encryptor\InvalidCipherException 异常。

use Bayfront\Encryptor\Encryptor;

$encryptor = new Encryptor('private_key');

公共方法

getKey

描述

返回加密密钥。

参数

返回

  • (字符串)

getCipher

描述

返回用于加密的加密方法。

参数

返回

  • (字符串)

encrypt

描述

加密给定的值。

参数

  • $value (混合)
  • $serialize = true (布尔值)

返回

  • (字符串)

抛出

  • Bayfront\Encryptor\EncryptException

示例

try {

    $encrypted = $encryptor->encrypt([
        'name' => 'John',
        'user_id' => 8
    ]);

} catch (EncryptException $e) {
    die($e->getMessage());
}

encryptString

描述

不进行序列化的字符串加密。

参数

  • $value (字符串)

返回

  • (字符串)

抛出

  • Bayfront\Encryptor\EncryptException

示例

try {

    $encrypted_string = $encryptor->encryptString('A string to encrypt');

} catch (EncryptException $e) {
    die($e->getMessage());
}

decrypt

描述

解密给定的值。

参数

  • $data (字符串)
  • $unserialize = true (布尔值)

返回

  • (混合)

抛出

  • Bayfront\Encryptor\DecryptException

示例

try {

    $decrypted = $encryptor->decrypt($encrypted);

} catch (DecryptException $e) {
    die($e->getMessage());
}

decryptString

描述

不进行反序列化的字符串解密。

参数

  • $data (字符串)

返回

  • (字符串)

抛出

  • Bayfront\Encryptor\DecryptException

示例

try {

    $decrypted_string = $encryptor->decryptString($encrypted_string);

} catch (DecryptException $e) {
    die($e->getMessage());
}