Storing secret keys in a keystore
843811Mar 11 2007 — edited Mar 15 2007Hello,
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