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


C# BigInteger.isProbablePrime方法代码示例

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


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

示例1: RSA

        public RSA(BigInteger p, BigInteger q)
        {
            if (!p.isProbablePrime())
            {
                throw new Exception("Podana przez Ciebie liczba p prawdopodobnie nie jest liczbą pierwszą!");
            }

            if (!q.isProbablePrime())
            {
                throw new Exception("Podana przez Ciebie liczba q prawdopodobnie nie jest liczbą pierwszą!");
            }

            this.p = p;
            this.q = q;
            this.n = this.p * this.q;
            this.eulerOfN = (this.p - 1) * (this.q - 1);
            Random randomGenerator = new Random(DateTime.Now.Millisecond);

            do
            {
                this.e = this.eulerOfN.genCoPrime(randomGenerator.Next(1, this.eulerOfN.bitCount()), randomGenerator);
            }
            while ((this.e > this.eulerOfN) && this.e.gcd(this.eulerOfN) > 1);

            this.d = e.modInverse(eulerOfN);
        }
开发者ID:oryagel,项目名称:sha1pb,代码行数:26,代码来源:RSA.cs

示例2: Main

        public static void Main(string[] args)
        {
                // Known problem -> these two pseudoprimes passes my implementation of
                // primality test but failed in JDK's isProbablePrime test.

                byte[] pseudoPrime1 = { (byte)0x00,
                        (byte)0x85, (byte)0x84, (byte)0x64, (byte)0xFD, (byte)0x70, (byte)0x6A,
                        (byte)0x9F, (byte)0xF0, (byte)0x94, (byte)0x0C, (byte)0x3E, (byte)0x2C,
                        (byte)0x74, (byte)0x34, (byte)0x05, (byte)0xC9, (byte)0x55, (byte)0xB3,
                        (byte)0x85, (byte)0x32, (byte)0x98, (byte)0x71, (byte)0xF9, (byte)0x41,
                        (byte)0x21, (byte)0x5F, (byte)0x02, (byte)0x9E, (byte)0xEA, (byte)0x56,
                        (byte)0x8D, (byte)0x8C, (byte)0x44, (byte)0xCC, (byte)0xEE, (byte)0xEE,
                        (byte)0x3D, (byte)0x2C, (byte)0x9D, (byte)0x2C, (byte)0x12, (byte)0x41,
                        (byte)0x1E, (byte)0xF1, (byte)0xC5, (byte)0x32, (byte)0xC3, (byte)0xAA,
                        (byte)0x31, (byte)0x4A, (byte)0x52, (byte)0xD8, (byte)0xE8, (byte)0xAF,
                        (byte)0x42, (byte)0xF4, (byte)0x72, (byte)0xA1, (byte)0x2A, (byte)0x0D,
                        (byte)0x97, (byte)0xB1, (byte)0x31, (byte)0xB3,
                };

                byte[] pseudoPrime2 = { (byte)0x00,
                        (byte)0x99, (byte)0x98, (byte)0xCA, (byte)0xB8, (byte)0x5E, (byte)0xD7,
                        (byte)0xE5, (byte)0xDC, (byte)0x28, (byte)0x5C, (byte)0x6F, (byte)0x0E,
                        (byte)0x15, (byte)0x09, (byte)0x59, (byte)0x6E, (byte)0x84, (byte)0xF3,
                        (byte)0x81, (byte)0xCD, (byte)0xDE, (byte)0x42, (byte)0xDC, (byte)0x93,
                        (byte)0xC2, (byte)0x7A, (byte)0x62, (byte)0xAC, (byte)0x6C, (byte)0xAF,
                        (byte)0xDE, (byte)0x74, (byte)0xE3, (byte)0xCB, (byte)0x60, (byte)0x20,
                        (byte)0x38, (byte)0x9C, (byte)0x21, (byte)0xC3, (byte)0xDC, (byte)0xC8,
                        (byte)0xA2, (byte)0x4D, (byte)0xC6, (byte)0x2A, (byte)0x35, (byte)0x7F,
                        (byte)0xF3, (byte)0xA9, (byte)0xE8, (byte)0x1D, (byte)0x7B, (byte)0x2C,
                        (byte)0x78, (byte)0xFA, (byte)0xB8, (byte)0x02, (byte)0x55, (byte)0x80,
                        (byte)0x9B, (byte)0xC2, (byte)0xA5, (byte)0xCB,
                };

                Console.WriteLine("List of primes < 2000\n---------------------");
                int limit = 100, count = 0;
                for(int i = 0; i < 2000; i++)
                {
                        if(i >= limit)
                        {
                                Console.WriteLine();
                                limit += 100;
                        }

                        BigInteger p = new BigInteger(-i);

                        if(p.isProbablePrime())
                        {
                                Console.Write(i + ", ");
                                count++;
                        }
                }
                Console.WriteLine("\nCount = " + count);


                BigInteger bi1 = new BigInteger(pseudoPrime1);
                Console.WriteLine("\n\nPrimality testing for\n" + bi1.ToString() + "\n");
                Console.WriteLine("SolovayStrassenTest(5) = " + bi1.SolovayStrassenTest(5));
                Console.WriteLine("RabinMillerTest(5) = " + bi1.RabinMillerTest(5));
                Console.WriteLine("FermatLittleTest(5) = " + bi1.FermatLittleTest(5));
                Console.WriteLine("isProbablePrime() = " + bi1.isProbablePrime());

                Console.Write("\nGenerating 512-bits random pseudoprime. . .");
                Random rand = new Random();
                BigInteger prime = BigInteger.genPseudoPrime(512, 5, rand);
                Console.WriteLine("\n" + prime);

                //int dwStart = System.Environment.TickCount;
                //BigInteger.MulDivTest(100000);
                //BigInteger.RSATest(10);
                //BigInteger.RSATest2(10);
                //Console.WriteLine(System.Environment.TickCount - dwStart);

        }
开发者ID:AudriusKniuras,项目名称:ChatClientServer,代码行数:73,代码来源:BigInteger.cs

示例3: genPseudoPrime

        //***********************************************************************
        // Generates a positive BigInteger that is probably prime.
        //***********************************************************************

        public static BigInteger genPseudoPrime(int bits, int confidence, Random rand)
        {
	        BigInteger result = new BigInteger();
	        bool done = false;

	        while(!done)
	        {
		        result.genRandomBits(bits, rand);
		        result.data[0] |= 0x01;		// make it odd

		        // prime test
		        done = result.isProbablePrime(confidence);
	        }
	        return result;
        }
开发者ID:AudriusKniuras,项目名称:ChatClientServer,代码行数:19,代码来源:BigInteger.cs

示例4: runAllPrimes

		void runAllPrimes()
		{
			b_runningAllPrimes=true;

			Hashtable ht = new Hashtable();

			if(!b_PrimesLoaded)
			{
				loadPrimes();
			}
            this.Invoke((MethodInvoker)delegate{
                lblAllPrimes.Text = "Searching for primes...";
            });

			v_startPrime=int.Parse(txtAllPrimesStart.Text);
			v_endPrime = int.Parse(txtAllPrimesEnd.Text);

			int v_num1=0;
			int v_bnum1=0;
			int v_num2=0;
			int v_cnt=0;

			if(!isOdd(v_startPrime))
			{
				v_startPrime++;
			}
			if(isOdd(v_endPrime))
			{
				v_endPrime++;
			}
			for(int i = v_startPrime;i<=v_endPrime;i=i+2)
			{
				if(!b_runningAllPrimes)
				{
					break;
				}
				v_num1=i;
				v_num2=v_endPrime-i;
                double v_num3 = Math.Floor(((double)v_num2) / 2);
                double v_num4 = Math.Floor(((double)v_num3) / 2);
                double v_num5 = Math.Floor(((double)v_num4) / 2);
                if (!isOdd(v_num3))
                {
                    v_num3++;
                }
                if (!isOdd(v_num4))
                {
                    v_num4++;
                }
                if (!isOdd(v_num5))
                {
                    v_num5++;
                }

                this.Invoke((MethodInvoker)delegate
                {
                    txtAllPrimesStart.Text = v_startPrime.ToString();
                    txtAllPrimesEnd.Text = v_endPrime.ToString();
                });
				if((v_cnt%50)==0)
				{
                    this.Invoke((MethodInvoker)delegate
                    {
                        lblAllPrimes.Text = v_num1.ToString() + " - " + v_num2.ToString() + " - " + v_num3.ToString() + " - " + v_num4.ToString() + " - " + v_num5.ToString() + "\n" + htctrl.Count.ToString() + " - " + ht.Count.ToString() + "\n" + listBoxAllPrimes.Items.Count.ToString();
                    });
				}
				BigInteger bi1 = new BigInteger(v_num1);
				BigInteger bi2 = new BigInteger(v_num2);
					
				if (isOdd(v_num1) && !htctrl.ContainsKey(v_num1) && !ht.ContainsKey(v_num1) && bi1.isProbablePrime(10))// isPrime(v_num1))
				{
					ht.Add(v_num1,v_num1);
					htctrl.Add(v_num1,v_num1);
				}
				if (isOdd(v_num2) && !htctrl.ContainsKey(v_num2) && !ht.ContainsKey(v_num2) && bi2.isProbablePrime(10))// isPrime(v_num2))
				{
					ht.Add(v_num2,v_num2);
					htctrl.Add(v_num2,v_num2);
				}
                if (isOdd(v_num3) && !htctrl.ContainsKey(v_num3) && !ht.ContainsKey(v_num3) && bi2.isProbablePrime(10))// isPrime(v_num3))
                {
                    ht.Add(v_num3, v_num3);
                    htctrl.Add(v_num3, v_num3);
                }
                if (isOdd(v_num4) && !htctrl.ContainsKey(v_num4) && !ht.ContainsKey(v_num4) && bi2.isProbablePrime(10))// isPrime(v_num4))
                {
                    ht.Add(v_num4, v_num4);
                    htctrl.Add(v_num4, v_num4);
                }
                if (isOdd(v_num5) && !htctrl.ContainsKey(v_num5) && !ht.ContainsKey(v_num5) && bi2.isProbablePrime(10))// isPrime(v_num5))
                {
                    ht.Add(v_num5, v_num5);
                    htctrl.Add(v_num5, v_num5);
                }
                string strtxtAllPrimesSaveAfter = "";
                this.Invoke((MethodInvoker)delegate
                {
                    strtxtAllPrimesSaveAfter=txtAllPrimesSaveAfter.Text;
                });
                if (ht.Count >= int.Parse(strtxtAllPrimesSaveAfter))
//.........这里部分代码省略.........
开发者ID:Hagser,项目名称:csharp,代码行数:101,代码来源:Form1.cs

示例5: gennr

 private void gennr(bool cb)
 {
     Random rn = new Random();
     if (cb == true)
     {
         do
         {
             q = BigInteger.genPseudoPrime(160, 100, rn);
             r = BigInteger.genPseudoPrime(864, 100, rn);
             p = new BigInteger(2 * q * r + 1);
         }
         while (!p.isProbablePrime(100));
     }
     else
     {
         q = new BigInteger("1357240200991138844597040462790625645217868549141", 10);
         r = new BigInteger("113598119916368178543726680204430381937875824948480914597565387121457881140775963786806669392598432983134451798231342845963966490631191394432315401337959287970864814764728934773343090591391155652090238298766028332871998156437678877229180157399463412668170350113", 10);
         p = new BigInteger("308359870215014078485257030866450886542952487603077718832462434453183859939031089613588944732186396626095766979593995910212976752989132511989881464667656451202440745056276306885148035449349526765128968559736391792793918308470057210553727052388063491635390548699144068380621588468735471047786563470306630805867", 10);
     }
 }
开发者ID:Rotariu-Stefan,项目名称:INFO-SI,代码行数:20,代码来源:client.cs

示例6: GenerateRSAKey

    public static void GenerateRSAKey(int moduleBitLen, int pubKeyBitLen, out BigInteger publicExponent, out BigInteger module, out BigInteger privateExponent)
    {
        /* from Wikipedia:
        RSA involves a public key and a private key. The public key can be known to everyone and is used for encrypting messages. Messages encrypted with the public key can only be decrypted using the private key. The keys for the RSA algorithm are generated the following way:
        Choose two distinct prime numbers p and q.
        For security purposes, the integers p and q should be chosen at random, and should be of similar bit-length. Prime integers can be efficiently found using a primality test.
        Compute n = pq.
        n is used as the modulus for both the public and private keys
        Compute φ(n) = (p – 1)(q – 1), where φ is Euler's totient function.
        Choose an integer e such that 1 < e < φ(n) and greatest common divisor of (e, φ(n)) = 1; i.e., e and φ(n) are coprime.
        e is released as the public key exponent.
        e having a short bit-length and small Hamming weight results in more efficient encryption - most commonly 0x10001 = 65,537. However, small values of e (such as 3) have been shown to be less secure in some settings.[4]
        Determine d as:
        d = e^-1 (mod φ(n))

        i.e., d is the multiplicative inverse of e mod φ(n).
        This is more clearly stated as solve for d given (de) mod φ(n) = 1
        This is often computed using the extended Euclidean algorithm.
        d is kept as the private key exponent.
         * */
        Random r = new Random();
        BigInteger P = new BigInteger();
        BigInteger Q = new BigInteger();
        int bitLenP = moduleBitLen >> 1;
        int bitLenQ = bitLenP + (moduleBitLen & 1);
        while (true)
        {
            while (true)
            {
                P.genRandomBits(bitLenP, r);
                P.data[0] |= 0x01;		// make it odd
                if (P.isProbablePrime())
                    break;
            }
            while (true)
            {
                Q.genRandomBits(bitLenQ, r);
                Q.data[0] |= 0x01;		// make it odd
                if (Q.isProbablePrime())
                    break;
            }
            var N = P * Q;
            var PHI = (P - 1) * (Q - 1);
            var E = PHI.genCoPrime(pubKeyBitLen,r);
            BigInteger D = E.modInverse(PHI);
            publicExponent = E;
            privateExponent = D;
            module = N;
            return;
        }
    }
开发者ID:sagar1589,项目名称:Delta.Cryptography,代码行数:51,代码来源:BigInteger.cs


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