Convert certificate [] byte in X509Certificate in Java

I saved the Base64 encoded certificate in the database. I can read this from the database and decode, but I would like to convert the decoded byte [] to the X509 certificate. I am looking for sample code for this. I tried:

CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); InputStream in = new ByteArrayInputStream(bytes); X509Certificate cert = (X509Certificate)certFactory.generateCertificate(in); 

This leads to a problem with the encoding type. A valid String or byte [] pattern that represents the certificate would be good.

Any pointers would be great! Thanks.

+6
source share
1 answer

Your sample code looks great. You can create this byte[] yourself using openssl:

 openssl genrsa -out privkey.pem openssl req -new -x509 -key privkey.pem -outform DER -out cert.der 
0
source

Source: https://habr.com/ru/post/920648/


All Articles