Search in sources :

Example 16 with PemObject

use of org.bouncycastle.util.io.pem.PemObject in project cloudstack by apache.

the class CertServiceImpl method parsePrivateKey.

public PrivateKey parsePrivateKey(final String key) throws IOException {
    Preconditions.checkArgument(!Strings.isNullOrEmpty(key));
    try (final PemReader pemReader = new PemReader(new StringReader(key))) {
        final PemObject pemObject = pemReader.readPemObject();
        final byte[] content = pemObject.getContent();
        final PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(content);
        final KeyFactory factory = KeyFactory.getInstance("RSA", "BC");
        return factory.generatePrivate(privKeySpec);
    } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
        throw new IOException("No encryption provider available.", e);
    } catch (final InvalidKeySpecException e) {
        throw new IOException("Invalid Key format.", e);
    }
}
Also used : PemReader(org.bouncycastle.util.io.pem.PemReader) PemObject(org.bouncycastle.util.io.pem.PemObject) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) StringReader(java.io.StringReader) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchProviderException(java.security.NoSuchProviderException) KeyFactory(java.security.KeyFactory)

Aggregations

PemObject (org.bouncycastle.util.io.pem.PemObject)16 PemReader (org.bouncycastle.util.io.pem.PemReader)9 IOException (java.io.IOException)7 StringReader (java.io.StringReader)6 Certificate (java.security.cert.Certificate)5 PemWriter (org.bouncycastle.util.io.pem.PemWriter)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 StringWriter (java.io.StringWriter)4 PrivateKey (java.security.PrivateKey)4 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)4 KeyFactory (java.security.KeyFactory)3 KeyPair (java.security.KeyPair)3 X509Certificate (java.security.cert.X509Certificate)3 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)3 ArrayList (java.util.ArrayList)3 BigInteger (java.math.BigInteger)2 PublicKey (java.security.PublicKey)2 CertificateFactory (java.security.cert.CertificateFactory)2 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)2 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)2