Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Storing secret keys in a keystore

843811Mar 11 2007 — edited Mar 15 2007
Hello,

I want to store a secret key generated like this for example

KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
SecretKey myKey = keyGenerator.generateKey();
KeyStore.SecretKeyEntry skey = new KeyStore.SecretKeyEntry(myKey);

in a Java KeyStore. This key should be password protected like this

keystore.setEntry("secretKeyAlias", skey, new KeyStore.PasswordProtection("mypass".toCharArray()));

The exception I get is

java.security.KeyStoreException: Cannot store non-PrivateKeys

This seems to correspond to the information available at http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/keytool.html . But how can I protect a secret symmetric key with a password (it�s not absolutely necessary to store it in a Java KeyStore, the important point is the password protection)?

And why is there a �genseckey parameter for the keytool if it does not support non private keys? The exception mentioned above is the same when using keytool.

Thanks for your help. Dominik
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 12 2007
Added on Mar 11 2007
2 comments
470 views