phpgt/cipher

此包最新版本(v1.0.1)没有可用的许可信息。

消息的双向加密,以安全地传输纯文本。

维护者

详细信息

github.com/PhpGt/Cipher

源代码

问题

资助包维护!
PhpGt

v1.0.1 2023-05-12 11:14 UTC

README

当两个系统通过公共网络交换消息时,必须使用加密工具来保护通信通道。加密和解密消息的过程复杂且容易出错,但在此存储库中通过提供 PlainTextMessageEncryptedMessage 类的抽象来简化。

将您的秘密消息传递给 PlainTextMessage 构造函数,并附带一个私钥,然后您可以调用 encrypt() 将其转换为 EncryptedMessage。一个 EncryptedMessage 通过 getCipherText()getIv() 函数表示为密文和 IV 值。这两个字符串可以通过任何通信机制传递给接收者,安全地知道没有私钥无法读取内容。

在接收端,使用传入的密文和 IV 构造另一个 EncryptedMessage,然后可以使用 decrypt() 读取原始消息

CipherText 类还公开了一个 getUri() 函数,用于创建预编码的 URI。可以传递带有 cipheriv 查询字符串参数的 URI 给 EncryptedUri 类,以将其解密回 PlainTextMessage

Build status Code quality Code coverage Current version PHP.Gt/Cipher documentation

示例用法:通过查询字符串传输加密消息

sender.php:

use \Gt\Cipher\Message\PlainTextMessage;
use \Gt\Cipher\Message\EncryptedMessage;

$privateKey = "This can be any string, but a long random string is best.";

$message = new PlainTextMessage("Hello, PHP.Gt!");
$cipherText = $message->encrypt($privateKey);
header("Location: " . $cipherText->getUri("/receiver.php"));

receiver.php:

// This key must be the same on the sender and receiver!
use Gt\Cipher\EncryptedUri;

$privateKey = "This can be any string, but a long random string is best.";

$uri = new EncryptedUri($_SERVER["REQUEST_URI"]);
$plainText = $uri->decryptMessage($privateKey);
echo $plainText;
// Output: Hello, PHP.Gt!