public void generateNewKeys(string keyName, string passphrase, int moduleSize) { BigInteger testValue = 0; do { p = PrimeNumberGenerator.GeneratePrimeNumber(moduleSize / 2); q = PrimeNumberGenerator.GeneratePrimeNumber(moduleSize / 2); n = BigInteger.Multiply(p, q); phi = BigInteger.Multiply(BigInteger.Subtract(p, 1), BigInteger.Subtract(q, 1)); e = PrimeNumberGenerator.GenerateMutuallyPrimeNumber(phi, moduleSize / 2); d = PrimeNumberGenerator.MultiplicativelyInverseNumber(phi, e); if (BigInteger.Compare(e, 0) < 0) { continue; } if (BigInteger.Compare(d, 0) < 0) { continue; } publicKey = new RSAPublicKey(e, n); privateKey = new RSAPrivateKey(d, n); privateKey.EncryptByPassphrase(passphrase); testValue = privateKey.Decrypt(publicKey.Encrypt(TEST_VALUE)); } while (!BigInteger.Equals(testValue, TEST_VALUE)); _keyName = keyName; }