当前位置: 首页>>代码示例>>C#>>正文


C# BigInteger.IsProbablePrime方法代码示例

本文整理汇总了C#中Mono.Math.BigInteger.IsProbablePrime方法的典型用法代码示例。如果您正苦于以下问题:C# BigInteger.IsProbablePrime方法的具体用法?C# BigInteger.IsProbablePrime怎么用?C# BigInteger.IsProbablePrime使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Mono.Math.BigInteger的用法示例。


在下文中一共展示了BigInteger.IsProbablePrime方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Initialize

 // initializes the private variables (throws CryptographicException)
 private void Initialize(BigInteger p, BigInteger g, BigInteger x, int secretLen, bool checkInput)
 {
     if (!p.IsProbablePrime() || g <= 0 || g >= p || (x != null && (x <= 0 || x > p - 2)))
         throw new CryptographicException();
     // default is to generate a number as large as the prime this
     // is usually overkill, but it's the most secure thing we can
     // do if the user doesn't specify a desired secret length ...
     if (secretLen == 0)
         secretLen = p.BitCount();
     m_P = p;
     m_G = g;
     if (x == null) {
         BigInteger pm1 = m_P - 1;
         for(m_X = BigInteger.GenerateRandom(secretLen); m_X >= pm1 || m_X == 0; m_X = BigInteger.GenerateRandom(secretLen)) {}
     } else {
         m_X = x;
     }
 }
开发者ID:tt,项目名称:dotnetopenid,代码行数:19,代码来源:DiffieHellmanManaged.cs

示例2: ExpectPrime

		private void ExpectPrime (BigInteger bi)
		{
			Assertion.AssertEquals (true, bi.IsProbablePrime ());
		}
开发者ID:Jakosa,项目名称:MonoLibraries,代码行数:4,代码来源:PrimeTestingTest.cs

示例3: GenerateParams

		private void GenerateParams (int keyLength) 
		{
			byte[] seed = new byte[20];
			byte[] part1 = new byte[20];
			byte[] part2 = new byte[20];
			byte[] u = new byte[20];

			// TODO: a prime generator should be made for this

			SHA1 sha = SHA1.Create ();

			int n = (keyLength - 1) / 160;
			byte[] w = new byte [keyLength / 8];
			bool primesFound = false;

			while (!primesFound) {
				do {
					Random.GetBytes (seed);
					part1 = sha.ComputeHash (seed);
					Array.Copy(seed, 0, part2, 0, seed.Length);

					add (part2, seed, 1);

					part2 = sha.ComputeHash (part2);

					for (int i = 0; i != u.Length; i++)
						u [i] = (byte)(part1 [i] ^ part2 [i]);

					// first bit must be set (to respect key length)
					u[0] |= (byte)0x80;
					// last bit must be set (prime are all odds - except 2)
					u[19] |= (byte)0x01;

					q = new BigInteger (u);
				}
				while (!q.IsProbablePrime ());

				counter = 0;
				int offset = 2;
				while (counter < 4096) {
					for (int k = 0; k < n; k++) {
						add(part1, seed, offset + k);
						part1 = sha.ComputeHash (part1);
						Array.Copy (part1, 0, w, w.Length - (k + 1) * part1.Length, part1.Length);
					}

					add(part1, seed, offset + n);
					part1 = sha.ComputeHash (part1);
					Array.Copy (part1, part1.Length - ((w.Length - (n) * part1.Length)), w, 0, w.Length - n * part1.Length);

					w[0] |= (byte)0x80;
					BigInteger x = new BigInteger (w);

					BigInteger c = x % (q * 2);

					p = x - (c - 1);

					if (p.TestBit ((uint)(keyLength - 1))) {
						if (p.IsProbablePrime ()) {
							primesFound = true;
							break;
						}
					}

					counter += 1;
					offset += n + 1;
				}
			}

			// calculate the generator g
			BigInteger pMinusOneOverQ = (p - 1) / q;
			for (;;) {
				BigInteger h = BigInteger.GenerateRandom (keyLength);
				if ((h <= 1) || (h >= (p - 1)))
					continue;

				g = h.ModPow (pMinusOneOverQ, p);
				if (g <= 1)
					continue;
				break;
			}

			this.seed = new BigInteger (seed);
			j = (p - 1) / q;
		}
开发者ID:jack-pappas,项目名称:mono,代码行数:85,代码来源:DSAManaged.cs

示例4: ExpectComposite

		private void ExpectComposite (BigInteger bi)
		{
			Assertion.AssertEquals (false, bi.IsProbablePrime ());
		}
开发者ID:Jakosa,项目名称:MonoLibraries,代码行数:4,代码来源:PrimeTestingTest.cs

示例5: ExpectPrime

		private void ExpectPrime (BigInteger bi)
		{
			Assert.AreEqual (true, bi.IsProbablePrime ());
		}
开发者ID:ItsVeryWindy,项目名称:mono,代码行数:4,代码来源:PrimeTestingTest.cs


注:本文中的Mono.Math.BigInteger.IsProbablePrime方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。