Skip to content

Instantly share code, notes, and snippets.

@alastairmccormack
Created October 17, 2017 15:42
Show Gist options
  • Save alastairmccormack/80b6f9e5c13b3b7db490dd57880f8a22 to your computer and use it in GitHub Desktop.
Save alastairmccormack/80b6f9e5c13b3b7db490dd57880f8a22 to your computer and use it in GitHub Desktop.
PEM formatted certificate to java.security.cert.X509Certificate
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.openssl.PEMParser;
import java.security.cert.X509Certificate;
import java.io.FileReader;
import java.io.IOException;
import java.security.cert.CertificateException;
public class PEM2X509Certificate {
public X509Certificate loadCert(String fileName) throws IOException, CertificateException {
FileReader reader = new FileReader(fileName);
PEMParser pemParser = new PEMParser(reader);
X509CertificateHolder x509CertificateHolder = (X509CertificateHolder) pemParser.readObject();
X509Certificate x509Certificate = new JcaX509CertificateConverter().getCertificate( x509CertificateHolder );
return x509Certificate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment