Get public Key from imported certificate in Keystore with Java

爱⌒轻易说出口 提交于 2019-12-04 16:00:51

To get the certificates associated with a private key, you should be calling getCertificateChain(), and using the zeroth element of the returned array. Not getCertificate().

What about

keystore.load(is, passwd);
PublicKey pubKey = keystore.getCertificate(alias).getPublicKey();
String publicKeyString = Base64.encodeBase64String(pubKey.getEncoded());

Edit

After being downvoted, here some more detail how I see it:

The OP provided this link: https://docs.jboss.org/author/display/PLINK/Picketlink+as+SP,+Salesforce+as+IDP?_sscc=t where it says:

After certificate will be generated in Salesforce, you can download it to your computer.

and

This certificate will be used to sign SAMLResponse messages sent from Salesforce IDP.

Type self-signed

after that, OP is told to import that cert, from which he would now like to retrieve the public key:

keytool -import -file salesforce_idp_cert.cer -keystore jbid_test_keystore.jks -alias salesforce-idp

So it seems obvious that

  • OP does not have the private key for that cert
  • There is no chain for this cert
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!