1
package org.bouncycastle.crypto.generators;
3
import java.math.BigInteger;
4
import java.security.SecureRandom;
6
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
7
import org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator;
8
import org.bouncycastle.crypto.KeyGenerationParameters;
9
import org.bouncycastle.crypto.params.ECDomainParameters;
10
import org.bouncycastle.crypto.params.ECKeyGenerationParameters;
11
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
12
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
13
import org.bouncycastle.math.ec.ECConstants;
14
import org.bouncycastle.math.ec.ECPoint;
16
public class ECKeyPairGenerator
17
implements AsymmetricCipherKeyPairGenerator, ECConstants
19
ECDomainParameters params;
23
KeyGenerationParameters param)
25
ECKeyGenerationParameters ecP = (ECKeyGenerationParameters)param;
27
this.random = ecP.getRandom();
28
this.params = ecP.getDomainParameters();
32
* Given the domain parameters this routine generates an EC key
33
* pair in accordance with X9.62 section 5.2.1 pages 26, 27.
35
public AsymmetricCipherKeyPair generateKeyPair()
37
BigInteger n = params.getN();
38
int nBitLength = n.bitLength();
43
d = new BigInteger(nBitLength, random);
45
while (d.equals(ZERO) || (d.compareTo(n) >= 0));
47
ECPoint Q = params.getG().multiply(d);
49
return new AsymmetricCipherKeyPair(
50
new ECPublicKeyParameters(Q, params),
51
new ECPrivateKeyParameters(d, params));