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

Beispiel #1
0
        internal void Setup()
        {
            _b = new BigInteger();
            _b.genRandomBits(256, new Random((int)DateTime.Now.Ticks));

            _B = (_k * _v) + (_g.modPow(_b, _N));
            while (_B % _N == 0)
            {
                _b.genRandomBits(256, new Random((int)DateTime.Now.Ticks));
                _B = (_k * _v) + (_g.modPow(_b, _N));
            }
            _Bstr = _B.ToString(16);
        }
All Usage Examples Of BigInteger::genRandomBits