How to get the public key? [closed]

2019-09-30 10:53发布

问题:

I'm trying to get the public key from the following but I'm unable to retrieve.

-----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-----

I'm using the following code.

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();

But this only work, if I provide the certificate as somename.cer file.

回答1:

I just downloaded your certificate and tested in my local

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);
    }

Output

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