0

I'm working on project which collect data from some government portal.

To obtain valid data I had to make request containing some exact xml data. The xml data had also to contain public key of my certificate. The the portal then encrypts
the returned data. Having private key of the certificate I am then able to decrypt returned data. So far so good, it's working. But how am I getting the public key of my certificate for the xml? So far manually. I found the certificate among other personal ones in Chrome browser. Exported it into file, without private key, X.509, coding Base-64 (CER). Then opened the exported file in the text editor, removed '-----BEGIN CERTIFICATE-----' and '----END CERTIFICATE-----''and the rest put into XML

This all I need to repeat by java code (BouncyCastle library ?). I think it will be easy, but the examples are scarce.

Thanks.

  • 1
    This is a pretty common request, if I'm reading your question correctly -- you want programmatic secure access to an endpoint using the endpoint's certs. However, you really ought to just to do some research and give it a try, because SO really wants to see your coding attempt before helping you. So is not a great place for tutorials, but if you search SO you will find at least a start to solving your problem. Here's a start, I think: https://stackoverflow.com/q/4325263/437212 –  Mar 12 '20 at 15:14

1 Answers1

0

I finally googled the solution, the class JcaPEMWriter (BouncyCastle library) did the trick. Now the output is the same as exported manually.

public static String convertCertificateToPEM(X509Certificate signedCertificate) throws IOException {
    StringWriter signedCertificatePEMDataStringWriter = new StringWriter();
    JcaPEMWriter pemWriter                            = new JcaPEMWriter(signedCertificatePEMDataStringWriter);
    pemWriter.writeObject(signedCertificate);
    pemWriter.close();
    return signedCertificatePEMDataStringWriter.toString();
}
mkl
  • 90,588
  • 15
  • 125
  • 265