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


C# BigInteger.GenRandomBits方法代码示例

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


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

示例1: SqrtTest

        //***********************************************************************
        // Tests the correct implementation of sqrt() method.
        //***********************************************************************

        public static void SqrtTest(int rounds)
        {
                Random rand = new Random();
	        for(int count = 0; count < rounds; count++)
	        {
	                // generate data of random length
		        int t1 = 0;
		        while(t1 == 0)
			        t1 = (int)(rand.NextDouble() * 1024);

                        Console.Write("Round = " + count);

                        BigInteger a = new BigInteger();
                        a.GenRandomBits(t1, rand);

                        BigInteger b = a.Sqrt();
                        BigInteger c = (b+1)*(b+1);

                        // check that b is the largest integer such that b*b <= a
                        if(c <= a)
	        	{
		        	Console.WriteLine("\nError at round " + count);
                                Console.WriteLine(a + "\n");
			        return;
		        }
		        Console.WriteLine(" <PASSED>.");
	        }
        }
开发者ID:UlyssesWu,项目名称:Encryption,代码行数:32,代码来源:BigInteger.cs

示例2: Auth_AuthChallengeResult

        //internal class AuthLogonChallenge_Result
        //{
        //    public byte m_iCommand = (byte)RealmListOpCode.CMSG_AUTH_CHALLENGE_RESULT; // 0x00 CMD_AUTH_LOGON_CHALLENGE
        //    public byte m_iError = 0;		           // 0 - ok
        //    public byte m_iUnk = 0;		           // 0x00
        //    public byte[] m_iB = new byte[32];
        //    public byte m_iGLen = 1;		           // 0x01
        //    public byte[] m_iG = new byte[1];
        //    public byte m_iNLen = 32;		           // 0x20
        //    public byte[] m_iN = new byte[32];
        //    public byte[] m_iS = new byte[32];
        //    public byte[] m_iUnk2 = new byte[16];
        //    public byte m_iUnk3 = 0;
        //}

        /// <summary>
        /// 等于 AuthLogonChallenge_Result 结构
        /// </summary>
        public Auth_AuthChallengeResult( SecureRemotePassword srp )
            : base( (long)AuthOpCode.CMSG_AUTH_CHALLENGE_RESULT, 0 )
        {
            WriterStream.Write( (byte)AuthOpCode.CMSG_AUTH_CHALLENGE_RESULT );
            WriterStream.Write( (byte)LogineErrorInfo.LOGIN_SUCCESS );
            //////////////////////////////////////////////////////////////////////////

            WriterStream.Write( (byte)0 );

            WriterStream.Write( srp.PublicEphemeralValueB.GetBytes( 32 ), 0, 32 );

            WriterStream.Write( (byte)1 );
            WriterStream.Write( srp.Generator.GetBytes( 1 ), 0, 1 );

            WriterStream.Write( (byte)32 );
            WriterStream.Write( srp.Modulus.GetBytes( 32 ), 0, 32 );

            WriterStream.Write( srp.Salt.GetBytes( 32 ), 0, 32 );

            BigInteger unknown = new BigInteger();
            unknown.GenRandomBits( 128 /* 16 * 8 */ , new Random( 10 ) );/* 随机数 16字节 */
            WriterStream.Write( unknown.GetBytes( 16 ), 0, 16 );

            WriterStream.Write( (byte)0 );
        }
开发者ID:andyhebear,项目名称:HappyQ-WowServer,代码行数:43,代码来源:Packets.cs

示例3: GenCoPrime

        //***********************************************************************
        // Generates a random number with the specified number of bits such
        // that gcd(number, this) = 1
        //***********************************************************************

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

	        while(!done)
	        {
	                result.GenRandomBits(bits, rand);
	                //Console.WriteLine(result.ToString(16));

		        // gcd test
		        BigInteger g = result.Gcd(this);
			if(g.dataLength == 1 && g.data[0] == 1)
                                done = true;
	        }

	        return result;
        }
开发者ID:UlyssesWu,项目名称:Encryption,代码行数:23,代码来源:BigInteger.cs

示例4: 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:UlyssesWu,项目名称:Encryption,代码行数:19,代码来源:BigInteger.cs

示例5: SolovayStrassenTest

        //***********************************************************************
        // Probabilistic prime test based on Solovay-Strassen (Euler Criterion)
        //
        // p is probably prime if for any a < p (a is not multiple of p),
        // a^((p-1)/2) mod p = J(a, p)
        //
        // where J is the Jacobi symbol.
        //
        // Otherwise, p is composite.
        //
        // Returns
        // -------
        // True if "this" is a Euler pseudoprime to randomly chosen
        // bases.  The number of chosen bases is given by the "confidence"
        // parameter.
        //
        // False if "this" is definitely NOT prime.
        //
        //***********************************************************************

        public bool SolovayStrassenTest(int confidence)
        {
                BigInteger thisVal;
                if((this.data[MaxLength-1] & 0x80000000) != 0)        // negative
                        thisVal = -this;
                else
                        thisVal = this;

                if(thisVal.dataLength == 1)
                {
                        // test small numbers
                        if(thisVal.data[0] == 0 || thisVal.data[0] == 1)
                                return false;
                        else if(thisVal.data[0] == 2 || thisVal.data[0] == 3)
                                return true;
                }

                if((thisVal.data[0] & 0x1) == 0)     // even numbers
                        return false;


	        int bits = thisVal.bitCount();
	        BigInteger a = new BigInteger();
	        BigInteger p_sub1 = thisVal - 1;
	        BigInteger p_sub1_shift = p_sub1 >> 1;

	        Random rand = new Random();

	        for(int round = 0; round < confidence; round++)
	        {
		        bool done = false;

		        while(!done)		// generate a < n
		        {
			        int testBits = 0;

			        // make sure "a" has at least 2 bits
			        while(testBits < 2)
				        testBits = (int)(rand.NextDouble() * bits);

			        a.GenRandomBits(testBits, rand);

			        int byteLen = a.dataLength;

                                // make sure "a" is not 0
			        if(byteLen > 1 || (byteLen == 1 && a.data[0] != 1))
				        done = true;
		        }

                        // check whether a factor exists (fix for version 1.03)
		        BigInteger gcdTest = a.Gcd(thisVal);
                        if(gcdTest.dataLength == 1 && gcdTest.data[0] != 1)
                                return false;

		        // calculate a^((p-1)/2) mod p

		        BigInteger expResult = a.ModPow(p_sub1_shift, thisVal);
		        if(expResult == p_sub1)
		                expResult = -1;

                        // calculate Jacobi symbol
                        BigInteger jacob = Jacobi(a, thisVal);

                        //Console.WriteLine("a = " + a.ToString(10) + " b = " + thisVal.ToString(10));
                        //Console.WriteLine("expResult = " + expResult.ToString(10) + " Jacob = " + jacob.ToString(10));

                        // if they are different then it is not prime
                        if(expResult != jacob)
			        return false;
	        }

	        return true;
        }
开发者ID:UlyssesWu,项目名称:Encryption,代码行数:93,代码来源:BigInteger.cs

示例6: RabinMillerTest

        //***********************************************************************
        // Probabilistic prime test based on Rabin-Miller's
        //
        // for any p > 0 with p - 1 = 2^s * t
        //
        // p is probably prime (strong pseudoprime) if for any a < p,
        // 1) a^t mod p = 1 or
        // 2) a^((2^j)*t) mod p = p-1 for some 0 <= j <= s-1
        //
        // Otherwise, p is composite.
        //
        // Returns
        // -------
        // True if "this" is a strong pseudoprime to randomly chosen
        // bases.  The number of chosen bases is given by the "confidence"
        // parameter.
        //
        // False if "this" is definitely NOT prime.
        //
        //***********************************************************************

        public bool RabinMillerTest(int confidence)
        {
                BigInteger thisVal;
                if((this.data[MaxLength-1] & 0x80000000) != 0)        // negative
                        thisVal = -this;
                else
                        thisVal = this;

                if(thisVal.dataLength == 1)
                {
                        // test small numbers
                        if(thisVal.data[0] == 0 || thisVal.data[0] == 1)
                                return false;
                        else if(thisVal.data[0] == 2 || thisVal.data[0] == 3)
                                return true;
                }

                if((thisVal.data[0] & 0x1) == 0)     // even numbers
                        return false;


                // calculate values of s and t
                BigInteger p_sub1 = thisVal - (new BigInteger(1));
                int s = 0;

                for(int index = 0; index < p_sub1.dataLength; index++)
                {
                        uint mask = 0x01;

                        for(int i = 0; i < 32; i++)
                        {
                                if((p_sub1.data[index] & mask) != 0)
                                {
                                        index = p_sub1.dataLength;      // to break the outer loop
                                        break;
                                }
                                mask <<= 1;
                                s++;
                        }
                }

                BigInteger t = p_sub1 >> s;

	        int bits = thisVal.bitCount();
	        BigInteger a = new BigInteger();
	        Random rand = new Random();

	        for(int round = 0; round < confidence; round++)
	        {
		        bool done = false;

		        while(!done)		// generate a < n
		        {
			        int testBits = 0;

			        // make sure "a" has at least 2 bits
			        while(testBits < 2)
				        testBits = (int)(rand.NextDouble() * bits);

			        a.GenRandomBits(testBits, rand);

			        int byteLen = a.dataLength;

                                // make sure "a" is not 0
			        if(byteLen > 1 || (byteLen == 1 && a.data[0] != 1))
				        done = true;
		        }

                        // check whether a factor exists (fix for version 1.03)
		        BigInteger gcdTest = a.Gcd(thisVal);
                        if(gcdTest.dataLength == 1 && gcdTest.data[0] != 1)
                                return false;

                        BigInteger b = a.ModPow(t, thisVal);

                        /*
                        Console.WriteLine("a = " + a.ToString(10));
                        Console.WriteLine("b = " + b.ToString(10));
                        Console.WriteLine("t = " + t.ToString(10));
//.........这里部分代码省略.........
开发者ID:UlyssesWu,项目名称:Encryption,代码行数:101,代码来源:BigInteger.cs

示例7: FermatLittleTest

        //***********************************************************************
        // Probabilistic prime test based on Fermat's little theorem
        //
        // for any a < p (p does not divide a) if
        //      a^(p-1) mod p != 1 then p is not prime.
        //
        // Otherwise, p is probably prime (pseudoprime to the chosen base).
        //
        // Returns
        // -------
        // True if "this" is a pseudoprime to randomly chosen
        // bases.  The number of chosen bases is given by the "confidence"
        // parameter.
        //
        // False if "this" is definitely NOT prime.
        //
        // Note - this method is fast but fails for Carmichael numbers except
        // when the randomly chosen base is a factor of the number.
        //
        //***********************************************************************

        public bool FermatLittleTest(int confidence)
        {
                BigInteger thisVal;
                if((this.data[MaxLength-1] & 0x80000000) != 0)        // negative
                        thisVal = -this;
                else
                        thisVal = this;

                if(thisVal.dataLength == 1)
                {
                        // test small numbers
                        if(thisVal.data[0] == 0 || thisVal.data[0] == 1)
                                return false;
                        else if(thisVal.data[0] == 2 || thisVal.data[0] == 3)
                                return true;
                }

                if((thisVal.data[0] & 0x1) == 0)     // even numbers
                        return false;

	        int bits = thisVal.bitCount();
	        BigInteger a = new BigInteger();
	        BigInteger p_sub1 = thisVal - (new BigInteger(1));
	        Random rand = new Random();

	        for(int round = 0; round < confidence; round++)
	        {
		        bool done = false;

		        while(!done)		// generate a < n
		        {
			        int testBits = 0;

			        // make sure "a" has at least 2 bits
			        while(testBits < 2)
				        testBits = (int)(rand.NextDouble() * bits);

			        a.GenRandomBits(testBits, rand);

			        int byteLen = a.dataLength;

                                // make sure "a" is not 0
			        if(byteLen > 1 || (byteLen == 1 && a.data[0] != 1))
                                        done = true;
		        }

                        // check whether a factor exists (fix for version 1.03)
		        BigInteger gcdTest = a.Gcd(thisVal);
                        if(gcdTest.dataLength == 1 && gcdTest.data[0] != 1)
                                return false;

		        // calculate a^(p-1) mod p
		        BigInteger expResult = a.ModPow(p_sub1, thisVal);

		        int resultLen = expResult.dataLength;

                        // is NOT prime is a^(p-1) mod p != 1

		        if(resultLen > 1 || (resultLen == 1 && expResult.data[0] != 1))
		        {
		                //Console.WriteLine("a = " + a.ToString());
			        return false;
                        }
	        }

	        return true;
        }
开发者ID:UlyssesWu,项目名称:Encryption,代码行数:88,代码来源:BigInteger.cs


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