Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining public key from certificate

I'm trying to obtain the public key of a Certificate using the method:

FileInputStream fin = new FileInputStream("PathToCertificate"); CertificateFactory f = CertificateFactory.getInstance("X.509"); X509Certificate certificate = (X509Certificate)f.generateCertificate(fin); PublicKey pk = certificate.getPublicKey(); 

but I receive the following error:

Exception in thread "main" java.lang.ClassCastException: sun.security.x509.X509CertImpl cannot be cast to codec.x509.X509Certificate         at sergas_testcertificates.Main.main(Main.java:54) 

Does anyone know what this error is about?

Thanks in advance

like image 265
javi Avatar asked Jun 15 '11 13:06

javi


1 Answers

You have the wrong class imported for X509Certificate.

You are likely looking for java.security.cert.X509Certificate not codec.x509.X509Certificate.

like image 57
Dan Avatar answered Oct 13 '22 00:10

Dan