Example usage for org.apache.commons.ssl PEMUtil decode

List of usage examples for org.apache.commons.ssl PEMUtil decode

Introduction

In this page you can find the example usage for org.apache.commons.ssl PEMUtil decode.

Prototype

public static List decode(byte[] pemBytes) 

Source Link

Usage

From source file:cl.nic.dte.extension.AutorizacionTypeExtensionHandler.java

@SuppressWarnings("unchecked")
public static PrivateKey getPrivateKey(AutorizacionType auth, char[] password)
        throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException,
        InvalidAlgorithmParameterException, IOException {

    List<PEMItem> items = PEMUtil.decode(auth.getRSASK().getBytes());

    for (PEMItem item : items) {
        if ("RSA PRIVATE KEY".equals(item.pemType)) {
            try {
                PKCS8Key pkcs8 = new PKCS8Key(item.getDerBytes(), password);

                return Utilities.readPrivateKey(pkcs8.getDecryptedBytes(), "RSA", password);
            } catch (GeneralSecurityException e) {
                throw new InvalidKeySpecException(e);
            }/*from  ww w .j  a va2  s .co m*/
        }
    }

    return null;
}

From source file:cl.nic.dte.extension.AutorizacionTypeExtensionHandler.java

@SuppressWarnings("unchecked")
public static PublicKey getPublicKey(AutorizacionType auth)
        throws InvalidKeySpecException, NoSuchAlgorithmException {
    List<PEMItem> items = PEMUtil.decode(auth.getRSAPUBK().getBytes());

    for (PEMItem item : items) {
        if ("PUBLIC KEY".equals(item.pemType)) {
            X509EncodedKeySpec enc;
            try {
                enc = new X509EncodedKeySpec(item.getDerBytes());
                KeyFactory rsaKeyFac;
                rsaKeyFac = KeyFactory.getInstance("RSA");
                return (PublicKey) rsaKeyFac.generatePublic((enc));
            } catch (GeneralSecurityException e) {
                throw new InvalidKeySpecException(e);
            }/* w w  w  .  j  a  v a2s.  c  om*/
        }
    }
    return null;
}