Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decoding certificate in Java by DER algorithm

I have to open cert file in my Java class. Certyficates are encoded by DER algorithm. How can i decode this file ?

I upload certyficate to my servlet, this way

InputStream in = getResourceAsStream("/certyficate.cer");
BufferedReader br = new BufferedReader(new InputStreamReader(in))

now i have to decode this file, how can i do that ?

Now i have have trouble with, get.Instance()

enter image description here

i used it exactly how it is in documentation, but i have an error like in this screenshot

how can i fix that ?

like image 778
Empi Avatar asked Oct 31 '25 01:10

Empi


1 Answers

Given your InputStream in containing the certificate, you can decode the certificate through java.security.cert.CertificateFactory. DER and PEM encodings are supported.

Imports you will need:

import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

To decode the certificate:

try {
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509Certificate cert = (X509Certificate) cf.generateCertificate(in);
} catch (CertificateException e) {
    // handle failure to decode certificate
}
like image 54
frasertweedale Avatar answered Nov 02 '25 17:11

frasertweedale



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!