Bootstrap FreeKB - Java - get Private Key
Java - get Private Key

Updated:   |  Java articles

Create a Dynamic Web Project in Eclipse. Let's say the name of the project is SSL. 

In the left panel of Eclipse, at the name of your project (SSL in this example) > Java Resources > src, create a new package. For the sake of this tutorial, let's name the package com.ssl.demo.

In the package, create a class called SSL.java. In SSL.java, add the following markup

package com.ssl.demo;

import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.KeyStore.*;

public class SSL {

	public PrivateKey getPrivateKey() throws Exception {
	  String alias    = "foo-certificate";
	  char[] password = "itsasecret".toCharArray();
	  
	  KeyStore keystore = KeyStore.getInstance("PKCS12");
	  keystore.load(new FileInputStream("C:\\Users\\john.doe\\eclipse-workspace\\keystore.p12"), pwdArray);

	  PrivateKeyEntry entry = (PrivateKeyEntry)keystore.getEntry(alias, new PasswordProtection(password.toCharArray()));
	  
	  System.out.println("entry.getPrivateKey = " + entry.getPrivateKey());
	 	  
	  return entry.getPrivateKey();
	}
}

 

In the left panel of Eclipse, right click on the name of your project (demo in this example) and select New JSP File. Select the WebContent folder, give the file a name such as index.jsp, and select Finish.

Add the following to line 1 of the JSP file. This is the combination of the package (com.ssl.demo) and the class (SSL.java).

<%@page import="com.ssl.demo.SSL"%>

 

Add the following inside the <body> tags.

  • In this example, "SSL" must be used because the name of the class in SSL.java is SSL.
  • In this example, "getPrivateKey" must be cause because the name of the method in SSL.java is getPrivateKey.
<body>
<%
  SSL foo = new SSL();
  out.println(foo.getPrivateKey());
%>
</body>

 

Select the green play button in Eclipse, and hopefully something like this is returned.

SunRsaSign RSA private CRT key, 2048 bitsparams: nullmodulus:
012345678901234567890123456789...

private exponent:
012345678901234567890123456789...

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter b92658 in the box below so that we can be sure you are a human.