org.bouncycastle.crypto.generators.DHParametersGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bcprov-lts8on Show documentation
Show all versions of bcprov-lts8on Show documentation
The Long Term Stable (LTS) Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms. This jar contains the JCA/JCE provider and low-level API for the BC LTS version 2.73.7 for Java 8 and later.
package org.bouncycastle.crypto.generators;
import java.math.BigInteger;
import java.security.SecureRandom;
import org.bouncycastle.crypto.params.DHParameters;
public class DHParametersGenerator
{
private int size;
private int certainty;
private SecureRandom random;
private static final BigInteger TWO = BigInteger.valueOf(2);
/**
* Initialise the parameters generator.
*
* @param size bit length for the prime p
* @param certainty level of certainty for the prime number tests
* @param random a source of randomness
*/
public void init(
int size,
int certainty,
SecureRandom random)
{
this.size = size;
this.certainty = certainty;
this.random = random;
}
/**
* which generates the p and g values from the given parameters,
* returning the DHParameters object.
*
* Note: can take a while...
* @return a generated Diffie-Hellman parameters object.
*/
public DHParameters generateParameters()
{
//
// find a safe prime p where p = 2*q + 1, where p and q are prime.
//
BigInteger[] safePrimes = DHParametersHelper.generateSafePrimes(size, certainty, random);
BigInteger p = safePrimes[0];
BigInteger q = safePrimes[1];
BigInteger g = DHParametersHelper.selectGenerator(p, q, random);
return new DHParameters(p, g, q, TWO, null);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy