6

I want to take a public key .cer file generated from java keytool command like this:

"keytool -export -alias privatekey -file publickey.cer -keystore privateKeys.store"

and import it into a new, empty java keystore like this:

"keytool -import -alias publiccert -file publickey.cer -keystore publicCerts.store"

except I want to do the import programmatically, using JSSE.

Stack Overlords, work your magic! Thanks!

J P
  • 61
  • 1
  • 2
  • JSSE may not be the right acronym. JCE perhaps? Whatever the Java API that deals with keystores is called! :) – J P Jun 17 '09 at 00:20
  • Thanks guys. I would upvote you but I don't have enough credit to do so yet. Following Reginaldo's advice, I figured it out after looking at the decompiled KeyTool source. I did something similar to this: Certificate cert = CertificateFactory.getInstance("X509").generateCertificate(new FileInputStream(certFileLocation)); keyStore.setCertificateEntry("publickey", cert); – J P Jun 18 '09 at 00:27

2 Answers2

2

Look at the KeyStore class in Java. Here is a class which might give you some hints. You might require the free BouncyCastle crypto provider to operate all of its function

akarnokd
  • 69,132
  • 14
  • 157
  • 192
0

If you use jad to decompile the tools.jar that comes with Sun JDK, you will see exactly how it works.

Unzip the tools.jar in a folder and use the following command to decompile all classes inside that jar.

for i in $(find . -name "*.class" | perl -ple 's/\.class$//g'); do jad -p $i.class > $i.java; done

The keytool source can be seen in class sun.security.tools.KeyTool

Reginaldo
  • 897
  • 5
  • 12