How to Store Private/Public Key in Keystore

I can not simply make sense of this KeySTore Class.

I have private and public key and I simply want to use them in KeyStore to use later.

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");

SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN"); java.security.NoSuchProviderException

keyGen.initialize(512 , random);

KeyPair keypair = keyGen.genKeyPair();

PrivateKey priKey = keypair.getPrivate();

PublicKey pubKey = keypair.getPublic();

File f = new File("c:\keystore");

// Create an empty keystore object

KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());

?

// code to store private and public key in keystore

// Save the new keystore contents

FileOutputStream out = new FileOutputStream(keystoreFile);

keystore.store(out, password.toCharArray());

out.close();

Can some one please help me with missing code.

[959 byte] By [namon20a] at [2007-11-27 4:07:47]
# 1

There is a function in KeySTore class

setKeyEntry(String alias, Key key, char[] password, Certificate[] chain)

Assigns the given key to the given alias, protecting it with the given password.

So I am wondering hwo could I get the corresponding Certficate array for my privatekey ?

namon20a at 2007-7-12 9:13:05 > top of Java-index,Security,Other Security APIs, Tools, and Issues...
# 2

If you don't mind coding to Sun's internal classes (sun.security.*), check out the source of the keytool command line tool. See here, too:

http://forum.java.sun.com/thread.jspa?threadID=5164712

Of course, you can always generate the JKS keystore file with keytool, create a key pair and a self-signed certificate and continue from there.

Regards,

Anestis

mrAnesta at 2007-7-12 9:13:05 > top of Java-index,Security,Other Security APIs, Tools, and Issues...