如何初始化RSA :: PrivateKey?

时间:2016-06-13 05:12:25

标签: c++ rsa public-key-encryption crypto++

我尝试使用Crypto ++的RSA加密。问题是如何从数字字符串初始化RSA::PrivateKey

生成密钥对的代码(来自here

cout << hex ;

AutoSeededRandomPool rng;
InvertibleRSAFunction params;
params.GenerateRandomWithKeySize(rng, 2048);

params.SetPublicExponent(65537);

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();

///////////////////////////////////////
// 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;

所以我得到 n,d,e 字符串,应该能够初始化私钥,然后我找到了一些示例代码here

Integer n("0xbeaadb3d839f3b5f"), e("0x11"), d("0x21a5ae37b9959db9");

RSA::PrivateKey privKey;
privKey.Initialize(n, e, d);

RSA::PublicKey pubKey;
pubKey.Initialize(n, e);

代码有效,它不会抛出任何异常。所以我尝试将n,e,d更改为先前生成的字符串。

// n: b0f2bee69386528216049775704d402cb3ff443ca2ea25a74b11c1c9c321b7ea46327b4e413f532616812fece07d061cf96e373789b3b9b05d2d31174c700a066868d26c52b5d48e6dbaf664fac66ee31747133a6569e16d12f521b56a12aadd74e7cf2534353a5e338173b8f884a568a25173f3a33782f9047af59da9b21180534923e5210c3989851f0d69d68d92c272769fbf2a833e2f522f60f76bec12d3b194c2f3b945c913649e2be54295a2f58e7c040bf61421f01077fdf234ddfe73663deec8979256c721fd65c046a7d21530adec1af2922ed6a27004bf31a04cd57981ca22208572743b6b64d4d30b0efe446fc7608b4178ff8a0ba7db3e45ecf3h
// e: 10001h
// d: 246e365ca5e6f2de8c100110a62e05aed9c39d1b8af3f8b1806589c7a82c96ce59bf1962ef50cd5aaa47c61a2e37db9c8db4cf2205c31eb35e7a3ed017443e4c9d0685ace3da243b70f1c951067425e375bbcf40ba86bd7856b9ff691d5e323ca720aaa5c6fbe65eb0404c87f6ee220e034d0148bfb89af70873ab09df2c30c74104b0973aa4e93ca95db749da4f6b2d9594ab487db1f6f194ab0b77bd91d834daf269c63d3abecad54a1a71599524e679a425c55b16a9ff7f0c37b2d259eb44ea5782f314f61cc0ac874b2e6ae870d798e90e5bc96ab57c8fd904fa9d199c46c971de3a5d7cabfdca0663373843bd41ec246e158754dabc9ec2172f7a5982edh

RSA::PrivateKey privKey;
privKey.Initialize(n, e, d);

它崩溃了。我用Google搜索了一段时间,发现some other tip

InvertibleRSAFunction params;
params.Initialize(n, e, d);

RSA::PrivateKey(params);

但它仍然崩溃。什么是初始化2048位rsa私钥的正确方法?

1 个答案:

答案 0 :(得分:1)

    // n: b0f2bee69386528216049775704d402cb3ff443ca2ea25a74b11c1c9c321b7ea46327b4e413f532616812fece07d061cf96e373789b3b9b05d2d31174c700a066868d26c52b5d48e6dbaf664fac66ee31747133a6569e16d12f521b56a12aadd74e7cf2534353a5e338173b8f884a568a25173f3a33782f9047af59da9b21180534923e5210c3989851f0d69d68d92c272769fbf2a833e2f522f60f76bec12d3b194c2f3b945c913649e2be54295a2f58e7c040bf61421f01077fdf234ddfe73663deec8979256c721fd65c046a7d21530adec1af2922ed6a27004bf31a04cd57981ca22208572743b6b64d4d30b0efe446fc7608b4178ff8a0ba7db3e45ecf3h
    // e: 10001h
    // d: 246e365ca5e6f2de8c100110a62e05aed9c39d1b8af3f8b1806589c7a82c96ce59bf1962ef50cd5aaa47c61a2e37db9c8db4cf2205c31eb35e7a3ed017443e4c9d0685ace3da243b70f1c951067425e375bbcf40ba86bd7856b9ff691d5e323ca720aaa5c6fbe65eb0404c87f6ee220e034d0148bfb89af70873ab09df2c30c74104b0973aa4e93ca95db749da4f6b2d9594ab487db1f6f194ab0b77bd91d834daf269c63d3abecad54a1a71599524e679a425c55b16a9ff7f0c37b2d259eb44ea5782f314f61cc0ac874b2e6ae870d798e90e5bc96ab57c8fd904fa9d199c46c971de3a5d7cabfdca0663373843bd41ec246e158754dabc9ec2172f7a5982edh
    RSA::PrivateKey privKey;
    privKey.Initialize(n, e, d);
     

它崩溃了。

我们需要查看实际代码。我猜两件事。首先(1),您没有使用try / catch,因此程序因未捕获的异常而终止。解决这个问题:

try
{
    // Some operation
}
catch (const Exception& ex)
{
    cerr << ex.what() << endl;
}

第二个(2),您在Iniaitialize的调用中使用字符串而不是整数。解决这个问题:

string n = "b0f2bee693865282...8a0ba7db3e45ecf3h";
string e = "10001h";
string d = "246e365ca5e6f2de...9ec2172f7a5982edh";

Integer _n(n.c_str()), _e(e.c_str()), _d(d.c_str());
RSA::PrivateKey privKey;
privKey.Initialize(_n, _e, _d);

第二个问题本应该被编译器捕获。您应该考虑使用-Wall来获得一组基本的编译器诊断。

可能存在第三个问题。那就是“关键无法验证”案例。如果您按照(1)中的描述添加try / catch,那么您可能会在CryptoMaterial类中看到“无效的密钥材料”或类似内容。在这种情况下,您的参数不会根据Validate执行的检查进行验证。

Validate使用低级别的彻底性(下面的Initialize参数)调用

levelValidate功能可以在InvertibleRSAFunction::Validate的手册中找到。点击链接Definition at line 247 of file rsa.cpp

  247 bool InvertibleRSAFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
  248 {
  249     bool pass = RSAFunction::Validate(rng, level);
  250     pass = pass && m_p > Integer::One() && m_p.IsOdd() && m_p < m_n;
  251     pass = pass && m_q > Integer::One() && m_q.IsOdd() && m_q < m_n;
  252     pass = pass && m_d > Integer::One() && m_d.IsOdd() && m_d < m_n;
  253     pass = pass && m_dp > Integer::One() && m_dp.IsOdd() && m_dp < m_p;
  254     pass = pass && m_dq > Integer::One() && m_dq.IsOdd() && m_dq < m_q;
  255     pass = pass && m_u.IsPositive() && m_u < m_p;
  256     if (level >= 1)
  257     {
  258         pass = pass && m_p * m_q == m_n;
  259         pass = pass && m_e*m_d % LCM(m_p-1, m_q-1) == 1;
  260         pass = pass && m_dp == m_d%(m_p-1) && m_dq == m_d%(m_q-1);
  261         pass = pass && m_u * m_q % m_p == 1;
  262     }
  263     if (level >= 2)
  264         pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
  265     return pass;
  266 }

剩下的未解决的问题可能是,m_pm_q等来自哪里?答案是Initialize因子n基于ed,并填充CRT values,如pq,{ {1}},dp等。它会加快计算速度。

这可能是相关的.... dq告诉我Crypto ++可能没有生成密钥对。如果Crypto ++生成密钥对,则默认情况下将使用e=17。你是如何生成密钥对的?