} // End Function GenerateElGamalKeyPair public static Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair GenerateDHKeyPair( int length , Org.BouncyCastle.Security.SecureRandom secureRandom ) { // Org.BouncyCastle.Math.BigInteger p = new BigInteger("123", 10); // Org.BouncyCastle.Math.BigInteger g = new BigInteger("456", 10); // Org.BouncyCastle.Crypto.Parameters.DHParameters dhParams = // new Org.BouncyCastle.Crypto.Parameters.DHParameters(p, g); Org.BouncyCastle.Crypto.Generators.DHParametersGenerator pg = new Org.BouncyCastle.Crypto.Generators.DHParametersGenerator(); pg.Init(length, 100, secureRandom); Org.BouncyCastle.Crypto.Parameters.DHParameters dhp = pg.GenerateParameters(); Org.BouncyCastle.Crypto.Parameters.DHKeyGenerationParameters pars = new Org.BouncyCastle.Crypto.Parameters.DHKeyGenerationParameters(secureRandom, dhp); Org.BouncyCastle.Crypto.Generators.DHKeyPairGenerator keyGenerator = new Org.BouncyCastle.Crypto.Generators.DHKeyPairGenerator(); keyGenerator.Init(pars); return(keyGenerator.GenerateKeyPair()); } // End Function GenerateDHKeyPair
// https://stackoverflow.com/questions/33813108/bouncycastle-diffie-hellman public static Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair GenerateDiffieHellmanKeyPair(Org.BouncyCastle.Security.SecureRandom random , int keystrength ) { const int DefaultPrimeProbability = 30; Org.BouncyCastle.Crypto.IAsymmetricCipherKeyPairGenerator keypairGen = new Org.BouncyCastle.Crypto.Generators.DHKeyPairGenerator(); Org.BouncyCastle.Crypto.Generators.DHParametersGenerator pGen = new Org.BouncyCastle.Crypto.Generators.DHParametersGenerator(); pGen.Init(keystrength, DefaultPrimeProbability, random); Org.BouncyCastle.Crypto.Parameters.DHParameters parameters = pGen.GenerateParameters(); Org.BouncyCastle.Crypto.KeyGenerationParameters genParam = new Org.BouncyCastle.Crypto.Parameters.DHKeyGenerationParameters(new Org.BouncyCastle.Security.SecureRandom(), parameters); keypairGen.Init(genParam); Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair kp = keypairGen.GenerateKeyPair(); return(kp); }