martinusso/opencrypt

使用 PHP 和 OpenSSL 进行双向加密(加密和解密)数据

3.1.0 2017-10-26 16:08 UTC

This package is auto-updated.

Last update: 2024-09-22 23:08:04 UTC


README

Build Status Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version Latest Unstable Version License composer.lock

使用 PHP 和 OpenSSL 进行双向加密(加密和解密)数据

安装

composer require martinusso/opencrypt

提示

  • $secretKey 应该之前以安全方式生成,例如 openssl_random_pseudo_bytes。OpenCrypt 提供了一个静态方法 OpenCrypt::generateKey() 来实现这一点。

用法

$password = "OpenCrypt";

// Should have been previously generated in a cryptographically safe way
$secretKey = 'SECRET_KEY';

// You can pass the IV as argument or it is generated automatically
$openCrypt = new OpenCrypt($secretKey [, string $iv ]);

// get the IV
$iv = $openCrypt->iv();

// encrypt
$encryptedPassword = $openCrypt->encrypt($password);
// $encryptedPassword = 'GWw3bqL7FqjmRs0yyIR/8A=='

// decrypt
$decryptedPassword = $openCrypt->decrypt($encryptedPassword);
// $decryptedPassword = 'OpenCrypt'

生成 IV

OpenCrypt 提供了一个静态方法来生成安全的 IV

$iv = OpenCrypt::generateIV();

生成密钥

也可以生成安全的密钥

$secretKey = OpenCrypt::generateKey();

许可证

此软件是开源的,根据 MIT 许可证(MIT)许可。有关详细信息,请参阅 LICENSE