如何获取公钥?

时间:2013-11-23 09:15:47

标签: java public-key-encryption

我正在尝试从以下内容获取公钥,但我无法检索。

-----BEGIN CERTIFICATE-----
MIIDfDCCAmSgAwIBAgIECmnOfzANBgkqhkiG9w0BAQUFADB2MQswCQYDVQQGEwJJ
TjESMBAGA1UECBMJS0FSTkFUQUtBMRIwEAYDVQQHEwlCYW5nYWxvcmUxDjAMBgNV
BAoTBVVJREFJMRQwEgYDVQQLEwtUZWNoIENlbnRlcjEZMBcGA1UEAxMQQXV0aCBU
ZXN0IFNlcnZlcjAeFw0xMTA5MjIwMDAwMDBaFw0xNTA5MjIwMDAwMDBaMHYxCzAJ
BgNVBAYTAklOMRIwEAYDVQQIEwlLQVJOQVRBS0ExEjAQBgNVBAcTCUJhbmdhbG9y
ZTEOMAwGA1UEChMFVUlEQUkxFDASBgNVBAsTC1RlY2ggQ2VudGVyMRkwFwYDVQQD
ExBBdXRoIFRlc3QgU2VydmVyMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC
AQEAxgVhHZZwTbiBMbu6zPzpNRfVp7ldubZvqPuKWT77UhR8pUUiBW7f19P+Ox6E
r6K6F/hoMWzz2k8geVEqFoRbVUVTfxxoxBkisQ82WCpbG0KkPNGyZ9i/56Aslq/b
wfSbyH6rfpAqLjFu9BXw4W+SY1zTKIjkxCjTh4FbqgzpFkGVlV3Al98ODrlS7uuy
X+qeqjvDZ3HQ+MzejrbNn6TQRBLaaqCVlkSzrzejXyEqaqliwVOVqNI3YPde7Mjl
4p6yGJ9T5Z4SGXSQXXP542kbxblcWn+inQvEmLCs3na841KflFRnFcMw27NDH1KQ
L8IZZByu56KE6BOIidcqoFF2aQIDAQABoxIwEDAOBgNVHQ8BAf8EBAMCBaAwDQYJ
KoZIhvcNAQEFBQADggEBAIFZ3r5wdZoKb8EOoP/hDnGotyxXJYgmUSmx4PNUPj0W
78eV4PTC8f9DPL2/DJUM7fvfsrO1M3R6fiTB+umrphEX9a6yX6K66KSWd8yaGouO
tX+ExkUNkEzmlS1ktKnhdSVwi0fkt8LBMjposZcz/0cPpjNFUyAyeyvSYbD2HusE
sbuMzHnSTFia5P5d32LtqD9tK7L3fS5d7NXa4Aod36JECqbKYyWInnWs3R3jFtKW
PGIA/V82oDQTvg9ey4fAvBrAUKfO3/UxF844k9cfCo1xrNxWv37wFuzuYbjA9w6Y
xiYWElRfRaEwChu+/2A2jYJ76lAOvbAyfILYNTtditM=
-----END CERTIFICATE-----

我正在使用以下代码。

CertificateFactory certFactory = CertificateFactory.getInstance(CERTIFICATE_TYPE, JCE_PROVIDER);

            fileInputStream = new FileInputStream(new File(publicKeyFileName));
            X509Certificate cert = (X509Certificate) certFactory.generateCertificate(fileInputStream);
            publicKey = cert.getPublicKey();
            certExpiryDate = cert.getNotAfter();

但是,如果我将证书提供为somename.cer文件,这只能起作用。

1 个答案:

答案 0 :(得分:4)

我刚下载了您的证书并在我当地进行了测试

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.security.PublicKey;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

public static void main(String[] args) throws FileNotFoundException, CertificateException {
         FileInputStream fin = new FileInputStream("C://Users/admin/Desktop/uidai_auth_stage.cer");
         CertificateFactory f = CertificateFactory.getInstance("X.509");
         X509Certificate certificate = (X509Certificate)f.generateCertificate(fin);
         PublicKey pk = certificate.getPublicKey();
         System.out.println(pk);
    }

输出

Sun RSA public key, 2048 bits
  modulus: 24997837043438188953607876466237786267538116823171823066632273818789601748586746440305433922940695151571735365523051675886995065116460565496861407875514773210269965513442232418015450789090428409276489545406694488823092308675874342532769927581914928367201662168201179961444037956523723476457691797490148453388604630304483861894406799761861931859139814624419421952118624143676117705606807289725313505871679662196624284199277807296317727434572557780473195825103383647653402128556252829571054599064686396724089686056490117328555759648852424450610766660552196304214260431235894942706902606159465251944070268033712979539561
  public exponent: 65537