BigInteger.genRandomBits C# (CSharp) Method

genRandomBits() public method

public genRandomBits ( int bits, Random rand ) : void
bits int
rand Random
return void
        public void genRandomBits(int bits, Random rand)
        {
	        int dwords = bits >> 5;
	        int remBits = bits & 0x1F;

	        if(remBits != 0)
		        dwords++;

	        if(dwords > maxLength)
		        throw (new ArithmeticException("Number of required bits > maxLength."));

	        for(int i = 0; i < dwords; i++)
		        data[i] = (uint)(rand.NextDouble() * 0x100000000);

	        for(int i = dwords; i < maxLength; i++)
		        data[i] = 0;

	        if(remBits != 0)
	        {
		        uint mask = (uint)(0x01 << (remBits-1));
		        data[dwords-1] |= mask;

		        mask = (uint)(0xFFFFFFFF >> (32 - remBits));
		        data[dwords-1] &= mask;
	        }
	        else
		        data[dwords-1] |= 0x80000000;

	        dataLength = dwords;

	        if(dataLength == 0)
	                dataLength = 1;
        }

Usage Example

示例#1
1
        //формирование цифровой подписи.
        public string genDS(byte[] h, BigInteger d)
        {
            BigInteger a = new BigInteger(h);
            BigInteger e = a % n;
            if (e == 0)
                e = 1;
            BigInteger k = new BigInteger();
            CECPoint C=new CECPoint();
            BigInteger r=new BigInteger();
            BigInteger s = new BigInteger();
            do
            {
                do
                {
                    k.genRandomBits(n.bitCount(), new Random());
                } 
                while ((k < 0) || (k > n));

                C = CECPoint.multiply(G, k);
                r = C.x % n;
                s = ((r * d) + (k * e)) % n;
            } 
            while ((r == 0)||(s==0));

            string Rvector = padding(r.ToHexString(), n.bitCount() / 4);
            string Svector = padding(s.ToHexString(), n.bitCount() / 4);
            return Rvector + Svector;
        }
All Usage Examples Of BigInteger::genRandomBits