- 更新
正如
dave_thompson_085评论中提到的那样,OP解决方案唯一的问题是OP
KeyFactory使用的是密钥,而不是
CertificateFactory证书。
这是 无需base64转换 的代码 , 因为
CertificateFactory它本身可以读取PEM文件(它会查找
BEGIN/ENDCERTIFICATE块本身以知道它正在读取PEM文件)。
import java.io.ByteArrayInputStream;import java.io.InputStream;import java.security.PublicKey;import java.security.cert.CertificateFactory;import java.security.cert.X509Certificate;class LoadKey { public static void main(String[] args) throws Exception { String cert = "-----BEGIN CERTIFICATE-----n" +"MIIDITCCAoqgAwIBAgIQT52W2WawmStUwpV8tBV9TTANBgkqhkiG9w0BAQUFADBMn" +"MQswCQYDVQQGEwJaQTElMCMGA1UEChMcVGhhd3RlIENvbnN1bHRpbmcgKFB0eSkgn" +"THRkLjEWMBQGA1UEAxMNVGhhd3RlIFNHQyBDQTAeFw0xMTEwMjYwMDAwMDBaFw0xn" +"MzA5MzAyMzU5NTlaMGgxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhn" +"MRYwFAYDVQQHFA1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKFApHb29nbGUgSW5jMRcwn" +"FQYDVQQDFA53d3cuZ29vZ2xlLmNvbTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCn" +"gYEA3rcmQ6aZhc04pxUJuc8PycNVjIjujI0oJyRLKl6g2Bb6YRhLz21ggNM1QDJyn" +"wI8S2OVOj7my9tkVXlqGMaO6hqpryNlxjMzNJxMenUJdOPanrO/6YvMYgdQkRn8Bn" +"d3zGKokUmbuYOR2oGfs5AER9G5RqeC1prcB6LPrQ2iASmNMCAwEAAaOB5zCB5DAMn" +"BgNVHRMBAf8EAjAAMDYGA1UdHwQvMC0wK6ApoCeGJWh0dHA6Ly9jcmwudGhhd3Rln" +"LmNvbS9UaGF3dGVTR0NDQS5jcmwwKAYDVR0lBCEwHwYIKwYBBQUHAwEGCCsGAQUFn" +"BwMCBglghkgBhvhCBAEwcgYIKwYBBQUHAQEEZjBkMCIGCCsGAQUFBzABhhZodHRwn" +"Oi8vb2NzcC50aGF3dGUuY29tMD4GCCsGAQUFBzAChjJodHRwOi8vd3d3LnRoYXd0n" +"ZS5jb20vcmVwb3NpdG9yeS9UaGF3dGVfU0dDX0NBLmNydDANBgkqhkiG9w0BAQUFn" +"AAOBgQAhrNWuyjSJWsKrUtKyNGadeqvu5nzVfsJcKLt0AMkQH0IT/GmKHiSgAgDpn" +"ulvKGQSy068Bsn5fFNum21K5mvMSf3yinDtvmX3qUA12IxL/92ZzKbeVCq3Yi7Len" +"IOkKcGQRCMha8X2e7GmlpdWC1ycenlbN0nbVeSv3JUMcafC4+Q==n" +"-----END CERTIFICATE-----n"; System.out.println(cert); byte[] certBytes = cert.getBytes(java.nio.charset.StandardCharsets.UTF_8); CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); InputStream in = new ByteArrayInputStream(certBytes); X509Certificate certificate = (X509Certificate)certFactory.generateCertificate(in); System.out.println("Subject DN : " + certificate.getSubjectDN().getName()); System.out.println("Issuer : " + certificate.getIssuerDN().getName()); System.out.println("Not After: " + certificate.getNotAfter()); System.out.println("Not Before: " + certificate.getNotBefore()); System.out.println("version: " + certificate.getVersion()); System.out.println("serial number : " + certificate.getSerialNumber()); PublicKey publicKey = certificate.getPublicKey(); System.out.println("PublicKey : n" + publicKey); }}
您缺少base64解码部分。 见上面的评论
这是工作代码,您需要base64类的Apache Commons
Codec库。
注意: 检查上面的代码,上面的base64步骤是由自动完成的
CertificateFactory,无需手动进行。
import java.io.ByteArrayInputStream;import java.io.InputStream;import java.security.PublicKey;import java.security.cert.CertificateFactory;import java.security.cert.X509Certificate;import org.apache.commons.prec.binary.base64;class LoadKey { public static void main(String[] args) throws Exception { String cert = "-----BEGIN CERTIFICATE-----n" +"MIIDITCCAoqgAwIBAgIQT52W2WawmStUwpV8tBV9TTANBgkqhkiG9w0BAQUFADBMn" +"MQswCQYDVQQGEwJaQTElMCMGA1UEChMcVGhhd3RlIENvbnN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-----BEGIN CERTIFICATE-----n", ""); cert = cert.replace("-----END CERTIFICATE-----n", ""); System.out.println(cert); byte[] enpredCert = cert.getBytes("UTF-8"); byte[] depredCert = base64.deprebase64(enpredCert); CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); InputStream in = new ByteArrayInputStream(depredCert); X509Certificate certificate = (X509Certificate)certFactory.generateCertificate(in); System.out.println("Subject DN : " + certificate.getSubjectDN().getName()); System.out.println("Issuer : " + certificate.getIssuerDN().getName()); System.out.println("Not After: " + certificate.getNotAfter()); System.out.println("Not Before: " + certificate.getNotBefore()); System.out.println("version: " + certificate.getVersion()); System.out.println("serial number : " + certificate.getSerialNumber()); PublicKey publicKey = certificate.getPublicKey(); System.out.println("PublicKey : n" + publicKey); }}
输出量
MIIDITCCAoqgAwIBAgIQT52W2WawmStUwpV8tBV9TTANBgkqhkiG9w0BAQUFADBMMQswCQYDVQQGEwJaQTElMCMGA1UEChMcVGhhd3RlIENvbnN1bHRpbmcgKFB0eSkgTHRkLjEWMBQGA1UEAxMNVGhhd3RlIFNHQyBDQTAeFw0xMTEwMjYwMDAwMDBaFw0xMzA5MzAyMzU5NTlaMGgxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHFA1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKFApHb29nbGUgSW5jMRcwFQYDVQQDFA53d3cuZ29vZ2xlLmNvbTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA3rcmQ6aZhc04pxUJuc8PycNVjIjujI0oJyRLKl6g2Bb6YRhLz21ggNM1QDJywI8S2OVOj7my9tkVXlqGMaO6hqpryNlxjMzNJxMenUJdOPanrO/6YvMYgdQkRn8Bd3zGKokUmbuYOR2oGfs5AER9G5RqeC1prcB6LPrQ2iASmNMCAwEAAaOB5zCB5DAMBgNVHRMBAf8EAjAAMDYGA1UdHwQvMC0wK6ApoCeGJWh0dHA6Ly9jcmwudGhhd3RlLmNvbS9UaGF3dGVTR0NDQS5jcmwwKAYDVR0lBCEwHwYIKwYBBQUHAwEGCCsGAQUFBwMCBglghkgBhvhCBAEwcgYIKwYBBQUHAQEEZjBkMCIGCCsGAQUFBzABhhZodHRwOi8vb2NzcC50aGF3dGUuY29tMD4GCCsGAQUFBzAChjJodHRwOi8vd3d3LnRoYXd0ZS5jb20vcmVwb3NpdG9yeS9UaGF3dGVfU0dDX0NBLmNydDANBgkqhkiG9w0BAQUFAAOBgQAhrNWuyjSJWsKrUtKyNGadeqvu5nzVfsJcKLt0AMkQH0IT/GmKHiSgAgDpulvKGQSy068Bsn5fFNum21K5mvMSf3yinDtvmX3qUA12IxL/92ZzKbeVCq3Yi7LeIOkKcGQRCMha8X2e7GmlpdWC1ycenlbN0nbVeSv3JUMcafC4+Q==Subject DN : CN=www.google.com, O=Google Inc, L=Mountain View, ST=California, C=USIssuer : CN=Thawte SGC CA, O=Thawte Consulting (Pty) Ltd., C=ZANot After: Mon Sep 30 19:59:59 EDT 2013Not Before: Tue Oct 25 20:00:00 EDT 2011version: 3serial number : 105827261859531100510423749949966875981PublicKey : Sun RSA public key, 1024 bits modulus: 156396091895984667473837837332877995558144703880815901117439532534031286131520903863087599986938779606924811933611903716377206837300122262900786662124968110191717844999183338594373129421417536020806373385428322642107305024162536996222164292639147591878860587271770855626780464602884552232097424473091745159379 public exponent: 65537
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)