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

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

Introduction

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

Prototype

public byte[] getEncoded(String encoding) throws IOException 

Source Link

Document

Return either the default for "BER" or a DER encoding if "DER" is specified.

Usage

From source file:net.jmhertlein.mcanalytics.api.auth.SSLUtil.java

License:Open Source License

public static PublicKey getPublicKeyFromInfo(SubjectPublicKeyInfo o) {
    try {/*  ww  w  .j  ava2 s . c  o m*/
        byte[] bytes = o.getEncoded("X509");
        return KeyFactory.getInstance("EC").generatePublic(new X509EncodedKeySpec(bytes));
    } catch (NoSuchAlgorithmException | InvalidKeySpecException | IOException ex) {
        Logger.getLogger(SSLUtil.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

From source file:org.jmrtd.Util.java

License:Open Source License

public static PublicKey toPublicKey(SubjectPublicKeyInfo subjectPublicKeyInfo) {
    try {/*from  w ww. j  a v  a2 s .c  o  m*/
        byte[] encodedPublicKeyInfoBytes = subjectPublicKeyInfo.getEncoded(ASN1Encoding.DER);
        KeySpec keySpec = new X509EncodedKeySpec(encodedPublicKeyInfoBytes);
        try {
            KeyFactory factory = KeyFactory.getInstance("DH");
            return factory.generatePublic(keySpec);
        } catch (GeneralSecurityException gse) {
            KeyFactory factory = KeyFactory.getInstance("EC", BC_PROVIDER);
            return factory.generatePublic(keySpec);
        }
    } catch (GeneralSecurityException gse2) {
        LOGGER.severe("Exception: " + gse2.getMessage());
        return null;
    } catch (Exception ioe) {
        LOGGER.severe("Exception: " + ioe.getMessage());
        return null;
    }
}