Example usage for org.bouncycastle.asn1.pkcs PrivateKeyInfo toASN1Primitive

List of usage examples for org.bouncycastle.asn1.pkcs PrivateKeyInfo toASN1Primitive

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.pkcs PrivateKeyInfo toASN1Primitive.

Prototype

public ASN1Primitive toASN1Primitive() 

Source Link

Usage

From source file:com.github.autermann.sockets.ssl.SSLUtils.java

License:Apache License

private static PrivateKey createPrivateKey(PemObject privatePemObject)
        throws IOException, InvalidKeySpecException, NoSuchAlgorithmException {
    AlgorithmIdentifier algId = new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE);
    RSAPrivateKey instance = RSAPrivateKey.getInstance(privatePemObject.getContent());
    PrivateKeyInfo privateKeyInfo = new PrivateKeyInfo(algId, instance);
    return createKeyFromDER(privateKeyInfo.toASN1Primitive().getEncoded());
}

From source file:org.globus.gsi.bc.BouncyCastleOpenSSLKey.java

License:Apache License

protected PrivateKey getKey(String alg, byte[] data) throws GeneralSecurityException {
    if (alg.equals("RSA")) {
        try {/*from   w w w  .  ja va 2s.c  o m*/
            if (data.length == 0) {
                throw new GeneralSecurityException("Cannot process empty byte stream.");
            }
            ByteArrayInputStream bis = new ByteArrayInputStream(data);
            ASN1InputStream derin = new ASN1InputStream(bis);
            ASN1Primitive keyInfo = derin.readObject();

            ASN1ObjectIdentifier rsaOid = PKCSObjectIdentifiers.rsaEncryption;
            AlgorithmIdentifier rsa = new AlgorithmIdentifier(rsaOid);
            PrivateKeyInfo pkeyinfo = new PrivateKeyInfo(rsa, keyInfo);
            ASN1Primitive derkey = pkeyinfo.toASN1Primitive();
            byte[] keyData = BouncyCastleUtil.toByteArray(derkey);
            // The DER object needs to be mangled to
            // create a proper ProvateKeyInfo object
            PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyData);
            KeyFactory kfac = KeyFactory.getInstance("RSA");

            return kfac.generatePrivate(spec);
        } catch (IOException e) {
            // that should never happen
            return null;
        }

    } else {
        return null;
    }
}