C#随机BigInt生成器

编程入门 行业动态 更新时间:2024-10-27 10:31:56
本文介绍了C#随机BigInt生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我即将实现 DSA算法,但是存在问题:

I'm about to implement the DSA algorithm, but there is a problem:

选择"p",它是L位的质数,其中512< = L< = 1024,L是64的倍数.

choose "p", a prime number with L bits, where 512 <= L <= 1024 and L is a multiple of 64

如何实现该数量的随机生成器? Int64的长度只有63位.

How can I implement a random generator of that number? Int64 has "only" 63 bits length.

推荐答案

您可以使用以下代码生成带有n位的随机数:

You can generate a random number with n bits using this code:

var rng = new RNGCryptoServiceProvider(); byte[] bytes = new byte[n / 8]; rng.GetBytes(bytes); BigInteger p = new BigInteger(bytes);

结果当然是随机的,不一定是素数.

The result is, of course, random and not necessarily a prime.

引入了 BigInteger类在.NET 4.0 Framework中.

The BigInteger class was introduced in the .NET 4.0 Framework.

要生成大素数,维基百科说:

对于密码术中使用的大质数,通常使用改进的筛分形式:将所需大小的奇数随机选择范围与相对较小的奇数质数(通常所有质数小于65,000).其余候选素数按标准的素数检验(例如Miller-Rabin素数检验)以随机顺序进行测试,以检验可能的素数.

For the large primes used in cryptography, it is usual to use a modified form of sieving: a randomly-chosen range of odd numbers of the desired size is sieved against a number of relatively small odd primes (typically all primes less than 65,000). The remaining candidate primes are tested in random order with a standard primality test such as the Miller-Rabin primality test for probable primes.

因此您可以执行以下操作:

So you could do something like this:

var p = Enumerable.Range(0, numberOfCandidates) .Select(i => RandomOddNumber(bits)) .Where(x => !primesLessThan65000.Contains(x)) .Where(x => PrimalityTest(x)) .FirstOrDefault();

更多推荐

C#随机BigInt生成器

本文发布于:2023-11-10 09:19:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574980.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:生成器   BigInt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!