javax.security.cert.X509Certificate vs java.securi

2020-08-09 06:32发布

问题:

Did you spot the difference in the title? (for me, it took some time)

So I used bouncy castle with connection to java.security.cert.X509Certificate in order to create certificates. Now I'm looking for a way to create a java.security.cert.X509Certificate from raw bytes. I haven't any method to do this in java.security.cert.X509Certificate but there is one(the getInstance static method) in javax.security.cert.X509Certificate.

The problem is I cannot cast a javax.security.cert.X509Certificate to java.security.cert.X509Certificate.

Any ideas on what to do, to transform raw bytes into a java.security.cert.X509Certificate?

Also, what's the differences between the one abstract class and the other class? Why does java has two of them with different functionality?

回答1:

The javax version is deprecated. Use CertificateFactory to generate a certificate from raw bytes. There is an example in the javadocs.



回答2:

This happened for me because I am using j2se but imported javax classes. Certificate and X509Certficate classes exists both in J2SE and in J2EE.

Imports in J2SE - Not javax.security...

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

import java.security.cert.Certificate;

Not these.

import javax.security.cert.X509Certificate;