rust “serde-encrypt”的“发送方”和“接收方”如何交换它们的“私钥”和“公钥”,例如“SenderPublicKey”?

3z6pesqy  于 2023-03-12  发布在  其他
关注(0)|答案(1)|浏览(136)

serde-encryptGitHub上的测试中,密钥交换是如何进行的?我假设它是通过发送加密的String来完成的,就像加密货币的公共地址一样。然而,在这个例子中,不清楚如何分别串行化/反串行化SenderReceiverPublicKeyPrivateKey

#[test]
fn test_serde_encrypt_public_key() -> Result<(), Error> {
    // public/private key generation to start key exchange
    let alice_key_pair = ReceiverKeyPair::generate();
    let bob_key_pair = SenderKeyPair::generate();

    let alice_combined_key =
        ReceiverCombinedKey::new(bob_key_pair.public_key(), alice_key_pair.private_key());
    let bob_combined_key =
        SenderCombinedKey::new(bob_key_pair.private_key(), alice_key_pair.public_key());

    // key exchange HOW IS IT SENT?
    let bob_shared_key = bob_generates_shared_key();
    let encrypted_shared_key = bob_sends_shared_key(&bob_shared_key, &bob_combined_key)?;
    let alice_shared_key = alice_receives_shared_key(encrypted_shared_key, &alice_combined_key)?;
    assert_eq!(alice_shared_key, bob_shared_key);

    // message exchange using shared key
    let secret_message = alice_sends_secret_message(&alice_shared_key)?;
    bob_reads_secret_message(secret_message, &bob_shared_key)
}

我也没有找到从String构建eidogg. SenderPublicKey的方法。

vdzxcuhz

vdzxcuhz1#

您可以使用任何能够发送字节序列的介质进行通信,下面是扩展为使用mpsc<Vec<u8>>对的示例。

use std::sync::mpsc::{self, Receiver, Sender};

use crypto_box::PublicKey;
use serde::{Deserialize, Serialize};

use serde_encrypt::{
    key::key_pair::{ReceiverKeyPair, SenderKeyPair},
    serialize::{impls::BincodeSerializer, TypedSerialized},
    shared_key::SharedKey,
    traits::{SerdeEncryptPublicKey, SerdeEncryptSharedKey},
    AsSharedKey, EncryptedMessage, ReceiverCombinedKey, ReceiverKeyPairCore, SenderCombinedKey,
    SenderKeyPairCore,
};
use serde_encrypt_core::key::key_pair::public_key::{ReceiverPublicKey, SenderPublicKey};

fn main() {
    let (bob_tx, alice_rx) = mpsc::channel();
    let (alice_tx, bob_rx) = mpsc::channel();
    let h = std::thread::spawn(move || bob(bob_rx, bob_tx));
    alice(alice_rx, alice_tx);
    h.join().unwrap();
}

fn bob(rx: Receiver<Vec<u8>>, tx: Sender<Vec<u8>>) {
    // generate key pair for bob
    let key_pair = SenderKeyPair::generate();

    // receive public key from alice
    let alice_key_bytes: [u8; 32] = rx.recv().unwrap().try_into().unwrap();
    let alice_public_key = ReceiverPublicKey::from(PublicKey::from(alice_key_bytes));

    // send own public key to alice
    tx.send(key_pair.public_key().as_ref().as_bytes().to_vec())
        .unwrap();

    // generate combined key from the public key of alice and our private key
    let combined_key = SenderCombinedKey::new(key_pair.private_key(), &alice_public_key);

    // generate a shared key, encrypt it and send it to alice
    let shared_key = SharedKey::generate();
    let encrypted_shared_key = shared_key.encrypt(&combined_key).unwrap();
    tx.send(encrypted_shared_key.serialize()).unwrap();

    // receive message from alice
    let encrypted_serialized = rx.recv().unwrap();
    let encrypted_message = EncryptedMessage::deserialize(encrypted_serialized).unwrap();

    // decrypt the message
    let decrypted = Message::decrypt_ref(&encrypted_message, &shared_key).unwrap();
    let revealed_message = decrypted.deserialize().unwrap();

    // Congrats 🎉👏
    assert_eq!(revealed_message.content.title, "my heart");
    assert_eq!(revealed_message.content.sentence, "I ❤️ you.");
    assert_eq!(revealed_message.sender, "Alice");
    dbg!(revealed_message);
}

fn alice(rx: Receiver<Vec<u8>>, tx: Sender<Vec<u8>>) {
    // generate key pair for alice
    let key_pair = ReceiverKeyPair::generate();

    // send our public key to bob
    tx.send(key_pair.public_key().as_ref().as_bytes().to_vec())
        .unwrap();

    // receive the public key from bob
    let bob_key_bytes: [u8; 32] = rx.recv().unwrap().try_into().unwrap();
    let bob_public_key = SenderPublicKey::from(PublicKey::from(bob_key_bytes));

    // generate combined key from the public key of bob and our private key
    let combined_key = ReceiverCombinedKey::new(&bob_public_key, key_pair.private_key());

    // receive the shared key from bob and decrypt it
    let encrypted_shared_key = EncryptedMessage::deserialize(rx.recv().unwrap()).unwrap();
    let shared_key = SharedKey::decrypt_owned(&encrypted_shared_key, &combined_key).unwrap();

    // create a message, encrypt it using the shared key and send it to bob.
    let msg = Message {
        content: Content {
            title: "my heart",
            sentence: "I ❤️ you.",
        },
        sender: "Alice",
    };
    let encrypted_message = msg.encrypt(&shared_key).unwrap();
    tx.send(encrypted_message.serialize()).unwrap();
}

#[derive(Debug, Serialize, Deserialize)]
struct Content<'a> {
    title: &'a str,
    sentence: &'a str,
}

#[derive(Debug, Serialize, Deserialize)]
struct Message<'a> {
    content: Content<'a>,
    sender: &'a str,
}

impl<'a> SerdeEncryptSharedKey for Message<'a> {
    type S = BincodeSerializer<Self>;
}

相关问题