如何交换公钥

How to exchange public keys?

本文关键字:公钥 交换 何交换      更新时间:2023-10-16

我有客户端和服务器,并希望在它们之间有加密通信通道。下面是一些生成公钥和私钥的简单代码:

    RSA::PrivateKey privateKey;
    privateKey.Initialize()
    ///////////////////////////////////////
    // Generate Parameters
    InvertibleRSAFunction params;
    params.GenerateRandomWithKeySize(this->rng, 3072);
    ///////////////////////////////////////
    // Generated Parameters
    const Integer& n = params.GetModulus();
    const Integer& p = params.GetPrime1();
    const Integer& q = params.GetPrime2();
    const Integer& d = params.GetPrivateExponent();
    const Integer& e = params.GetPublicExponent();
    params.
    ///////////////////////////////////////
    // Dump
    cout << "RSA Parameters:" << endl;
    cout << " n: " << n << endl;
    cout << " p: " << p << endl;
    cout << " q: " << q << endl;
    cout << " d: " << d << endl;
    cout << " e: " << e << endl;
    cout << endl;
    ///////////////////////////////////////
    // Create Keys
    RSA::PrivateKey privateKey(params);
    RSA::PublicKey publicKey(params);

但是我如何在服务器端获得客户端的公钥和在客户端获得服务器端的公钥呢?是否存在比在文件中序列化公钥,在另一端发送文件,接收文件并反序列化更简单的方法?

ChatDataTransferingInterface实例,指向ServerClient(取决于用户在开始时是否选择)。一些理解代码:

类聊天:

class Chat : public OwnerServerInterface, public OwnerClientInterface
    public:
        //a lot of methods
    protected:
        virtual void handshakeServerSide(int clientSocket, void *objectForSaveIn, void *dataToSend);
        virtual void handshakeClientSide(int serverSocket, void *objectForSaveIn, void *dataToSend);
    private:
        DataTransferingInterface* interface;

handshakeServerSide()handshakeClientSide()方法中,服务器的Chat实例和客户端的Chat实例进行握手(数据交换)。在这种方法中,服务器必须发送自己的公钥,并获取客户端的公钥。但是怎么做呢?

…我如何在服务器端获得客户端的公钥,在客户端获得服务器的公钥?

这就是众所周知的密钥分配问题。在小范围内,这可能是(也可能不是)可控的,但当你扩大规模时,这是一个非常棘手的问题。

答案取决于您的特定实例问题,这看起来像一个N-way聊天程序。在Stack Overflow这样的编程网站上,这个问题实在太多了。

也许你应该仔细研究一下这个问题,包括多方或群迪菲-赫尔曼方案。还要考虑一下,当一个成员离开一个团队时,你希望发生什么。然后在信息安全堆栈交换上提出有针对性的问题

下面的代码是非常简单的Crypto++ RSA算法的类包装器:

EncoderRSA.h

class EncoderRSA
{
    public:
        EncoderRSA();
        void keyGeneration();
        void substitutePublicKey(Integer e, Integer n);
        Integer encode(std::string plainText);
        std::string decode(Integer cypher);
        Integer getE();
        Integer getN();
    private:
        AutoSeededRandomPool prng;
        RSA::PublicKey publicKey;   // For encrypt plain text
        RSA::PrivateKey privateKey; // For decrypt plain text
};

EncoderRSA.cpp

void EncoderRSA::keyGeneration() {
    InvertibleRSAFunction params;
    params.GenerateRandomWithKeySize(this->prng, 3072);
    RSA::PrivateKey privateKey(params);
    RSA::PublicKey publicKey(params);
    //because you can't do:    this->privateKey(params);
    this->privateKey = privateKey;
    this->publicKey = publicKey;
}
void EncoderRSA::substitutePublicKey(Integer e, Integer n) {
    this->publicKey.Initialize(n, e);
}
Integer EncoderRSA::encode(std::string plainText) {
    Integer m((const byte*)plainText.data(), plainText.size());
    Integer c = this->publicKey.ApplyFunction(m);
    return c;
}
std::string EncoderRSA::decode(Integer cypher) {
    Integer r = this->privateKey.CalculateInverse(this->prng, cypher);
    std::string decoded;
    size_t req = r.MinEncodedSize();
    decoded.resize(req);
    r.Encode((byte*)decoded.data(), decoded.size());
    return decoded;
}
Integer EncoderRSA::getE() {
    return this->publicKey.GetPublicExponent();
}
Integer EncoderRSA::getN() {
    return this->publicKey.GetModulus();
}

服务器必须生成自己的密钥(公钥和私钥),保存它,单独保存自己的公钥(如果确切:PublicExponent和PublicModulus)(在Integer变量中),从客户端获取公钥,用客户端的公钥替换自己的公钥。

客户端也一样