1. Go to this page and download the library: Download kaadon/peertopeer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kaadon / peertopeer example snippets
Kaadon\PeerToPeer\E2EEncryption;
// 创建两个用户实例
$alice = new E2EEncryption();
$bob = new E2EEncryption();
// 获取公钥(用于交换)
$alicePublicKey = $alice->getPublicKey();
$bobPublicKey = $bob->getPublicKey();
// Alice 发送加密消息给 Bob
$message = "Hello, Bob!";
$encrypted = $alice->encrypt($bobPublicKey, $message);
// Bob 解密消息
$decrypted = $bob->decrypt($alicePublicKey, $encrypted['iv'], $encrypted['ciphertext']);
echo "原始消息: $message\n";
echo "解密消息: $decrypted\n";
use Kaadon\PeerToPeer\E2EEncryption;
use Kaadon\PeerToPeer\LocalKeyStore;
// 创建加密实例
$user = new E2EEncryption();
// 创建密钥存储
$keyStore = new LocalKeyStore('/path/to/keys.json');
// 保存朋友的公钥
$keyStore->savePublicKey('alice', 'Base64EncodedPublicKey');
$keyStore->savePublicKey('bob', 'AnotherBase64PublicKey');
// 发送加密消息
$friendPublicKey = $keyStore->getPublicKey('alice');
if ($friendPublicKey) {
$encrypted = $user->encrypt($friendPublicKey, "机密消息");
// 发送 $encrypted 到对方
}
public function __construct(string $privateKey = null)
public function getPublicKey(): string
public function getPrivateKey(): string
public function encrypt(string $remotePublicKeyB64, string $message): array