Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Security / February 2005

Tip: Looking for answers? Try searching our database.

How to use the getEncoded() value from keystore?

Thread view: 
jacksu - 04 Feb 2005 22:43 GMT
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 - 05 Feb 2005 17:38 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.