אביב נח
אביב נח

Reputation: 171

CryptoPP generates invalid keys RSA

I'm trying to encrypt some text with RSA in c++,

when encrypting I'm generating n, e, d but when trying to decrypt, the private key initializer says the keys are invalid...

So I built a code that generates the keys and then tries to initialize a private key object right after that and it says the keys are still invalid:

int main()
{
    
    CryptoPP::InvertibleRSAFunction params;
    CryptoPP::AutoSeededRandomPool prng;
    params.GenerateRandomWithKeySize(prng, 2048);
    params.SetPublicExponent(65537);

    const CryptoPP::Integer& n = params.GetModulus();
    const CryptoPP::Integer& p = params.GetPrime1();
    const CryptoPP::Integer& q = params.GetPrime2();
    const CryptoPP::Integer& d = params.GetPrivateExponent();
    const CryptoPP::Integer& e = params.GetPublicExponent();

    CryptoPP::RSA::PrivateKey privk;
    privk.Initialize(n, e, d);
}

The program crashes to: InvertibleRSAFunction: input is not a valid RSA private key

Upvotes: 4

Views: 617

Answers (1)

3CxEZiVlQ
3CxEZiVlQ

Reputation: 38893

You problem is that you modified the exponent after the key was already generated.

params.GenerateRandomWithKeySize(prng, 2048);
params.SetPublicExponent(65537);

Actually the key was generated in the first line with e = 17. Try to change the line order.

params.SetPublicExponent(65537);
params.GenerateRandomWithKeySize(prng, 2048);

Upvotes: 3

Related Questions