Example usage for org.bouncycastle.jcajce.provider.asymmetric.ec BCECPrivateKey getParameters

List of usage examples for org.bouncycastle.jcajce.provider.asymmetric.ec BCECPrivateKey getParameters

Introduction

In this page you can find the example usage for org.bouncycastle.jcajce.provider.asymmetric.ec BCECPrivateKey getParameters.

Prototype

public org.bouncycastle.jce.spec.ECParameterSpec getParameters() 

Source Link

Usage

From source file:com.yahoo.athenz.auth.util.Crypto.java

License:Apache License

public static PublicKey extractPublicKey(PrivateKey privateKey) throws CryptoException {

    // we only support RSA and ECDSA private keys

    PublicKey publicKey = null;/*from  ww w  .  j  a  v  a2s. c o m*/
    switch (privateKey.getAlgorithm()) {
    case RSA:
        try {
            KeyFactory kf = KeyFactory.getInstance(RSA, BC_PROVIDER);
            RSAPrivateCrtKey rsaCrtKey = (RSAPrivateCrtKey) privateKey;
            RSAPublicKeySpec keySpec = new RSAPublicKeySpec(rsaCrtKey.getModulus(),
                    rsaCrtKey.getPublicExponent());
            publicKey = kf.generatePublic(keySpec);
        } catch (NoSuchProviderException ex) {
            LOG.error("extractPublicKey: RSA - Caught NoSuchProviderException exception: " + ex.getMessage());
            throw new CryptoException(ex);
        } catch (NoSuchAlgorithmException ex) {
            LOG.error("extractPublicKey: RSA - Caught NoSuchAlgorithmException exception: " + ex.getMessage());
            throw new CryptoException(ex);
        } catch (InvalidKeySpecException ex) {
            LOG.error("extractPublicKey: RSA - Caught InvalidKeySpecException exception: " + ex.getMessage());
            throw new CryptoException(ex);
        }
        break;

    case ECDSA:
        try {
            KeyFactory kf = KeyFactory.getInstance(ECDSA, BC_PROVIDER);
            BCECPrivateKey ecPrivKey = (BCECPrivateKey) privateKey;
            ECMultiplier ecMultiplier = new FixedPointCombMultiplier();
            ECParameterSpec ecParamSpec = (ECParameterSpec) ecPrivKey.getParameters();
            ECPoint ecPointQ = ecMultiplier.multiply(ecParamSpec.getG(), ecPrivKey.getD());
            ECPublicKeySpec keySpec = new ECPublicKeySpec(ecPointQ, ecParamSpec);
            publicKey = kf.generatePublic(keySpec);
        } catch (NoSuchProviderException ex) {
            LOG.error("extractPublicKey: ECDSA - Caught NoSuchProviderException exception: " + ex.getMessage());
            throw new CryptoException(ex);
        } catch (NoSuchAlgorithmException ex) {
            LOG.error(
                    "extractPublicKey: ECDSA - Caught NoSuchAlgorithmException exception: " + ex.getMessage());
            throw new CryptoException(ex);
        } catch (InvalidKeySpecException ex) {
            LOG.error("extractPublicKey: ECDSA - Caught InvalidKeySpecException exception: " + ex.getMessage());
            throw new CryptoException(ex);
        }
        break;

    default:
        String msg = "Unsupported Key Algorithm: " + privateKey.getAlgorithm();
        LOG.error("extractPublicKey: " + msg);
        throw new CryptoException(msg);
    }
    return publicKey;
}

From source file:edu.tamu.tcat.crypto.bouncycastle.ASN1SeqKeyImpl.java

License:Apache License

private static DERBitString getPublic(java.security.interfaces.ECPrivateKey key) throws EncodingException {
    BCECPrivateKey priv = (BCECPrivateKey) key;
    org.bouncycastle.math.ec.ECPoint g = priv.getParameters().getG();
    org.bouncycastle.math.ec.ECPoint q = g.multiply(priv.getS());
    return new DERBitString(q.getEncoded());
}