Crypto++ RSA 密钥大小限制

Crypto++ RSA key size limitation

我想将 Crypto++ 中使用的密钥大小限制为 256 位。我在代码中发现唯一包含关键字key的是变量aKeySize,以此类推,我发现它是摘要的字符数。

这是我复制程序的地方a link

RSA 对密钥如何定义 256 位的限制?

提前致谢!

How to define the limit of 256 bits only for the RSA pair keys?

来自你在link中的测试程序:

int main(int, char **) {
  auto keys = RsaGenerateHexKeyPair(3072);
  std::cout << "Private key: " << std::endl << keys.privateKey << "\n" << std::endl;

  std::cout << "Public key: " << std::endl << keys.publicKey << "\n" << std::endl;
  ...
}

您应该将 RsaGenerateHexKeyPair(3072) 更改为:

auto keys = RsaGenerateHexKeyPair(256);

如果您想在 Crypto++ 库中更改它,则修改 GenerateRandom 以在 bits 大于 256 时抛出 InvalidArgument 异常。

GenerateRandomWithKeySize 是 Crypto++ 深处基础 class 的一部分。它在cryptlib.cpp中实现,它的主体是:

void GeneratableCryptoMaterial::GenerateRandomWithKeySize(RandomNumberGenerator &rng, unsigned int keySize)
{
    GenerateRandom(rng, MakeParameters("KeySize", (int)keySize));
}

所以需要修改rsa.cpp中的GenerateRandom:

void InvertibleRSAFunction::GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
{
    int modulusSize = 2048;
    alg.GetIntValue(Name::ModulusSize(), modulusSize) || alg.GetIntValue(Name::KeySize(), modulusSize);

    CRYPTOPP_ASSERT(modulusSize >= 16);
    if (modulusSize < 16)
        throw InvalidArgument("InvertibleRSAFunction: specified modulus size is too small");

    m_e = alg.GetValueWithDefault(Name::PublicExponent(), Integer(17));

    CRYPTOPP_ASSERT(m_e >= 3); CRYPTOPP_ASSERT(!m_e.IsEven());
    if (m_e < 3 || m_e.IsEven())
        throw InvalidArgument("InvertibleRSAFunction: invalid public exponent");

    RSAPrimeSelector selector(m_e);
    AlgorithmParameters primeParam = MakeParametersForTwoPrimesOfEqualSize(modulusSize)
        (Name::PointerToPrimeSelector(), selector.GetSelectorPointer());
    m_p.GenerateRandom(rng, primeParam);
    m_q.GenerateRandom(rng, primeParam);

    m_d = m_e.InverseMod(LCM(m_p-1, m_q-1));
    CRYPTOPP_ASSERT(m_d.IsPositive());

    m_dp = m_d % (m_p-1);
    m_dq = m_d % (m_q-1);
    m_n = m_p * m_q;
    m_u = m_q.InverseMod(m_p);   
}

but the keys still so big ... (From an earlier comment)

256 位 RSA 通常被认为太小。对普通人来说,触手可及。

您应该考虑改用椭圆曲线。如果修复曲线并使用压缩的 public 点,那么您可以获得一个相当小的密钥,例如 secp256 的 32 字节数量级。