Example usage for org.bouncycastle.asn1.x509 SubjectPublicKeyInfo parsePublicKey

List of usage examples for org.bouncycastle.asn1.x509 SubjectPublicKeyInfo parsePublicKey

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.x509 SubjectPublicKeyInfo parsePublicKey.

Prototype

public ASN1Primitive parsePublicKey() throws IOException 

Source Link

Document

for when the public key is an encoded object - if the bitstring can't be decoded this routine throws an IOException.

Usage

From source file:io.apigee.trireme.crypto.algorithms.DsaKeyPairProvider.java

License:Open Source License

/**
 * DSA public key format -- the PEM file contains a "SubjectPublicKeyInfo" object, which contains
 * an "Algorithm Identifier" that consists of three integers (p, q, and g) and a single
 * integer representing y. We use those four parts to assemble a Java public key.
 *//*from   w w  w.  j  a va 2  s  .com*/
@Override
public PublicKey readPublicKey(String algorithm, Reader rdr) throws CryptoException, IOException {
    PEMParser pp = new PEMParser(rdr);
    try {
        Object po = pp.readObject();
        if (log.isDebugEnabled()) {
            log.debug("Trying to read an {} public key and got {}", algorithm, po);
        }

        if (po instanceof SubjectPublicKeyInfo) {
            SubjectPublicKeyInfo pk = (SubjectPublicKeyInfo) po;

            AlgorithmIdentifier alg = pk.getAlgorithm();
            if (!(alg.getParameters() instanceof ASN1Sequence)) {
                throw new CryptoException("Invalid DSA public key format: Algorithm ID not a Sequence");
            }

            ASN1Sequence identifiers = (ASN1Sequence) (alg.getParameters());
            if (identifiers.size() != 3) {
                throw new CryptoException("Invalid DSA public key format: Identifier does not have 3 items");
            }

            DERInteger p = (DERInteger) identifiers.getObjectAt(0);
            DERInteger q = (DERInteger) identifiers.getObjectAt(1);
            DERInteger g = (DERInteger) identifiers.getObjectAt(2);

            ASN1Primitive pkPrim = pk.parsePublicKey();
            if (!(pkPrim instanceof ASN1Integer)) {
                throw new CryptoException("Invalid DSA public key format: Public key is not an integer");
            }
            DERInteger y = (DERInteger) pkPrim;

            try {
                KeyFactory factory = KeyFactory.getInstance("DSA");
                DSAPublicKeySpec pubSpec = new DSAPublicKeySpec(y.getValue(), p.getValue(), q.getValue(),
                        g.getValue());
                return factory.generatePublic(pubSpec);
            } catch (GeneralSecurityException gse) {
                throw new CryptoException(gse);
            }
        }
        throw new CryptoException("Input data does not contain a public key");
    } finally {
        pp.close();
    }
}

From source file:io.apigee.trireme.crypto.algorithms.RsaKeyPairProvider.java

License:Open Source License

private PublicKey convertPublicKey(SubjectPublicKeyInfo pk) throws CryptoException, IOException {
    RSAPublicKey rsa = RSAPublicKey.getInstance(pk.parsePublicKey());

    try {//from w  w  w  .  ja v  a2s.co m
        KeyFactory factory = KeyFactory.getInstance("RSA");
        RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(rsa.getModulus(), rsa.getPublicExponent());
        return factory.generatePublic(pubSpec);

    } catch (GeneralSecurityException gse) {
        throw new CryptoException(gse);
    }
}

From source file:io.apigee.trireme.crypto.RSAConverter.java

License:Open Source License

public static PublicKey convertPublicKey(SubjectPublicKeyInfo pk) throws CryptoException, IOException {
    RSAPublicKey rsa = RSAPublicKey.getInstance(pk.parsePublicKey());

    try {/*  w  ww .  j ava2s  . c o m*/
        KeyFactory factory = KeyFactory.getInstance("RSA");
        RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(rsa.getModulus(), rsa.getPublicExponent());
        return factory.generatePublic(pubSpec);

    } catch (GeneralSecurityException gse) {
        throw new CryptoException(gse);
    }
}