eplayment/laravel-crypto-pkg

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

2.0.1 2021-05-24 04:17 UTC

This package is not auto-updated.

Last update: 2024-09-24 18:08:30 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

变更日志

有关最近更改的更多信息,请参阅变更日志

贡献

有关详细信息,请参阅贡献指南

安全漏洞

有关如何报告安全漏洞,请审查我们的安全策略

鸣谢

许可

MIT许可证(MIT)。有关更多信息,请参阅许可文件