01: package org.bouncycastle.crypto.generators;
02:
03: import org.bouncycastle.crypto.params.ElGamalParameters;
04:
05: import java.math.BigInteger;
06: import java.security.SecureRandom;
07:
08: public class ElGamalParametersGenerator {
09: private int size;
10: private int certainty;
11: private SecureRandom random;
12:
13: public void init(int size, int certainty, SecureRandom random) {
14: this .size = size;
15: this .certainty = certainty;
16: this .random = random;
17: }
18:
19: /**
20: * which generates the p and g values from the given parameters,
21: * returning the ElGamalParameters object.
22: * <p>
23: * Note: can take a while...
24: */
25: public ElGamalParameters generateParameters() {
26: //
27: // find a safe prime p where p = 2*q + 1, where p and q are prime.
28: //
29: BigInteger[] safePrimes = DHParametersHelper
30: .generateSafePrimes(size, certainty, random);
31:
32: BigInteger p = safePrimes[0];
33: BigInteger q = safePrimes[1];
34: BigInteger g = DHParametersHelper.selectGenerator(p, q, random);
35:
36: return new ElGamalParameters(p, g);
37: }
38: }
|