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
。通过 getCipherText()
和 getIv()
函数,EncryptedMessage
由一个密文和一个初始向量(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!