public static void GenerateKeyPair(out RSAKey PublicKey, out RSAKey PrivateKey) { // Generate a large prime number for P BigInteger P = GetLargeRandomPrime(); // generate a large prime number for Q BigInteger Q = GetLargeRandomPrime(); BigInteger N = P * Q; BigInteger Phi = (P - 1) * (Q - 1); BigInteger e; e = 65537; while (GCD(e, Phi) != 1) { e = GetFirstPrime(e); } BigInteger d = ModInverse(e, Phi); PublicKey.N = N; PublicKey.Key = e; PrivateKey.N = N; PrivateKey.Key = d; Console.WriteLine("Public Key is: \n" + PublicKey.ToString("X")); Console.WriteLine("Private Key is: \n" + PrivateKey.ToString("X")); }
public static void Bonus2() { int start = Environment.TickCount; RSAKeyGenerator keyGenerator = new RSAKeyGenerator(); RSAKey key = keyGenerator.GenerateRSAKeys(); int end = Environment.TickCount; float seconds = (end - start) / 1000.0f; Console.WriteLine(key.ToString()); Console.WriteLine("TOTAL TIME = " + (end - start).ToString().PadRight(6) + " ms = " + (seconds).ToString().PadRight(5) + " sec"); }