简体   繁体   English

使用BigInteger probablePrime函数进行RSA加密是否安全?

[英]Is using the BigInteger probablePrime function safe for RSA encryption?

I have written a code that is able to generate 2048 bit prime p & q's and use that to encrypt messages in RSA. 我编写了一个代码,该代码能够生成2048位素数p&q并将其用于加密RSA中的消息。 The way I generate these numbers is by using the java.math.BigInteger package's probablePrime() function. 我生成这些数字的方式是使用java.math.BigInteger包的probablePrime()函数。 My question is how strong of an encryption these function generated prime numbers are in terms of encryption. 我的问题是这些函数生成的素数在加密方面的加密强度如何。

Here is my code for generating these numbers, isPrime is just a boolean function I wrote to check if the number is prime. 这是我生成这些数字的代码,isPrime只是我编写的用于检查数字是否为质数的布尔函数。

BigInteger definitePrime(int bits, Random rnd) {
  BigInteger prime = new BigInteger("4");
  while (!isPrime (prime)) {
    prime = BigInteger.probablePrime(bits, rnd);
  }
  return prime;
}

As Stephen C points out in his answer , the primes are probably ok for RSA encryption. 正如Stephen C在他的答案中指出的那样,素数对于RSA加密可能是可以的。

Cryptographic randomness 密码随机性

I would add, that you shouldn't actually use any Random instance, but only your systems best SecureRandom implementation. 我要补充一点,您实际上不应使用任何Random实例,而应仅使用系统最佳的SecureRandom实现。

new Random() is not a cryptographic randomness source, whereas new SecureRandom() should be. new Random()不是加密的随机性源,而new SecureRandom()应该是。 If the random numbers that are used for prime generation are not cryptographically secure, then an attacker may have a chance to simply recreate those based on other information (such as time or previous outputs of the weak randomness source). 如果用于素数生成的随机数不是密码安全的,那么攻击者就有机会根据其他信息(例如时间或弱随机性源的先前输出)简单地重新创建那些随机数。

No Textbook RSA 没有教科书RSA

You're doing "everything" yourself and it seems that you actually want to use this for serious encryption. 您自己在做“所有事情”,看来您实际上是想使用它进行认真的加密。 If you are, you're missing something crucial and that is the padding scheme. 如果是的话,那么您就缺少了一些关键的东西,那就是填充方案。

It is easy to use the BigInteger methods to implement RSA so that it works, but it's not enough to make it secure. 使用BigInteger方法来实现RSA使其易于工作是很容易的,但是不足以使其安全。 You need to use a padding scheme like PKCS#1 v1.5 (not recommended anymore) or PKCS#1 v2 OAEP (recommended). 需要使用填充方案,例如PKCS#1 v1.5(不再推荐)或PKCS#1v2 OAEP(推荐)。

Use existing implementations 使用现有的实现

Instead of implementing those padding schemes for your "handmade" RSA, use Java's Cipher instance which provides RSA with those padding schemes: 与其为您的“手工” RSA实现这些填充方案,不如使用Java的Cipher实例为RSA提供这些填充方案:

  • RSA/ECB/PKCS1Padding
  • RSA/ECB/OAEPWithSHA-256AndMGF1Padding

The javadoc for BigInteger.probablePrime() says: BigInteger.probablePrime()Javadoc说:

Returns a positive BigInteger that is probably prime, with the specified bitLength. 返回带有指定bitLength的正BigInteger(可能是素数)。 The probability that a BigInteger returned by this method is composite does not exceed 2 -100 . 此方法返回的BigInteger合成的概率不超过 2 -100

2 -100 means one chance in 1,267,650,600,228,229,401,496,703,205,376; 2 -100表示在1,267,650,600,228,229,401,496,703,205,376中的一次机会; ie 1 chance in ~1.267 * 10 30 即在〜1.267 * 10 30中有 1次机会

Since you are trying to generating 2 primes, that means that you have 2 chances in roughly 10 30 of generating a weak RSA key pair. 由于您尝试生成2个素数,因此大约10 30中有2次生成弱RSA密钥对的机会。

I'd have thought that that was good enough, but if you don't think so then you could use BigInteger.isProbablePrime(certainty) to test your prime candidates to an even higher level of certainty. 我以为那已经足够好了,但是如果您不这样认为,则可以使用BigInteger.isProbablePrime(certainty)对您的主要候选人进行更高程度的确定性测试。


My question is how strong of an encryption these function generated prime numbers are in terms of encryption. 我的问题是这些函数生成的素数在加密方面的加密强度如何。

I don't know if there is a mathematically rigorous way to quantify the strength of an encryption algorithm. 我不知道是否存在一种数学上严格的方法来量化加密算法的强度。 But the above analysis tells you the probability that a given generated key-pair will be weak / easy to crack. 但是上面的分析告诉您,给定生成的密钥对将很弱/容易破解的可能性。

The generated primes are not secure if you use something like java.util.Random instead of an instance of SecureRandom. 如果使用java.util.Random之类的东西代替SecureRandom的实例,则生成的素数是不安全的。 Your code snippet does not tell us what you are using, hence it is not possible to validate your code. 您的代码段没有告诉我们您正在使用什么,因此无法验证您的代码。 Of course, you probably should just use JCE to generate new RSA keys. 当然,您可能应该只使用JCE生成新的RSA密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM