PHP code example of kaadon / peertopeer

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

[
    'iv' => 'Base64EncodedIV',
    'ciphertext' => 'Base64EncodedCiphertext'
]

public function decrypt(string $remotePublicKeyB64, string $ivB64, string $ciphertextB64): string

public function __construct(string $keysFile = '/tmp/local_keys.json')

public function savePublicKey(string $name, string $publicKey): void

public function getPublicKey(string $name): ?string

public function listPublicKeys(): array

public function deletePublicKey(string $name): void

public function hasPublicKey(string $name): bool


Kaadon\PeerToPeer\E2EEncryption;
use Kaadon\PeerToPeer\LocalKeyStore;

try {
    // 模拟两个用户
    $alice = new E2EEncryption();
    $bob = new E2EEncryption();

    // 创建密钥存储
    $aliceKeyStore = new LocalKeyStore('/tmp/alice_keys.json');
    $bobKeyStore = new LocalKeyStore('/tmp/bob_keys.json');

    // 交换公钥(实际场景中通过安全渠道交换)
    $aliceKeyStore->savePublicKey('bob', $bob->getPublicKey());
    $bobKeyStore->savePublicKey('alice', $alice->getPublicKey());

    // Alice 发送消息给 Bob
    $message = "这是一条机密消息!";
    $bobPublicKey = $aliceKeyStore->getPublicKey('bob');
    $encrypted = $alice->encrypt($bobPublicKey, $message);

    echo "Alice 发送的加密消息:\n";
    echo "IV: " . $encrypted['iv'] . "\n";
    echo "密文: " . $encrypted['ciphertext'] . "\n\n";

    // Bob 接收并解密消息
    $alicePublicKey = $bobKeyStore->getPublicKey('alice');
    $decrypted = $bob->decrypt(
        $alicePublicKey,
        $encrypted['iv'],
        $encrypted['ciphertext']
    );

    echo "Bob 解密后的消息: $decrypted\n";

} catch (Exception $e) {
    echo "错误: " . $e->getMessage() . "\n";
}


namespace app\controller;

use Kaadon\PeerToPeer\E2EEncryption;
use think\Controller;

class ChatController extends Controller
{
    public function sendMessage()
    {
        $encryption = new E2EEncryption();

        // 从数据库获取对方公钥
        $friendPublicKey = $this->getUserPublicKey($friendId);

        // 加密消息
        $encrypted = $encryption->encrypt($friendPublicKey, $message);

        // 保存到数据库或发送到对方
        // ...
    }
}

try {
    $encryption = new E2EEncryption();
    $result = $encryption->encrypt($publicKey, $message);
} catch (\Kaadon\PeerToPeer\PeerToPeerException $e) {
    echo "加密错误: " . $e->getMessage();
} catch (\Exception $e) {
    echo "系统错误: " . $e->getMessage();
}