0

The public key is in .csr and private key is in .key extension. The exception I receive is

Exception in thread "main" java.security.cert.CertificateParsingException: java.io.IOException: 
ObjectIdentifier() -- data isn't an object ID (tag = 49)
at sun.security.x509.X509CertInfo.<init>(Unknown Source)
at sun.security.x509.X509CertImpl.parse(Unknown Source)
at sun.security.x509.X509CertImpl.<init>(Unknown Source)
at sun.security.provider.X509Factory.engineGenerateCertificate(Unknown Source)
at java.security.cert.CertificateFactory.generateCertificate(Unknown Source)
at com.ebao.gimo.integration.security.RSAEnc.getPublicKey(RSAEnc.java:208)
at com.ebao.gimo.integration.security.RSAEnc.main(RSAEnc.java:37)
Caused by: java.io.IOException: ObjectIdentifier() -- data isn't an object ID (tag = 49)
at sun.security.util.ObjectIdentifier.<init>(Unknown Source)
at sun.security.util.DerInputStream.getOID(Unknown Source)
at sun.security.x509.AlgorithmId.parse(Unknown Source)
at sun.security.x509.CertificateAlgorithmId.<init>(Unknown Source)
at sun.security.x509.X509CertInfo.parse(Unknown Source)

The code I have tried is :

    public static PublicKey getPublicKey(String fileName) throws Exception {
    FileInputStream fis = new FileInputStream(fileName);
    CertificateFactory cf = CertificateFactory.getInstance("X509");
    X509Certificate xCert = (X509Certificate)cf.generateCertificate(fis);
    PublicKey pubKeyVal = xCert.getPublicKey();
    return pubKeyVal;
  }

Reading Private Key

    public static Key getPrivateKey(String filename) throws Exception {
    PemReader pemReader = new PemReader(new FileReader(filename));
    PemObject pemObject = pemReader.readPemObject();
    byte[] der = pemObject.getContent();
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(der);
    RSAPrivateKey privKey = (RSAPrivateKey)keyFactory.generatePrivate(ks);
    return privKey;
  }

Kindly help The public key is

-----BEGIN CERTIFICATE REQUEST-----
MIICwDCCAagCAQAwezELMAkGA1UEBhMCSU4xFDASBgNVBAgMC01haGFyYXNodHJh
MQ8wDQYDVQQHDAZNdW1iYWkxHzAdBgNVBAoMFkViYW90ZWNoIEluZGlhIFB2dCBM
dGQxJDAiBgNVBAsMG0luZGlhIENsb3VkIFJlZmluZW1lbnQgU0JJRzCCASIwDQYJ
KoZIhvcNAQEBBQADggEPADCCAQoCggEBALhcoHMrt4QroPYeIyr0oYiE1Kjrs7xo
L5eryiOVgJp4ddWGtKy9vVjJJcDhs5D+d78d9wu/4u9ET2LtgOX/99JhbPz6UuVX
UP0vdrfVyWJvbwCrfWPUzW2/vmeP0wXIISzvXghzy/5DhRYfaOlhRHGDl0lRvHgU
DxIWZtl9sNoWO3CRZAO0D5QX0Cq/S+uc9WPXFchdve32DsQ+YFMoBYwli8uH//rB
ZFc95oHo1WEHCrCHHNE6EAgFG/zgBGyEqgSMfOBVt28r89lgZCIoM5zAuVvI/VM2
ROy8pOMoGKNfXlg8rYq/1OljQ1XIIOt/Ir/0T5YDcs771SgNTiVCUicCAwEAAaAA
MA0GCSqGSIb3DQEBBQUAA4IBAQBrz0KUgI3CG8xFVVtiqDOeTqutrvOoRRz5ziiE
uMeGrkN7jlF/EyurReO0TIGzYiQbVnl/XKOhpKIPf8EKI8nN/Idr2dA8z9NrH9gM
Iat6wuSACC6Txb+RbGSYo66FAaJZQU1OJTFtfIP7LfM9mZPA2gi3aKb0sM+VuCph
WpMm0Kjbp9m665hRbJ//nck+os2CxWhRTyuxRbK007IDi/4FNMnlV/2cxMi644m/
++hbFoF0ihZzq+npezh7URU0Oj9aW7YBVXy9110XBX8JfgOJ5pfZxjU6ID+HQdi/
SciHqqv15tKsxxlKOi8Ju2y3g8vW5dcPJOS4/G5QsQqZsPn9
-----END CERTIFICATE REQUEST-----

Let me know if the extension of key affects it

Saii
  • 3
  • 1
  • 4
  • 3
    Does this answer your question? [Got "data isn't an object ID (tag = 49)" while generating X509 cert](https://stackoverflow.com/questions/41512915/got-data-isnt-an-object-id-tag-49-while-generating-x509-cert) – Philippe B. Sep 22 '20 at 14:05
  • 1
    As you printed out "...CERTIFICATE REQUEST..." you do not have a Public Key nor a Certificate nor a Private Key. This file is "only" useful to get signed by a Certification Agency - they will check your identity and return a Certificate that is signed by this agency, so that others get an "identity proved" certificate. On the other hand you cannot do anything else with this certification request file. – Michael Fehr Sep 22 '20 at 15:21

1 Answers1

0

The issue was how the keys were created. Once my keys were exported to .der format the code worked. Below link helped me out. Load RSA public key from file

Saii
  • 3
  • 1
  • 4