生成非常大的素数

发布于 2024-07-27 18:53:19 字数 75 浏览 7 评论 0原文

我正在尝试编写 RSA 的实现。 问题是我一直在生成生成密钥对所涉及的大量素数。 有人能指出一种快速生成巨大素数/可能素数的方法吗?

I'm playing around and trying to write an implementation of RSA. The problem is that I'm stuck on generating the massive prime numbers that are involved in generating a key pair. Could someone point me to a fast way to generate huge primes/probable primes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倾城月光淡如水﹏ 2024-08-03 18:53:20

你不能准确地生成素数。 您随机生成一个大奇数,然后测试该数字是否为素数,如果不是则随机生成另一个。 有一些素数定律,基本上规定通过随机尝试“击中”素数的几率是 (2/ln n)

例如,如果您想要一个 512 位随机素数,您会发现 2/ 之一(512*ln(2))
因此,您尝试的每 177 个数字中,大约有 1 个是素数。

有多种方法可以测试一个数字是否为素数,一个好的方法是“米勒-拉宾测试”如另一个答案中所述问题

此外,OpenSSL 有一个很好的实用程序来测试素数:

$ openssl prime 119054759245460753
1A6F7AC39A53511 is not prime

You don't generate prime numbers exactly. You generate a large odd number randomly, then test if that number is prime, if not generate another one randomly. There are some laws of prime numbers that basically state that your odds of "hitting" a prime via random tries is (2/ln n)

For example, if you want a 512-bit random prime number, you will find one in 2/(512*ln(2))
So roughly 1 out of every 177 of the numbers you try will be prime.

There are multiple ways to test if a number is prime, one good one is the "Miller-Rabin test" as stated in another answer to this question.

Also, OpenSSL has a nice utility to test for primes:

$ openssl prime 119054759245460753
1A6F7AC39A53511 is not prime
Hello爱情风 2024-08-03 18:53:20

看看 TrueCrypt 是如何做到的。 另外,请查看 Rabin-Miller 用于测试大型伪素数。

Take a look at how TrueCrypt does it. Also, take a look at Rabin-Miller for testing large pseudoprimes.

久光 2024-08-03 18:53:20

U. Maurer 提出了一种算法,可以生成随机的可证明(与统计上高度可能的)素数,这些素数几乎均匀分布在所有特殊大小的素数集合上。 我有一个它的 Python 实现,它在以下方面相当有效:
http://s13.zetaboards.com/Crypto/topic/7234475/1/

There is an algorithm due to U. Maurer that generates random provable (in contrast to statistically highly-probable) primes that are almost uniformly distributed over the set of all primes of a special size. I have a Python implementation of it that is fairly efficient at:
http://s13.zetaboards.com/Crypto/topic/7234475/1/

阳光①夏 2024-08-03 18:53:20

你没有提到你使用什么语言。 有些内置了执行此操作的方法。例如,在 java 中,这就像调用 nextProbablePrime()

You didn't mention what language you are using. Some have a method of doing this built in. For example, in java this is as easy as calling nextProbablePrime() on a BigInteger.

花落人断肠 2024-08-03 18:53:20

Mono 有一个 BigInteger 类,它和 java 一样都是开源的。 你可以看看那些。 它们可能是便携式的:)祝你好运

Mono has a BigInteger class that's open source as does java. You could take a look at those. They're probably portable :) g'luck

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文