问题描述
我正在尝试用 C++ 中的 RSA 加密一些文本,
加密时我生成 n,e,d
但尝试解密时,私钥初始值设定项说密钥无效...
所以我构建了一个生成密钥的代码,然后尝试在此之后立即初始化一个私钥对象,它说密钥仍然无效:
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,d);
}
程序崩溃到:InvertibleRSAFunction: input is not a valid RSA private key
解决方法
您的问题是您在密钥生成后修改了指数。
params.GenerateRandomWithKeySize(prng,2048);
params.SetPublicExponent(65537);
其实key是在第一行生成的,e = 17。尝试改变行序。
params.SetPublicExponent(65537);
params.GenerateRandomWithKeySize(prng,2048);