~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/crypto/generators/DHBasicKeyPairGenerator.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.bouncycastle.crypto.generators;
 
2
 
 
3
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
 
4
import org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator;
 
5
import org.bouncycastle.crypto.KeyGenerationParameters;
 
6
import org.bouncycastle.crypto.params.DHKeyGenerationParameters;
 
7
import org.bouncycastle.crypto.params.DHParameters;
 
8
import org.bouncycastle.crypto.params.DHPrivateKeyParameters;
 
9
import org.bouncycastle.crypto.params.DHPublicKeyParameters;
 
10
 
 
11
import java.math.BigInteger;
 
12
 
 
13
/**
 
14
 * a basic Diffie-Hellman key pair generator.
 
15
 *
 
16
 * This generates keys consistent for use with the basic algorithm for
 
17
 * Diffie-Hellman.
 
18
 */
 
19
public class DHBasicKeyPairGenerator
 
20
    implements AsymmetricCipherKeyPairGenerator
 
21
{
 
22
    private DHKeyGenerationParameters param;
 
23
 
 
24
    public void init(
 
25
        KeyGenerationParameters param)
 
26
    {
 
27
        this.param = (DHKeyGenerationParameters)param;
 
28
    }
 
29
 
 
30
    public AsymmetricCipherKeyPair generateKeyPair()
 
31
    {
 
32
        DHKeyGeneratorHelper helper = DHKeyGeneratorHelper.INSTANCE;
 
33
        DHParameters dhp = param.getParameters();
 
34
 
 
35
        BigInteger x = helper.calculatePrivate(dhp, param.getRandom()); 
 
36
        BigInteger y = helper.calculatePublic(dhp, x);
 
37
 
 
38
        return new AsymmetricCipherKeyPair(
 
39
            new DHPublicKeyParameters(y, dhp),
 
40
            new DHPrivateKeyParameters(x, dhp));
 
41
    }
 
42
}