01: package org.bouncycastle.crypto.generators;
02:
03: import java.math.BigInteger;
04:
05: import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
06: import org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator;
07: import org.bouncycastle.crypto.KeyGenerationParameters;
08: import org.bouncycastle.crypto.params.ElGamalKeyGenerationParameters;
09: import org.bouncycastle.crypto.params.ElGamalParameters;
10: import org.bouncycastle.crypto.params.ElGamalPrivateKeyParameters;
11: import org.bouncycastle.crypto.params.ElGamalPublicKeyParameters;
12:
13: /**
14: * a ElGamal key pair generator.
15: * <p>
16: * This generates keys consistent for use with ElGamal as described in
17: * page 164 of "Handbook of Applied Cryptography".
18: */
19: public class ElGamalKeyPairGenerator implements
20: AsymmetricCipherKeyPairGenerator {
21: private DHKeyGeneratorHelper helper = DHKeyGeneratorHelper.INSTANCE;
22:
23: private ElGamalKeyGenerationParameters param;
24:
25: public void init(KeyGenerationParameters param) {
26: this .param = (ElGamalKeyGenerationParameters) param;
27: }
28:
29: public AsymmetricCipherKeyPair generateKeyPair() {
30: BigInteger p, x, y;
31: ElGamalParameters elParams = param.getParameters();
32:
33: p = elParams.getP();
34:
35: x = helper.calculatePrivate(p, param.getRandom(), elParams
36: .getL());
37: y = helper.calculatePublic(p, elParams.getG(), x);
38:
39: return new AsymmetricCipherKeyPair(
40: new ElGamalPublicKeyParameters(y, elParams),
41: new ElGamalPrivateKeyParameters(x, elParams));
42: }
43: }
|