class CertUtils {

    public static KeyPair readKeyPairFromPemFile(String filename)
            throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
        File pkFile = new File(filename);
        try (PEMParser pemParser = new PEMParser(new FileReader(pkFile))) {
            Object o = pemParser.readObject();
            if (o == null || !(o instanceof PrivateKeyInfo)) {
                throw new CodedException(X_INTERNAL_ERROR,
                        "Could not read key from '%s'", filename);
            }
            PrivateKeyInfo pki = (PrivateKeyInfo) o;
            KeyFactory kf = KeyFactory.getInstance("RSA");
            final PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(pki.getEncoded());
            final PrivateKey privateKey = kf.generatePrivate(ks);
            final RSAPrivateKey rpk = RSAPrivateKey.getInstance(pki.parsePrivateKey());
            final PublicKey publicKey = kf.generatePublic(new RSAPublicKeySpec(rpk.getModulus(),
                    rpk.getPublicExponent()));
            KeyPair kp = new KeyPair(publicKey, privateKey);
            return kp;
        }
    }

    private  CertUtils();

}