How to convert byte type [] to certificate type in java?

I have byte[]certificate data and I want to change this type byte[]to Certificate. How can I achieve this?

Now I have used CertificateFactory.generateCertificate(InputStream inStream).

byte[] csr = getCSR(cn, ou, org, loc, state, country,email);
CertificateFactory  cf = CertificateFactory.getInstance("X.509");
ByteArrayInputStream bais = new ByteArrayInputStream(csr);
Certificate certificate = cf.generateCertificate(bais);

But there was an error Certificate Certificate = cf.generateCertificate (bais); in this line.

Error: java.security.cert.CertificateParsingException: java.io.IOException: ObjectIdentifier () - the data is not an object identifier (tag = 49)

Why did this error happen? What is wrong with this code? Please explain me. Thank.

+3
source share
2 answers

You can use CertificateFactory.generateCertificate(InputStream inStream)to create a certificate.

, X509:

CertificateFactory cf   = CertificateFactory.getInstance("X.509");
Certificate certificate = cf.generateCertificate(new ByteArrayInputStream(buf));
+7

, CertificateFactory.generateCertificate CSR. , ; .. , . ( ), ( http://www.journaldev.com/223/generating-a-certificate-signing-request-using-java-api):

import sun.security.pkcs.PKCS10;
...
PKCS10 csr = new PKCS10(Base64Utils.decode(csrPem1.getBytes()));

Base64Utils .

+2

All Articles