Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've a byte[] of certificate data and I want to change this byte[] type to Certificate type. How can I achieve this?

Now I 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 Error occurred Certificate certificate = cf.generateCertificate(bais); at this line.

Error : java.security.cert.CertificateParsingException: java.io.IOException: ObjectIdentifier() -- data isn't an object ID (tag = 49)

Why this error occurred? What wrong in this code? Please explain me. Thanks.

like image 344
Sharifah Avatar asked Oct 28 '25 05:10

Sharifah


1 Answers

You can use CertificateFactory.generateCertificate(InputStream inStream) to generate a Certificate.

Here is an example which generates an X509 Certificate:

CertificateFactory cf   = CertificateFactory.getInstance("X.509");
Certificate certificate = cf.generateCertificate(new ByteArrayInputStream(buf));
like image 60
Pau Kiat Wee Avatar answered Oct 29 '25 19:10

Pau Kiat Wee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!