Search in sources :

Example 16 with ECPoint

use of org.bouncycastle.math.ec.ECPoint in project athenz by yahoo.

the class Crypto method extractPublicKey.

public static PublicKey extractPublicKey(PrivateKey privateKey) throws CryptoException {
    // we only support RSA and ECDSA private keys
    PublicKey publicKey = null;
    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;
}
Also used : RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) BCECPublicKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey) PublicKey(java.security.PublicKey) ECMultiplier(org.bouncycastle.math.ec.ECMultiplier) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DEROctetString(org.bouncycastle.asn1.DEROctetString) DERIA5String(org.bouncycastle.asn1.DERIA5String) ECPoint(org.bouncycastle.math.ec.ECPoint) ECPublicKeySpec(org.bouncycastle.jce.spec.ECPublicKeySpec) BCECPrivateKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey) FixedPointCombMultiplier(org.bouncycastle.math.ec.FixedPointCombMultiplier) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchProviderException(java.security.NoSuchProviderException) KeyFactory(java.security.KeyFactory)

Aggregations

ECPoint (org.bouncycastle.math.ec.ECPoint)16 BigInteger (java.math.BigInteger)11 ECPublicKeyParameters (org.bouncycastle.crypto.params.ECPublicKeyParameters)8 ECPrivateKeyParameters (org.bouncycastle.crypto.params.ECPrivateKeyParameters)5 ECParameterSpec (org.bouncycastle.jce.spec.ECParameterSpec)4 ECPublicKeySpec (org.bouncycastle.jce.spec.ECPublicKeySpec)4 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)3 AsymmetricCipherKeyPair (org.bouncycastle.crypto.AsymmetricCipherKeyPair)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ECFieldElement (org.bouncycastle.math.ec.ECFieldElement)2 GeneralSecurityException (java.security.GeneralSecurityException)1 KeyFactory (java.security.KeyFactory)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 PublicKey (java.security.PublicKey)1 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)1 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)1 DERIA5String (org.bouncycastle.asn1.DERIA5String)1 DEROctetString (org.bouncycastle.asn1.DEROctetString)1 X9ECParameters (org.bouncycastle.asn1.x9.X9ECParameters)1