有没有一种方法可以在 C# 中生成大素数,而不使用外部库?
我需要为密码学项目生成大量素数。我注意到 .NET 4.0 有一些内置的加密原语(例如 RSA),它们使用随机生成的大素数(RSA 的 p、q)。它们是否都使用公共的公共内置库,可以从其类范围之外访问,或者我是否必须使用外部库(我知道有简单的素性测试算法,我只是不想实施比我必须做的更多的事情。)。
I need to generate large prime numbers for a cryptography project. I noticed that .NET 4.0 has some built-in cryptographic primitive (for example RSA) which use random generated large primes (p,q for RSA). Do they all use a common built-in library which is public and can be accessed from outside their class scopes, or do I have to use an external library (I know there are simple algorithms for primality tests, I just don't want to implement more than i have to.).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
生成您需要的范围内的大量数字。测试一下它是否是素数。如果不是,请拒绝并重复。
对于测试,只需使用素数高达 1500 的试除法,然后切换到 Miller-Rabin。正确实施 Miller-Rabin 后,出现硬件故障的可能性比错误地将复合材料标记为素数的可能性更大。
Generate a large number in the range you require. Test it to see if it is prime. Reject and repeat if it isn't.
For the testing, just use trial division with primes up to, say, 1500 and then switch to Miller-Rabin. With a properly implemented Miller-Rabin the chances of a hardware failure are greater than mistakenly flagging a composite as prime.
在 .NET v4(及更高版本)中,Microsoft 提供了一个新程序集
System.Numerics.dll
,其中包含BigInteger
类型。但是它没有提供任何检查素数的方法。Mono(自 1.0 之前)还提供了位于其
Mono.Security.dll
程序集中的[BigInteger][3]
类型。您可以按原样使用它,也可以将质数检查方法(存在多种方法)移植到新的 Microsoft BigInteger 类型。是的,
RSACryptoServiceProvider
和DSACryptoServiceProvider
都会调用 CryptoAPI 来执行此操作。然而,CAPI 不会公开它自己的 BigInteger 代码(甚至是本机代码),因此它对您没有帮助。In .NET v4 (and later) Microsoft provides a new assembly,
System.Numerics.dll
, which includes aBigInteger
type. However it does not provide any method to check for primes.Mono (since before 1.0) also provides a
[BigInteger][3]
type located in it'sMono.Security.dll
assembly. You can either use it as is or port the prime-checking methods (several methods exists) to the new MicrosoftBigInteger
type.Yes, both
RSACryptoServiceProvider
andDSACryptoServiceProvider
calls into CryptoAPI to do this. However CAPI does not expose it's own BigInteger code (even to native code) so it won't help you.