Java Code Examples for org.bouncycastle.crypto.generators.ECKeyPairGenerator#generateKeyPair()

The following examples show how to use org.bouncycastle.crypto.generators.ECKeyPairGenerator#generateKeyPair() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransactionUtil.java    From chain33-sdk-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * @description 创建私钥和公钥
 * 
 * @return 私钥
 */
public static byte[] generatorPrivateKey() {
	int length = 0;
	byte[] privateKey;
	do {
		ECKeyPairGenerator gen = new ECKeyPairGenerator();
		SecureRandom secureRandom = new SecureRandom();
		X9ECParameters secnamecurves = SECNamedCurves.getByName("secp256k1");
		ECDomainParameters ecParams = new ECDomainParameters(secnamecurves.getCurve(), secnamecurves.getG(),
				secnamecurves.getN(), secnamecurves.getH());
		ECKeyGenerationParameters keyGenParam = new ECKeyGenerationParameters(ecParams, secureRandom);
		gen.init(keyGenParam);
		AsymmetricCipherKeyPair kp = gen.generateKeyPair();
		ECPrivateKeyParameters privatekey = (ECPrivateKeyParameters) kp.getPrivate();
		privateKey = privatekey.getD().toByteArray();
		length = privatekey.getD().toByteArray().length;
	} while (length != 32);
	return privateKey;
}
 
Example 2
Source File: LocalIdentity.java    From ts3j with Apache License 2.0 6 votes vote down vote up
/**
 * Generates a new identity with a given security level target.
 * @param securityLevel security level to generate for (may take time)
 * @return local identity with given security level
 * @throws GeneralSecurityException
 */
public static LocalIdentity generateNew(int securityLevel) throws GeneralSecurityException {
    ECNamedCurveParameterSpec ecp = ECNamedCurveTable.getParameterSpec("prime256v1");
    ECDomainParameters domainParams =
            new ECDomainParameters(ecp.getCurve(), ecp.getG(), ecp.getN(), ecp.getH(), ecp.getSeed());
    ECKeyGenerationParameters keyGenParams = new ECKeyGenerationParameters(domainParams, new SecureRandom());

    ECKeyPairGenerator generator = new ECKeyPairGenerator();
    generator.init(keyGenParams);

    AsymmetricCipherKeyPair keyPair = generator.generateKeyPair();
    ECPrivateKeyParameters privateKey = (ECPrivateKeyParameters) keyPair.getPrivate();
    ECPublicKeyParameters publicKey = (ECPublicKeyParameters) keyPair.getPublic();

    LocalIdentity localIdentity = load(publicKey.getQ().normalize(), privateKey.getD());
    localIdentity.improveSecurity(securityLevel);

    return localIdentity;
}
 
Example 3
Source File: SM2Util.java    From javasdk with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * create a random key pair of sm.
 *
 * @return String array of publicKey and privateKey
 */
public static AsymmetricCipherKeyPair generateKeyPair() {
    SecureRandom random = new SecureRandom();
    ECKeyGenerationParameters keyGenerationParams = new ECKeyGenerationParameters(DOMAIN_PARAMS, random);
    ECKeyPairGenerator keyGen = new ECKeyPairGenerator();
    keyGen.init(keyGenerationParams);
    return keyGen.generateKeyPair();
}
 
Example 4
Source File: BCECUtil.java    From gmhelper with Apache License 2.0 5 votes vote down vote up
/**
 * 生成ECC密钥对
 *
 * @return ECC密钥对
 */
public static AsymmetricCipherKeyPair generateKeyPairParameter(
        ECDomainParameters domainParameters, SecureRandom random) {
    ECKeyGenerationParameters keyGenerationParams = new ECKeyGenerationParameters(domainParameters,
            random);
    ECKeyPairGenerator keyGen = new ECKeyPairGenerator();
    keyGen.init(keyGenerationParams);
    return keyGen.generateKeyPair();
}
 
Example 5
Source File: Sm2KeyPairImpl.java    From littleca with Apache License 2.0 5 votes vote down vote up
public Sm2KeyPairImpl(boolean selfgen) {
	SecureRandom random = new SecureRandom();
	ECKeyGenerationParameters keyGenerationParams = new ECKeyGenerationParameters(DOMAIN_PARAMS, random);
	ECKeyPairGenerator keyGen = new ECKeyPairGenerator();
	keyGen.init(keyGenerationParams);
	AsymmetricCipherKeyPair keyPair = keyGen.generateKeyPair();
	ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
	ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();
	ECDomainParameters domainParams = priKey.getParameters();
	ECParameterSpec spec = new ECParameterSpec(domainParams.getCurve(), domainParams.getG(), domainParams.getN(),
			domainParams.getH());
	BCECPublicKey bcecPublicKey = new BCECPublicKey(ALGO_NAME_EC, pubKey, spec, BouncyCastleProvider.CONFIGURATION);
	publicKey = new Sm2PublicKeyImpl(bcecPublicKey);
	privateKey = new Sm2PrivateKeyImpl(new BCECPrivateKey(ALGO_NAME_EC, priKey, bcecPublicKey, spec, BouncyCastleProvider.CONFIGURATION));
}
 
Example 6
Source File: BCECUtil.java    From littleca with Apache License 2.0 5 votes vote down vote up
/**
 * 生成ECC密钥对
 *
 * @return ECC密钥对
 */
public static AsymmetricCipherKeyPair generateKeyPair(ECDomainParameters domainParameters,
                                                      SecureRandom random) {
    ECKeyGenerationParameters keyGenerationParams = new ECKeyGenerationParameters(domainParameters,
        random);
    ECKeyPairGenerator keyGen = new ECKeyPairGenerator();
    keyGen.init(keyGenerationParams);
    return keyGen.generateKeyPair();
}
 
Example 7
Source File: SM2.java    From julongchain with Apache License 2.0 5 votes vote down vote up
/**
 * sm2密钥对生成
 *
 * @return
 */
public SM2KeyPair generateKeyPair() {
    ECKeyGenerationParameters ecKeyGenerationParameters = new ECKeyGenerationParameters(ecc_bc_spec, new SecureRandom());
    ECKeyPairGenerator keyPairGenerator = new ECKeyPairGenerator();
    keyPairGenerator.init(ecKeyGenerationParameters);
    AsymmetricCipherKeyPair kp = keyPairGenerator.generateKeyPair();
    ECPrivateKeyParameters ecpriv = (ECPrivateKeyParameters) kp.getPrivate();
    ECPublicKeyParameters ecpub = (ECPublicKeyParameters) kp.getPublic();
    BigInteger privateKey = ecpriv.getD();
    ECPoint publicKey = ecpub.getQ();
    return new SM2KeyPair(publicKey.getEncoded(false), privateKey.toByteArray());
}
 
Example 8
Source File: ECKey.java    From nuls-v2 with MIT License 5 votes vote down vote up
/**
 * Generates an entirely new keypair with the given {@link SecureRandom} object. Point compression is used so the
 * resulting public key will be 33 bytes (32 for the co-ordinate and 1 byte to represent the y bit).
 */
public ECKey(SecureRandom secureRandom) {
    ECKeyPairGenerator generator = new ECKeyPairGenerator();
    ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters(CURVE, secureRandom);
    generator.init(keygenParams);
    AsymmetricCipherKeyPair keypair = generator.generateKeyPair();
    ECPrivateKeyParameters privParams = (ECPrivateKeyParameters) keypair.getPrivate();
    ECPublicKeyParameters pubParams = (ECPublicKeyParameters) keypair.getPublic();
    priv = privParams.getD();
    pub = new LazyECPoint(CURVE.getCurve(), pubParams.getQ().getEncoded(true));
    creationTimeSeconds = System.currentTimeMillis();
}
 
Example 9
Source File: BouncyCastleCrypto.java    From fabric-api-archive with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] createNewPrivateKey() {
    ECKeyPairGenerator generator = new ECKeyPairGenerator();
    ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters(domain, secureRandom);
    generator.init(keygenParams);
    AsymmetricCipherKeyPair keypair = generator.generateKeyPair();
    ECPrivateKeyParameters privParams = (ECPrivateKeyParameters) keypair.getPrivate();
    return privParams.getD().toByteArray();
}
 
Example 10
Source File: BouncyCastleCrypto.java    From fabric-api with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] createNewPrivateKey() {
    ECKeyPairGenerator generator = new ECKeyPairGenerator();
    ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters(domain, secureRandom);
    generator.init(keygenParams);
    AsymmetricCipherKeyPair keypair = generator.generateKeyPair();
    ECPrivateKeyParameters privParams = (ECPrivateKeyParameters) keypair.getPrivate();
    return privParams.getD().toByteArray();
}
 
Example 11
Source File: Sign1MessageTest.java    From COSE-JAVA with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@BeforeClass
public static void setUpClass() throws CoseException {

    X9ECParameters p = NISTNamedCurves.getByName("P-256");
    
    ECDomainParameters parameters = new ECDomainParameters(p.getCurve(), p.getG(), p.getN(), p.getH());
    ECKeyPairGenerator pGen = new ECKeyPairGenerator();
    ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(parameters, null);
    pGen.init(genParam);
    
    AsymmetricCipherKeyPair p1 = pGen.generateKeyPair();
    
    keyPublic = (ECPublicKeyParameters) p1.getPublic();
    keyPrivate = (ECPrivateKeyParameters) p1.getPrivate();
    
byte[] rgbX = keyPublic.getQ().normalize().getXCoord().getEncoded();
byte[] rgbY = keyPublic.getQ().normalize().getYCoord().getEncoded();
boolean signY = true;
byte[] rgbD = keyPrivate.getD().toByteArray();

CBORObject key = CBORObject.NewMap();
    key.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_EC2);
    key.Add(KeyKeys.EC2_Curve.AsCBOR(), KeyKeys.EC2_P256);
    key.Add(KeyKeys.EC2_X.AsCBOR(), rgbX);
    key.Add(KeyKeys.EC2_Y.AsCBOR(), rgbY);
    cnKeyPublic = new OneKey(key);
    
    key = CBORObject.NewMap();
    key.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_EC2);
    key.Add(KeyKeys.EC2_Curve.AsCBOR(), KeyKeys.EC2_P256);
    key.Add(KeyKeys.EC2_X.AsCBOR(), rgbX);
    key.Add(KeyKeys.EC2_Y.AsCBOR(), rgbY);
    cnKeyPublicCompressed = new OneKey(key);

    key = CBORObject.NewMap();
    key.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_EC2);
    key.Add(KeyKeys.EC2_Curve.AsCBOR(), KeyKeys.EC2_P256);
    key.Add(KeyKeys.EC2_D.AsCBOR(), rgbD);
    cnKeyPrivate = new OneKey(key);
}
 
Example 12
Source File: BCECUtil.java    From jiguang-java-client-common with MIT License 5 votes vote down vote up
/**
 * 生成ECC密钥对
 *
 * @return ECC密钥对
 */
public static AsymmetricCipherKeyPair generateKeyPairParameter(ECDomainParameters domainParameters,
    SecureRandom random) {
    ECKeyGenerationParameters keyGenerationParams = new ECKeyGenerationParameters(domainParameters,
        random);
    ECKeyPairGenerator keyGen = new ECKeyPairGenerator();
    keyGen.init(keyGenerationParams);
    return keyGen.generateKeyPair();
}
 
Example 13
Source File: SecP256K1KeyGenerator.java    From nem.core with MIT License 5 votes vote down vote up
@Override
public KeyPair generateKeyPair() {
	final ECKeyPairGenerator generator = new ECKeyPairGenerator();
	final ECKeyGenerationParameters keyGenParams = new ECKeyGenerationParameters(SecP256K1Curve.secp256k1().getParams(), RANDOM);
	generator.init(keyGenParams);

	final AsymmetricCipherKeyPair keyPair = generator.generateKeyPair();
	final ECPrivateKeyParameters privateKeyParams = (ECPrivateKeyParameters)keyPair.getPrivate();
	final PrivateKey privateKey = new PrivateKey(privateKeyParams.getD());
	return new KeyPair(privateKey, CryptoEngines.secp256k1Engine());
}
 
Example 14
Source File: ECKeyPair.java    From bop-bitcoin-client with Apache License 2.0 5 votes vote down vote up
public static ECKeyPair createNew (boolean compressed)
{
	ECKeyPairGenerator generator = new ECKeyPairGenerator ();
	ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters (domain, secureRandom);
	generator.init (keygenParams);
	AsymmetricCipherKeyPair keypair = generator.generateKeyPair ();
	ECPrivateKeyParameters privParams = (ECPrivateKeyParameters) keypair.getPrivate ();
	ECPublicKeyParameters pubParams = (ECPublicKeyParameters) keypair.getPublic ();
	ECKeyPair k = new ECKeyPair ();
	k.priv = privParams.getD ();
	k.compressed = compressed;
	k.pub = pubParams.getQ ().getEncoded (compressed);
	return k;
}
 
Example 15
Source File: ECKeyPair.java    From WalletCordova with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static ECKeyPair createNew (boolean compressed)
{
	ECKeyPairGenerator generator = new ECKeyPairGenerator ();
	ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters (domain, secureRandom);
	generator.init (keygenParams);
	AsymmetricCipherKeyPair keypair = generator.generateKeyPair ();
	ECPrivateKeyParameters privParams = (ECPrivateKeyParameters) keypair.getPrivate ();
	ECPublicKeyParameters pubParams = (ECPublicKeyParameters) keypair.getPublic ();
	ECKeyPair k = new ECKeyPair ();
	k.priv = privParams.getD ();
	k.compressed = compressed;
	k.pub = pubParams.getQ ().getEncoded (compressed);
	return k;
}
 
Example 16
Source File: ECGEN.java    From warp10-platform with Apache License 2.0 2 votes vote down vote up
@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {
  Object top = stack.pop();

  if (!(top instanceof String)) {
    throw new WarpScriptException(getName() + " expects a curve name.");
  }

  String name = (String) top;

  ECKeyPairGenerator gen = new ECKeyPairGenerator();

  ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(name);
  
  if (null == spec) {
    throw new WarpScriptException(getName() + " only supports the following curves: " + getCurves() + ".");
  }
  
  ECCurve curve = spec.getCurve();
  ECDomainParameters domainParams = new ECDomainParameters(curve, spec.getG(),spec.getN(), spec.getH(), spec.getSeed());    
  ECKeyGenerationParameters params = new ECKeyGenerationParameters(domainParams, CryptoHelper.getSecureRandom());
  
  gen.init(params);

  final AsymmetricCipherKeyPair keypair = gen.generateKeyPair();

  ECPrivateKeyParameters privateKey = (ECPrivateKeyParameters) keypair.getPrivate();
  ECPublicKeyParameters publicKey = (ECPublicKeyParameters) keypair.getPublic();
  
  Map<String,String> keyparams = new HashMap<String,String>();
  
  keyparams.put(Constants.KEY_CURVE, name);
  keyparams.put(Constants.KEY_D, privateKey.getD().toString());
  
  stack.push(keyparams);
  
  keyparams = new HashMap<String,String>();

  keyparams.put(Constants.KEY_CURVE, name);
  keyparams.put(Constants.KEY_Q, Hex.encodeHexString(publicKey.getQ().getEncoded()));

  stack.push(keyparams);

  return stack;
}