• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get the private key from a self signed certificate in java  RSS feed

 
Ravi Danum
Ranch Hand
Posts: 154
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello,

I need to create a SOAP request header, in java, that requires a certificate and a private key.

I have created a self-signed certificate as follows:

keytool -genkey -keyalg RSA -alias selfsigned -keystore mykeystore.jks -storepass password -validity 360 -keysize 2048

Now that I've created it, I want to write a java program to read the certificate from the keystore, and get the password from the keystore. I will base64 encode the certificate and put it in the <BinarySecurityToken> of the request header. I will use the private key to compute the signature value in the Signature element of the header.

Does anyone know how I can, in java, retrieve the certificate and the private key from the keystore?

Thanks so much.

-Ravi

 
Ravi Danum
Ranch Hand
Posts: 154
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have found that this should work:


InputStream path = new FileInputStream(System.getProperty("javax.net.ssl.keyStore"));
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(path, System.getProperty("javax.net.ssl.keyStorePassword").toCharArray());
PrivateKey privateKey = (PrivateKey) ks.getKey("selfsigned", System.getProperty("javax.net.ssl.keyStorePassword").toCharArray());

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!