spatie/crypto

使用私钥/公钥加密和签名数据

维护者

详细信息

github.com/spatie/crypto

主页

源代码

资助包维护!
spatie
其他

2.1.0 2024-06-20 08:24 UTC

This package is auto-updated.

Last update: 2024-09-08 05:42:09 UTC


README

Latest Version on Packagist Tests Total Downloads

此包允许您轻松生成私钥/公钥对,并使用这些密钥加密/解密消息。

use Spatie\Crypto\Rsa\KeyPair;
use Spatie\Crypto\Rsa\PrivateKey;
use Spatie\Crypto\Rsa\PublicKey;

// generating an RSA key pair
[$privateKey, $publicKey] = (new KeyPair())->generate();

// when passing paths, the generated keys will be written those paths
(new KeyPair())->generate($pathToPrivateKey, $pathToPublicKey);

$data = 'my secret data';

$privateKey = PrivateKey::fromFile($pathToPrivateKey);
$encryptedData = $privateKey->encrypt($data); // returns something unreadable

$publicKey = PublicKey::fromFile($pathToPublicKey);
$decryptedData = $publicKey->decrypt($encryptedData); // returns 'my secret data'

此包中的大多数函数都是围绕 openssl_* 函数的包装,以提高开发体验。

支持我们

我们投入了大量资源来创建 一流的开放源代码包。您可以通过 购买我们的付费产品之一 来支持我们。

我们非常感谢您从家乡寄给我们明信片,说明您正在使用我们的哪些包。您可以在 我们的联系页面 上找到我们的地址。我们在 我们的虚拟明信片墙上 发布所有收到的明信片。

安装

您可以通过 composer 安装此包

composer require spatie/crypto

用法

您可以使用 KeyPair 类上的 generate 函数生成密钥对。

use Spatie\Crypto\Rsa\KeyPair;

[$privateKey, $publicKey] = (new KeyPair())->generate();

您可以通过传递路径到 generate 函数将密钥写入磁盘。

// when passing paths, the generate keys will to those paths
(new KeyPair())->generate($pathToPrivateKey, $pathToPublicKey)

您可以通过使用 password 方法用密码保护私钥

[$passwordProtectedPrivateKey, $publicKey] = (new KeyPair())->password('my-password')->generate();

当使用密码生成私钥时,您在实例化 PrivateKey 类时需要该密码。

加载密钥

要从一个文件加载密钥,请使用 fromFile 静态方法。

Spatie\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey);
Spatie\Crypto\Rsa\PublicKey::fromFile($pathToPublicKey);

或者,您也可以使用字符串创建一个密钥对象。

Spatie\Crypto\Rsa\PrivateKey::fromString($privateKeyString);
Spatie\Crypto\Rsa\PublicKey::fromString($publicKeyString);

如果私钥受密码保护,您需要将密码作为第二个参数传递。

Spatie\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey, $password);
Spatie\Crypto\Rsa\PrivateKey::fromString($privateKeyString, $password);

如果您未指定正确的密码,将抛出 Spatie\Crypto\Exceptions\InvalidPrivateKey 异常。

使用私钥加密消息,使用公钥解密

以下是使用私钥加密数据以及如何使用公钥解密它的方法。

$data = 'my secret data';

$privateKey = Spatie\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey);
$encryptedData = $privateKey->encrypt($data); // encrypted data contains something unreadable

$publicKey = Spatie\Crypto\Rsa\PublicKey::fromFile($pathToPublicKey);
$decryptedData = $publicKey->decrypt($encryptedData); // decrypted data contains 'my secret data'

如果 decrypt 无法解密给定的数据(可能使用了与加密数据不匹配的私钥,或者数据被篡改),将抛出类为 Spatie\Crypto\Exceptions\CouldNotDecryptData 的异常。

使用公钥加密消息,使用私钥解密

以下是使用公钥加密数据以及如何使用私钥解密它的方法。

$data = 'my secret data';

$publicKey = Spatie\Crypto\Rsa\PublicKey::fromFile($pathToPublicKey);
$encryptedData = $publicKey->encrypt($data); // encrypted data contains something unreadable

$privateKey = Spatie\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey);
$decryptedData = $privateKey->decrypt($encryptedData); // decrypted data contains 'my secret data'

如果 decrypt 无法解密给定的数据(可能使用了与加密数据不匹配的公钥,或者数据被篡改),将抛出类为 Spatie\Crypto\Exceptions\CouldNotDecryptData 的异常。

确定数据是否可以解密

两个类 PublicKeyPrivateKey 都有一个 canDecrypt 方法,可以确定给定数据是否可以解密。

Spatie\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey)->canDecrypt($data); // returns a boolean;
Spatie\Crypto\Rsa\PublicKey::fromFile($pathToPublicKey)->canDecrypt($data); // returns a boolean;

签名和验证数据

PrivateKey 类有一个 sign 方法可以生成给定数据的签名。可以在 PublicKey 类的 verify 方法上使用来验证签名是否对给定数据有效。

如果 verify 返回 true,你可以确定私钥持有者已经签名了该消息,并且该消息没有被篡改。

$signature = Spatie\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey)->sign('my message'); // returns a string

$publicKey = Spatie\Crypto\Rsa\PublicKey::fromFile($pathToPublicKey);

$publicKey->verify('my message', $signature) // returns true;
$publicKey->verify('my modified message', $signature) // returns false;

备选方案

本软件包旨在非常轻量级且易于使用。如果您需要更多功能,请考虑使用以下备选方案:

关于RSA使用的说明

在撰写本文时,RSA对于本软件包所针对的使用场景来说是足够安全的。

要了解更多关于为什么RSA可能不足以满足您的需求,请阅读 Paragonie.com上的这篇关于公开密钥加密的文章

测试

composer test

变更日志

请参阅 CHANGELOG 了解最近更改的详细信息。

贡献

请参阅 CONTRIBUTING 了解详细信息。

安全漏洞

请参阅 我们的安全政策 了解如何报告安全漏洞。

鸣谢

许可

MIT许可(MIT)。请参阅 许可文件 了解更多信息。