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 basic Diffie-Helman key pair generator.
15: *
16: * This generates keys consistent for use with the basic algorithm for
17: * Diffie-Helman.
18: */
19: public class DHBasicKeyPairGenerator implements
20: AsymmetricCipherKeyPairGenerator {
21: private DHKeyGeneratorHelper helper = DHKeyGeneratorHelper.INSTANCE;
22: private DHKeyGenerationParameters param;
23:
24: public void init(KeyGenerationParameters param) {
25: this .param = (DHKeyGenerationParameters) param;
26: }
27:
28: public AsymmetricCipherKeyPair generateKeyPair() {
29: BigInteger p, x, y;
30: DHParameters dhParams = param.getParameters();
31:
32: p = dhParams.getP();
33: x = helper.calculatePrivate(p, param.getRandom(), dhParams
34: .getL());
35: y = helper.calculatePublic(p, dhParams.getG(), x);
36:
37: return new AsymmetricCipherKeyPair(new DHPublicKeyParameters(y,
38: dhParams), new DHPrivateKeyParameters(x, dhParams));
39: }
40: }
|