phpgt / cipher
此包最新版本(v1.0.1)没有可用的许可信息。
消息的双向加密,以安全地传输纯文本。
v1.0.1
2023-05-12 11:14 UTC
Requires
- php: >=8.0
- ext-sodium: *
- phpgt/http: v1.*
Requires (Dev)
- phpstan/phpstan: ^v1.8
- phpunit/phpunit: ^v9.5
This package is auto-updated.
Last update: 2024-09-12 13:59:54 UTC
README
当两个系统通过公共网络交换消息时,必须使用加密工具来保护通信通道。加密和解密消息的过程复杂且容易出错,但在此存储库中通过提供 PlainTextMessage
和 EncryptedMessage
类的抽象来简化。
将您的秘密消息传递给 PlainTextMessage
构造函数,并附带一个私钥,然后您可以调用 encrypt()
将其转换为 EncryptedMessage
。一个 EncryptedMessage
通过 getCipherText()
和 getIv()
函数表示为密文和 IV 值。这两个字符串可以通过任何通信机制传递给接收者,安全地知道没有私钥无法读取内容。
在接收端,使用传入的密文和 IV 构造另一个 EncryptedMessage
,然后可以使用 decrypt()
读取原始消息
CipherText
类还公开了一个 getUri()
函数,用于创建预编码的 URI。可以传递带有 cipher
和 iv
查询字符串参数的 URI 给 EncryptedUri
类,以将其解密回 PlainTextMessage
。
示例用法:通过查询字符串传输加密消息
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!