For Programmers: Free Programming Magazines  


Home > Archive > Java Security > February 2005 > How to use the getEncoded() value from keystore?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to use the getEncoded() value from keystore?
jacksu

2005-02-04, 8:58 pm

Hi all,

I get a key from jks keystore. Code looks like

Key key = keystore.getKey("mykey", "mypwd".toCharArray());
System.out.println(key);
byte keyBuf[] = key.getEncoded();

Then how could I use keyBuf to regenerate the same key in another
keystore?

It appears that the key from export is:
RSAMultiPrimePrivateCrtKeySpec


Thanks in advance.

Mr. Skeptic

2005-02-05, 4:00 pm

Look at key.getFormat() to see what encoding is used. I'm guessing it
is PKCS8 encoding, but if it is not just make some minor modification
to the code fragment below.
Also, you should now what the key algorithm is; you can get this from
the getAlgorithm() method, i.e. String alg = key.getAlgorithm();

Here is one method to recover the key.

KeyFactory kf = KeyFactory.getInstance(alg);
KeySpec ks = new PKCS8EncodedKeySpec(keyBuf);
Key newkey = kf.generatePrivate(ks);
if (kf instanceof RSAMultiPrimePrivateCrtKeySpec)
System.out.println("Sucess");
else
System.out.println("failure");



// newkey should be the same as key.

I haven't actually tried this so I don't know if it will work.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com