基于 Diffie Hellman 生成公钥和私钥并协调共享密钥

问题描述 投票:0回答:1

我在根据共同值为 2 个用户 Bob Alice 生成共同私钥和公钥时遇到问题,使用这些密钥 c++ 库 cryptocpp 并加密数据。
谢谢大家的建议和意见

#include <iostream>
#include <string>
#include <cryptopp/cryptlib.h>
#include <cryptopp/dh.h>
#include <cryptopp/dh2.h>
#include <cryptopp/osrng.h>
#include <cryptopp/integer.h>
#include <cryptopp/nbtheory.h>
#include <iostream>
#include <hex.h>
using namespace std;
/*
using CryptoPP::RSA;
using CryptoPP::InvertibleRSAFunction;
using CryptoPP::RSAES_OAEP_SHA_Encryptor;
using CryptoPP::RSAES_OAEP_SHA_Decryptor;
*/
#include <rsa.h>

#include <modes.h>
using CryptoPP::CFB_Mode;

CryptoPP::Integer my_pow(CryptoPP::Integer base, CryptoPP::Integer exponent) 
{

    if (exponent == 0)
    {
        return 1;
    }
    else if (exponent == 1) {
        return base;
    }
    else
    {
        CryptoPP::Integer n = exponent / 2;
        CryptoPP::Integer xn2 = my_pow(base, n);

        if (exponent % 2 == 0) {
      return xn2 * xn2;
        }
        else {
      return xn2 * base;
        }
    }
}


CryptoPP::Integer power(CryptoPP::Integer a, CryptoPP::Integer b, CryptoPP::Integer P)
{
    if (b == 1)
        return a;
    else
        return (((CryptoPP::Integer)my_pow(a, b)) % P);
}

int main(int argc, char* argv[])
{
    std::cout << std::hex;
    
    //All User ALICE BOB CryptoPP::Integer p, q,
    CryptoPP::AutoSeededRandomPool rnd;
    CryptoPP::PrimeAndGenerator pg;
    pg.Generate(1, rnd, 512, 511);
    const CryptoPP::Integer p = pg.Prime();
    const CryptoPP::Integer q = pg.SubPrime();

    std::cout << "p" << p << endl;
    std::cout << "q" << q << endl;


    // Alice
    CryptoPP::AutoSeededRandomPool prngA;
    CryptoPP::PrimeAndGenerator pgA;
    pgA.Generate(1, prngA, 512, 511);
    CryptoPP::Integer a = pgA.Generator();;
    // Bob
    CryptoPP::AutoSeededRandomPool prngB;
    CryptoPP::PrimeAndGenerator pgB;
    pgB.Generate(1, prngB, 512, 511);
    CryptoPP::Integer b = pgB.Generator();;
    
    //Public aPublic = power(q, a, P);
    CryptoPP::Integer aPublic = power(q, a, p);
    
    //Public bPublic = power(q, a, P);
    CryptoPP::Integer bPublic = power(q, b, p);
    
    std::cout << "A Alice Key"; 
    std::cout << a << endl;
    std::cout << "B Bob Key";
    std::cout << b << endl;

    
    //Alice 
    CryptoPP::Integer kb = power(bPublic, a, p); // Secret key for Bob
   
    //Bob
    CryptoPP::Integer ka = power(aPublic, b, p); // Secret key for Alice

    std::cout <<"Bob Key" << kb << endl;
    std::cout <<"Alice Key" << ka << endl;
    if (kb == ka)
        std::cout << "Same"<<"\n";
    //Alice 
    CryptoPP::DH dhA = CryptoPP::DH(p, q, kb);
    CryptoPP::SecByteBlock t1A(dhA.PrivateKeyLength()), t2A(dhA.PublicKeyLength());
    dhA.GenerateKeyPair(rnd, t1A, t2A);
    std::cout << "Private Key" << t1A << "\n" << "Public Key" << t2A << "\n";

    //Bob
    CryptoPP::DH dhB = CryptoPP::DH(p, q, ka);
    CryptoPP::SecByteBlock t1B(dhB.PrivateKeyLength()), t2B(dhB.PublicKeyLength());
    dhB.GenerateKeyPair(rnd, t1B, t2B);
    std::cout << "Private Key" << t1B << "\n" << "Public Key" << t2B << "\n";


    return true;
}

不明白为什么尽管值相同,但会生成不同的公钥和私钥。

c++ crypto++
1个回答
0
投票

A 和 B 使用相同的随机数生成器。

// Common random number generator
CryptoPP::AutoSeededRandomPool rnd;

// Alice
CryptoPP::PrimeAndGenerator pgA;
pgA.Generate(1, rnd, 512, 511);
CryptoPP::Integer a = pgA.Generator();

// Bob
CryptoPP::PrimeAndGenerator pgB;
pgB.Generate(1, rnd, 512, 511);
CryptoPP::Integer b = pgB.Generator();
© www.soinside.com 2019 - 2024. All rights reserved.