Java Code Examples for org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey#getD()

The following examples show how to use org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey#getD() . 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: SECP256K1.java    From incubator-tuweni with Apache License 2.0 6 votes vote down vote up
/**
 * Generate a new keypair.
 *
 * Entropy for the generation is drawn from {@link SecureRandom}.
 *
 * @return A new keypair.
 */
public static KeyPair random() {
  java.security.KeyPair rawKeyPair = Parameters.KEY_PAIR_GENERATOR.generateKeyPair();
  BCECPrivateKey privateKey = (BCECPrivateKey) rawKeyPair.getPrivate();
  BCECPublicKey publicKey = (BCECPublicKey) rawKeyPair.getPublic();

  BigInteger privateKeyValue = privateKey.getD();

  // Ethereum does not use encoded public keys like bitcoin - see
  // https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details
  // Additionally, as the first bit is a constant prefix (0x04) we ignore this value
  byte[] publicKeyBytes = publicKey.getQ().getEncoded(false);
  BigInteger publicKeyValue = new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length));

  return new KeyPair(SecretKey.fromInteger(privateKeyValue), PublicKey.fromInteger(publicKeyValue));
}
 
Example 2
Source File: BlockDataGenerator.java    From besu with Apache License 2.0 6 votes vote down vote up
private SECP256K1.KeyPair generateKeyPair() {
  final java.security.KeyPair rawKeyPair = keyPairGenerator.generateKeyPair();
  final BCECPrivateKey privateKey = (BCECPrivateKey) rawKeyPair.getPrivate();
  final BCECPublicKey publicKey = (BCECPublicKey) rawKeyPair.getPublic();

  final BigInteger privateKeyValue = privateKey.getD();

  // Ethereum does not use encoded public keys like bitcoin - see
  // https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details
  // Additionally, as the first bit is a constant prefix (0x04) we ignore this value
  final byte[] publicKeyBytes = publicKey.getQ().getEncoded(false);
  final BigInteger publicKeyValue =
      new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length));

  return new SECP256K1.KeyPair(
      SECP256K1.PrivateKey.create(privateKeyValue), SECP256K1.PublicKey.create(publicKeyValue));
}
 
Example 3
Source File: SECP256K1.java    From besu with Apache License 2.0 6 votes vote down vote up
public static KeyPair generate() {
  final java.security.KeyPair rawKeyPair = KEY_PAIR_GENERATOR.generateKeyPair();
  final BCECPrivateKey privateKey = (BCECPrivateKey) rawKeyPair.getPrivate();
  final BCECPublicKey publicKey = (BCECPublicKey) rawKeyPair.getPublic();

  final BigInteger privateKeyValue = privateKey.getD();

  // Ethereum does not use encoded public keys like bitcoin - see
  // https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details
  // Additionally, as the first bit is a constant prefix (0x04) we ignore this value
  final byte[] publicKeyBytes = publicKey.getQ().getEncoded(false);
  final BigInteger publicKeyValue =
      new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length));

  return new KeyPair(PrivateKey.create(privateKeyValue), PublicKey.create(publicKeyValue));
}
 
Example 4
Source File: SECP256K1.java    From cava with Apache License 2.0 6 votes vote down vote up
/**
 * Generate a new keypair.
 *
 * Entropy for the generation is drawn from {@link SecureRandom}.
 *
 * @return A new keypair.
 */
public static KeyPair random() {
  java.security.KeyPair rawKeyPair = Parameters.KEY_PAIR_GENERATOR.generateKeyPair();
  BCECPrivateKey privateKey = (BCECPrivateKey) rawKeyPair.getPrivate();
  BCECPublicKey publicKey = (BCECPublicKey) rawKeyPair.getPublic();

  BigInteger privateKeyValue = privateKey.getD();

  // Ethereum does not use encoded public keys like bitcoin - see
  // https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details
  // Additionally, as the first bit is a constant prefix (0x04) we ignore this value
  byte[] publicKeyBytes = publicKey.getQ().getEncoded(false);
  BigInteger publicKeyValue = new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length));

  return new KeyPair(SecretKey.fromInteger(privateKeyValue), PublicKey.fromInteger(publicKeyValue));
}
 
Example 5
Source File: ECKeyPair.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
/**
 * create ECKeyPair from KeyPair
 *
 * @param keyPair
 * @return
 */
public static ECKeyPair create(KeyPair keyPair) {
    BCECPrivateKey privateKey = (BCECPrivateKey) keyPair.getPrivate();
    BCECPublicKey publicKey = (BCECPublicKey) keyPair.getPublic();

    BigInteger privateKeyValue = privateKey.getD();

    // Ethereum does not use encoded public keys like bitcoin - see
    // https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details
    // Additionally, as the first bit is a constant prefix (0x04) we ignore this value
    byte[] publicKeyBytes = publicKey.getQ().getEncoded(false);
    BigInteger publicKeyValue =
            new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length));

    ECKeyPair ecKeyPair = new ECKeyPair(privateKeyValue, publicKeyValue);
    return ecKeyPair;
}
 
Example 6
Source File: ECKeyPair.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
public static ECKeyPair create(KeyPair keyPair) {
    BCECPrivateKey privateKey = (BCECPrivateKey) keyPair.getPrivate();
    BCECPublicKey publicKey = (BCECPublicKey) keyPair.getPublic();

    BigInteger privateKeyValue = privateKey.getD();

    // Ethereum does not use encoded public keys like bitcoin - see
    // https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details
    // Additionally, as the first bit is a constant prefix (0x04) we ignore this value
    byte[] publicKeyBytes = publicKey.getQ().getEncoded(false);
    BigInteger publicKeyValue =
            new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length));

    return new ECKeyPair(privateKeyValue, publicKeyValue);
}
 
Example 7
Source File: ECKeyPair.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
public static ECKeyPair create(KeyPair keyPair) {
    BCECPrivateKey privateKey = (BCECPrivateKey) keyPair.getPrivate();
    BCECPublicKey publicKey = (BCECPublicKey) keyPair.getPublic();

    BigInteger privateKeyValue = privateKey.getD();

    // Ethereum does not use encoded public keys like bitcoin - see
    // https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details
    // Additionally, as the first bit is a constant prefix (0x04) we ignore this value
    byte[] publicKeyBytes = publicKey.getQ().getEncoded(false);
    BigInteger publicKeyValue =
            new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length));

    return new ECKeyPair(privateKeyValue, publicKeyValue);
}
 
Example 8
Source File: BCECUtil.java    From littleca with Apache License 2.0 5 votes vote down vote up
/**
 * openssl i2d_ECPrivateKey函数生成的DER编码的ecc私钥是:PKCS1标准的、带有EC_GROUP、带有公钥的,
 * 这个工具函数的主要目的就是为了使Java程序能够“识别”openssl生成的ECC私钥
 *
 * @param encodedKey
 * @return
 * @throws NoSuchAlgorithmException
 * @throws NoSuchProviderException
 * @throws InvalidKeySpecException
 */
public static ECPrivateKeyParameters convertPkcs1DerToEcPriKey(byte[] encodedKey)
    throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
    PKCS8EncodedKeySpec peks = new PKCS8EncodedKeySpec(encodedKey);
    KeyFactory kf = KeyFactory.getInstance(ALGO_NAME_EC, BouncyCastleProvider.PROVIDER_NAME);
    BCECPrivateKey privateKey = (BCECPrivateKey) kf.generatePrivate(peks);
    ECParameterSpec ecParameterSpec = privateKey.getParameters();
    ECDomainParameters ecDomainParameters = new ECDomainParameters(ecParameterSpec.getCurve(),
        ecParameterSpec.getG(), ecParameterSpec.getN(), ecParameterSpec.getH());
    ECPrivateKeyParameters priKey = new ECPrivateKeyParameters(privateKey.getD(),
        ecDomainParameters);
    return priKey;
}
 
Example 9
Source File: ECKeyPair.java    From blockchain with Apache License 2.0 5 votes vote down vote up
public ECKeyPair(PrivateKey privateKey, PublicKey publicKey) {
    this.privateKey = privateKey;
    this.publicKey = publicKey;
    // 生成 BigInteger 形式的公钥和私钥
    BCECPrivateKey bcecPrivateKey = (BCECPrivateKey) this.privateKey;
    BCECPublicKey bcecPublicKey = (BCECPublicKey) this.publicKey;
    // 分别计算公钥和私钥的值
    BigInteger privateKeyValue = bcecPrivateKey.getD();
    byte[] publicKeyBytes = bcecPublicKey.getQ().getEncoded(false);
    BigInteger publicKeyValue = new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length));
    this.privateKeyValue = privateKeyValue;
    this.publicKeyValue = publicKeyValue;
}
 
Example 10
Source File: ECKeyPair.java    From web3j with Apache License 2.0 5 votes vote down vote up
public static ECKeyPair create(KeyPair keyPair) {
    BCECPrivateKey privateKey = (BCECPrivateKey) keyPair.getPrivate();
    BCECPublicKey publicKey = (BCECPublicKey) keyPair.getPublic();

    BigInteger privateKeyValue = privateKey.getD();

    // Ethereum does not use encoded public keys like bitcoin - see
    // https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details
    // Additionally, as the first bit is a constant prefix (0x04) we ignore this value
    byte[] publicKeyBytes = publicKey.getQ().getEncoded(false);
    BigInteger publicKeyValue =
            new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length));

    return new ECKeyPair(privateKeyValue, publicKeyValue);
}
 
Example 11
Source File: BCECUtil.java    From gmhelper with Apache License 2.0 4 votes vote down vote up
public static ECPrivateKeyParameters convertPrivateKeyToParameters(BCECPrivateKey ecPriKey) {
    ECParameterSpec parameterSpec = ecPriKey.getParameters();
    ECDomainParameters domainParameters = new ECDomainParameters(parameterSpec.getCurve(), parameterSpec.getG(),
            parameterSpec.getN(), parameterSpec.getH());
    return new ECPrivateKeyParameters(ecPriKey.getD(), domainParameters);
}
 
Example 12
Source File: BCECUtil.java    From jiguang-java-client-common with MIT License 4 votes vote down vote up
public static ECPrivateKeyParameters convertPrivateKeyToParameters(BCECPrivateKey ecPriKey) {
    ECParameterSpec parameterSpec = ecPriKey.getParameters();
    ECDomainParameters domainParameters = new ECDomainParameters(parameterSpec.getCurve(), parameterSpec.getG(),
        parameterSpec.getN(), parameterSpec.getH());
    return new ECPrivateKeyParameters(ecPriKey.getD(), domainParameters);
}
 
Example 13
Source File: ECDSAKeyFactory.java    From oxAuth with MIT License 4 votes vote down vote up
public ECDSAKeyFactory(SignatureAlgorithm signatureAlgorithm, String dnName)
        throws InvalidParameterException, NoSuchProviderException, NoSuchAlgorithmException,
        InvalidAlgorithmParameterException, SignatureException, InvalidKeyException, CertificateEncodingException {
    if (signatureAlgorithm == null) {
        throw new InvalidParameterException("The signature algorithm cannot be null");
    }

    this.signatureAlgorithm = signatureAlgorithm;

    ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(signatureAlgorithm.getCurve().getName());

    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDSA", "BC");
    keyGen.initialize(ecSpec, new SecureRandom());

    this.keyPair = keyGen.generateKeyPair();
    BCECPrivateKey privateKeySpec = (BCECPrivateKey) keyPair.getPrivate();
    BCECPublicKey publicKeySpec = (BCECPublicKey) keyPair.getPublic();

    BigInteger x = publicKeySpec.getQ().getXCoord().toBigInteger();
    BigInteger y = publicKeySpec.getQ().getYCoord().toBigInteger();
    BigInteger d = privateKeySpec.getD();

    this.ecdsaPrivateKey = new ECDSAPrivateKey(d);
    this.ecdsaPublicKey = new ECDSAPublicKey(signatureAlgorithm, x, y);

    if (StringUtils.isNotBlank(dnName)) {
        // Create certificate
        GregorianCalendar startDate = new GregorianCalendar(); // time from which certificate is valid
        GregorianCalendar expiryDate = new GregorianCalendar(); // time after which certificate is not valid
        expiryDate.add(Calendar.YEAR, 1);
        BigInteger serialNumber = new BigInteger(1024, new Random()); // serial number for certificate

        X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
        X500Principal principal = new X500Principal(dnName);

        certGen.setSerialNumber(serialNumber);
        certGen.setIssuerDN(principal);
        certGen.setNotBefore(startDate.getTime());
        certGen.setNotAfter(expiryDate.getTime());
        certGen.setSubjectDN(principal); // note: same as issuer
        certGen.setPublicKey(keyPair.getPublic());
        certGen.setSignatureAlgorithm("SHA256WITHECDSA");

        X509Certificate x509Certificate = certGen.generate(privateKeySpec, "BC");
        this.certificate = new Certificate(signatureAlgorithm, x509Certificate);
    }
}