File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Security and the fly likes Storing Keys in KeyStore Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Security
Bookmark "Storing Keys in KeyStore" Watch "Storing Keys in KeyStore" New topic

Storing Keys in KeyStore

Shariq Roomi
Ranch Hand

Joined: Oct 19, 2004
Posts: 31
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");
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);, password.toCharArray());

Can some one please help me with missing code.
Jan Cumps

Joined: Dec 20, 2006
Posts: 2565

Installing Providers.

You can also use the keytool utility to store certificates.

Regards, Jan

OCUP UML fundamental and ITIL foundation
Shariq Roomi
Ranch Hand

Joined: Oct 19, 2004
Posts: 31
Thanks Jan but I want to do programmatically.

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 ??
I agree. Here's the link:
subject: Storing Keys in KeyStore
It's not a secret anymore!