Exemple #1
0
        private void Initialize(int BitLength)
        {
            // get primes P and Q
            _P = BigInteger.ProbablePrime(BitLength, _secRand);
            _Q = BigInteger.ProbablePrime(BitLength, _secRand);
            // N = P * Q
            _N = _P.Multiply(_Q);
            // get X
            _X = BigInteger.ValueOf(_secRand.NextInt64());

            // find random X mod N
            for (int i = 0; i < 10 || _X.CompareTo(BigInteger.One) < 1; i++)
            {
                _X = _X.Multiply(BigInteger.ValueOf(_secRand.NextInt64())).Mod(_N);
            }

            // X = (X pow 2) mod N
            _X = _X.Multiply(_X).Mod(_N);
            // store X
            _X0 = _X;
        }
Exemple #2
0
        private void Initialize(int BitLength)
        {
            // get primes P and Q
            m_P = BigInteger.ProbablePrime(BitLength, m_secRand);
            m_Q = BigInteger.ProbablePrime(BitLength, m_secRand);
            // N = P * Q
            m_N = m_P.Multiply(m_Q);
            // get X
            m_X = BigInteger.ValueOf(m_secRand.NextInt64());

            // find random X mod N
            for (int i = 0; i < 10 || m_X.CompareTo(BigInteger.One) < 1; i++)
            {
                m_X = m_X.Multiply(BigInteger.ValueOf(m_secRand.NextInt64())).Mod(m_N);
            }

            // X = (X pow 2) mod N
            m_X = m_X.Multiply(m_X).Mod(m_N);
            // store X
            m_X0 = m_X;
        }
Exemple #3
0
        private void SecRandTest()
        {
            using (SecureRandom rnd = new SecureRandom())
            {
                double x1 = 0.0;
                for (int i = 0; i < 1000; i++)
                {
                    x1 = rnd.NextDouble();
                    if (x1 > 1.0)
                        throw new Exception("SecureRandom: NextDouble returned a value outside of the expected range.");
                }

                short x2 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x2 = rnd.NextInt16(1, 6);
                    if (x2 > 6)
                        throw new Exception("SecureRandom: NextInt16 returned a value outside of the expected range.");
                    if (x2 < 1)
                        throw new Exception("SecureRandom: NextInt16 returned a value outside of the expected range.");
                }

                ushort x3 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x3 = rnd.NextUInt16(1, 52);
                    if (x3 > 52)
                        throw new Exception("SecureRandom: NextUInt16 returned a value outside of the expected range.");
                    if (x3 < 1)
                        throw new Exception("SecureRandom: NextUInt16 returned a value outside of the expected range.");
                }

                int x4 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x4 = rnd.NextInt32(3371, 16777216);
                    if (x4 > 16777216)
                        throw new Exception("SecureRandom: NextInt32 returned a value outside of the expected range.");
                    if (x4 < 3371)
                        throw new Exception("SecureRandom: NextInt32 returned a value outside of the expected range.");
                }

                uint x5 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x5 = rnd.NextUInt32(77721, 777216);
                    if (x5 > 777216)
                        throw new Exception("SecureRandom: NextUInt32 returned a value outside of the expected range.");
                    if (x5 < 77721)
                        throw new Exception("SecureRandom: NextUInt32 returned a value outside of the expected range.");
                }

                long x6 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x6 = rnd.NextInt64(2814749767, 281474976710653);
                    if (x6 > 281474976710656)
                        throw new Exception("SecureRandom: NextInt64 returned a value outside of the expected range.");
                    if (x6 < 2814749767)
                        throw new Exception("SecureRandom: NextInt64 returned a value outside of the expected range.");
                }

                ulong x7 = 0;
                for (int i = 0; i < 1000; i++)
                {
                    x7 = rnd.NextUInt64(5759403792, 72057594037927934);
                    if (x7 > 72057594037927936)
                        throw new Exception("SecureRandom: NextUInt64 returned a value outside of the expected range.");
                    if (x7 < 5759403792)
                        throw new Exception("SecureRandom: NextUInt64 returned a value outside of the expected range.");
                }
            }
        }