01: package org.bouncycastle.crypto.generators;
02:
03: import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
04: import org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator;
05: import org.bouncycastle.crypto.KeyGenerationParameters;
06: import org.bouncycastle.crypto.params.DHKeyGenerationParameters;
07: import org.bouncycastle.crypto.params.DHParameters;
08: import org.bouncycastle.crypto.params.DHPrivateKeyParameters;
09: import org.bouncycastle.crypto.params.DHPublicKeyParameters;
10:
11: import java.math.BigInteger;
12:
13: /**
14: * a Diffie-Helman key pair generator.
15: *
16: * This generates keys consistent for use in the MTI/A0 key agreement protocol
17: * as described in "Handbook of Applied Cryptography", Pages 516-519.
18: */
19: public class DHKeyPairGenerator implements
20: AsymmetricCipherKeyPairGenerator {
21: private DHKeyGeneratorHelper helper = DHKeyGeneratorHelper.INSTANCE;
22:
23: private DHKeyGenerationParameters param;
24:
25: public void init(KeyGenerationParameters param) {
26: this .param = (DHKeyGenerationParameters) param;
27: }
28:
29: public AsymmetricCipherKeyPair generateKeyPair() {
30: BigInteger p, x, y;
31: DHParameters dhParams = param.getParameters();
32:
33: p = dhParams.getP();
34: x = helper.calculatePrivate(p, param.getRandom(), dhParams
35: .getL());
36: y = helper.calculatePublic(p, dhParams.getG(), x);
37:
38: return new AsymmetricCipherKeyPair(new DHPublicKeyParameters(y,
39: dhParams), new DHPrivateKeyParameters(x, dhParams));
40: }
41: }
|