Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > storing SecretKey in keystore

Reply
Thread Tools

storing SecretKey in keystore

 
 
jimgardener
Guest
Posts: n/a
 
      06-19-2008
hi
i created a keystore as below

public static void makeKeyStore(){
try{
KeyStore ks=KeyStore.getInstance(KeyStore.getDefaultType()) ;

ks.load(null,"".toCharArray());
FileOutputStream ksout=new FileOutputStream("myks.keystore");
char[] password = new char[] {'m','y','n','a','m','e'};
ks.store(ksout, password);
Arrays. fill(password, '\u0000' ) ;

}
catch(Exception e){
e.printStackTrace();
}

}

then i tried to store a generated key using an alias

public static void putEntriestoKS(){
try{
KeyStore ks=KeyStore.getInstance(KeyStore.getDefaultType()) ;;
FileInputStream fin=new FileInputStream("myks.keystore");
char[] password = new char[] {'m','y','n','a','m','e'};
ks.load(fin,password);
FileOutputStream fout=new FileOutputStream("myks.keystore");
KeyGenerator kg=KeyGenerator.getInstance("AES");
SecretKey skey=kg.generateKey();
ks.setKeyEntry("mysecretkey", skey, password,null);
ks.store(fout,password);
Arrays.fill(password,'\u0000');

}
catch(Exception e){
e.printStackTrace();
}
}



when i run this i am getting a java.security.KeyStoreException: Cannot
store non-PrivateKeys
How then can i store SecretKey ?Do i have to use another provider?can
someone explain?
thanks
Jim
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      06-20-2008
On Thu, 19 Jun 2008 07:08:12 -0700 (PDT), jimgardener
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
said :

>when i run this i am getting a java.security.KeyStoreException: Cannot
>store non-PrivateKeys
>How then can i store SecretKey ?Do i have to use another provider?can
>someone explain?
>thanks


You can extract the raw key bytes and store that. However it is then
totally unprotected. When you store things in a keystore, they have an
additional layer of encryption.

Just guessing here, but perhaps the problem surrounds providing a
password for the keystore file.

You might experiment creating the keystore with keytool and adding
your key to it rather than trying to create a keystore out of thin
air.

see http://mindprod.com/jgloss/keytool.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
 
subhasish.das@gmail.com
Guest
Posts: n/a
 
      06-25-2008
On Jun 20, 9:03*pm, Roedy Green <(E-Mail Removed)>
wrote:
> On Thu, 19 Jun 2008 07:08:12 -0700 (PDT), jimgardener
> <(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
> said :
>
> >when i run this i am getting a java.security.KeyStoreException: Cannot
> >store non-PrivateKeys
> >How then can *i store SecretKey ?Do i have to use another provider?can
> >someone explain?
> >thanks

>
> You can extract the raw key bytes and store that. *However it is then
> totally unprotected. When you store things in a keystore, they have an
> additional layer of encryption.
>
> Just guessing here, but perhaps the problem surrounds providing a
> password for the keystore file.
>
> You might experiment creating the keystore with keytool and adding
> your key to it rather than trying to create a keystore out of thin
> air.
>
> seehttp://mindprod.com/jgloss/keytool.html
> --
>
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com


This error normally comes up when you have not specified the keystore
type as JCEKS. The default value is JKS but Secret Keys require JCEKS
so you have to explicitly specify that.

-- SD
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How and where to store a SecretKey David Segall Java 3 01-27-2008 02:53 AM
Hardcode keystore into java program Fritz Bayer Java 1 10-25-2004 07:54 AM
KeyStore and PublicKey Rogue Chameleon Java 3 09-27-2004 05:43 PM
lost the key store password, can create new keystore? Andrew Thompson Java 5 12-12-2003 04:07 AM
JSSE -- SSL with client authentication and keystore with multiplecerts John Salvo Java 3 09-09-2003 01:04 AM



Advertisments