public static Collection<X509Certificate> parseCertificates(String pemRepresentation) throws IOException,
            CertificateException {

        // The parser is very picky. We should trim each line of the input string.
        final String pem = pemRepresentation //
                .replaceAll("(?m) +$", "") // remove trailing whitespace
                .replaceAll("(?m)^ +", ""); // remove leading whitespace

        ByteArrayInputStream input = new ByteArrayInputStream(pem.getBytes(StandardCharsets.UTF_8));
        return parseCertificates(input);
    }
Parses a PrivateKey instance from a PEM representation.

When the provided key is encrypted, the provided pass phrase is applied.

@param pemRepresentation a PEM representation of a private key (cannot be null or empty)
@param passPhrase optional pass phrase (must be present if the private key is encrypted).
@return a PrivateKey instance (never null)
@throws IOException if there was a problem parsing the key
xmppserver/src/main/java/org/jivesoftware/util/CertificateManager.java