Ejemplo n.º 1
0
 public void GetTwoProbablePrimesParallelTest()
 {
     BigInteger[] res = PrimeGenerator.GetTwoProbablePrimesParallel(64);
     Assert.AreEqual(2, res.Length);
     Assert.IsFalse(res[0] == res[1]);
     Assert.Pass(string.Join(", ", res));
 }
Ejemplo n.º 2
0
        public void GenerateNewKeypair(CancellationToken ct = default)
        {
            BigInteger[] primes = PrimeGenerator.GetTwoProbablePrimesParallel(512, ct: ct);
            if (primes == null || primes.Length <= 0)
            {
                throw new OperationCanceledException();
            }
            BigInteger p    = primes[0];
            BigInteger q    = primes[1];
            BigInteger phiN = (p - 1) * (q - 1);
            int        e    = -1;

            foreach (int candidate in Exponents)
            {
                if (BigInteger.GreatestCommonDivisor(phiN, candidate) == 1)
                {
                    e = candidate;
                    break;
                }
            }

            if (e == -1)
            {
                throw new CryptographicException("Co-prime exponent was not found.");
            }
            var k = 1;

            while (true)
            {
                if ((1 + k * phiN) % e == 0)
                {
                    break;
                }
                k++;
            }

            BigInteger d = (1 + k * phiN) / e;

            PrivateExponent = d;
            Modulus         = p * q;
            Exponent        = e;
        }