当前位置: 动力学知识库 > 问答 > 编程问答 >

Converting certificate byte[] to X509Certificate in Java

问题描述:

I've stored a certificate as a Base64 encoded string in the database. I can read this from the the database and decode it but I'd like to convert the decoded byte[] into a X509Certificate. I am looking for sample code to do this. I have tried:

CertificateFactory certFactory = CertificateFactory.getInstance("X.509");

InputStream in = new ByteArrayInputStream(bytes);

X509Certificate cert = (X509Certificate)certFactory.generateCertificate(in);

This leads to an issue with the encoding type. A valid sample String or byte[] that represents a certificate will be good.

Any pointers will be great! Thanks.

网友答案:

Your sample code looks fine. You can generate that byte[] yourself using openssl:

openssl genrsa -out privkey.pem
openssl req -new -x509 -key privkey.pem -outform DER -out cert.der
分享给朋友:
您可能感兴趣的文章:
随机阅读: