// initializes the private variables (throws CryptographicException) private void Initialize(BigInteger p, BigInteger g, BigInteger x, int secretLen, bool checkInput) { if (!p.isProbablePrime() || g <= 0 || g >= p || (x != null && (x <= 0 || x > p - 2))) throw new CryptographicException(); // default is to generate a number as large as the prime this // is usually overkill, but it's the most secure thing we can // do if the user doesn't specify a desired secret length ... if (secretLen == 0) secretLen = p.bitCount(); m_P = p; m_G = g; if (x == null) { BigInteger pm1 = m_P - 1; for(m_X = BigInteger.genRandom(secretLen); m_X >= pm1 || m_X == 0; m_X = BigInteger.genRandom(secretLen)) {} } else { m_X = x; } }
private void ExpectPrime (BigInteger bi) { Assert.AreEqual (true, bi.isProbablePrime ()); }
private void Initialize(BigInteger p, BigInteger g, BigInteger x) { if (!p.isProbablePrime() || g <= 0 || g >= p) throw new CryptographicException("Inputs p or g are not as expected. P probably isn't a prime or G is less than zero or more than P."); if(x != null) { _x = x; } else { var pMinus1 = p - 1; var secretLen = p.bitCount(); for (_x = BigInteger.genRandom(secretLen); _x >= pMinus1 || _x == 0; _x = BigInteger.genRandom(secretLen)) { } } _p = p; _g = g; }
private void ExpectComposite (BigInteger bi) { Assert.AreEqual (false, bi.isProbablePrime ()); }