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 / General / July 2005

Tip: Looking for answers? Try searching our database.

Crypo API: Reading key from file

Thread view: 
Uwe Seimet - 26 Jul 2005 10:31 GMT
Hi,

how do I convert a byte[] which contains a private RSA key to an
instance of java.security.PrivateKey?
The byte[] was obtained with PrivateKey.getEncoded(), and I would expect
a complementary API function to convert the byte[] back to a PrivateKey,
but I don't find anything. Am I missing something? How is this done?

Best regards,  Uwe

Signature

-----------------------------------------------------------------------
  Dr. Uwe Seimet                                 http://www.seimet.de

iksrazal@terra.com.br - 26 Jul 2005 12:18 GMT
I use getEncoded() on X509Certificate, store it in LDAP, and convert it
back. Should work the same way, here some code. Note that LDAP
ctx.lookup returns Object, in my case 'bin' and I just convert back to
byte array, as I store it.

X509Certificate cert = null;
   try
   {
     // retrieve as binary object
     Object bin = doLookup(results, "userCertificate;binary");
     if (null==bin)
     {
       throw new IllegalStateException("Search results do not contain
X509 Certificate");
     }
     // convert to byte array
     byte[] buf = (byte[])bin;
     if (1 >= buf.length)
     {
       throw new IllegalStateException("Illegal certificate size");
     }
     // convert to X.509 class
     CertificateFactory cf = CertificateFactory.getInstance( "X.509"
);
     ByteArrayInputStream bais = new ByteArrayInputStream(buf);
     while (bais.available() > 0)
     {
       cert = (X509Certificate) cf.generateCertificate( bais );
     }
   }//end try

HTH,
iksrazal
http://www.braziloutsource.com/
iksrazal@terra.com.br - 26 Jul 2005 12:36 GMT
Thinking about this a bit more, I can't find a PrivateKeyFactory,
although my guess is that if you have ByteArrayInputStream, there may
be something that creates PrivateKey. I use KeyStore to generate my
PrivateKey's, and store those in LDAP as in this case a Serialized
Object.

If you find out how, could you post it? I'm curious.
iksrazal

iksrazal
Uwe Seimet - 26 Jul 2005 13:52 GMT
> I use getEncoded() on X509Certificate, store it in LDAP, and convert it
> back. Should work the same way, here some code. Note that LDAP
> ctx.lookup returns Object, in my case 'bin' and I just convert back to
> byte array, as I store it.

I don't think the approach you used for a certificate will work, because
an X509 certificate is not the same object as a private RSA key, and
other APIs have to be used.

Signature

-----------------------------------------------------------------------
  Dr. Uwe Seimet                                 http://www.seimet.de

Jean-Baptiste Nizet - 26 Jul 2005 16:28 GMT
I think you should use the KeyFactory class. Look at its Javadoc, there
is an example for a DSA public key encoded using X509.
In your case, the code should probably look like (provided you encoded
your key using PKCS8)

PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(bytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);

JB.
Uwe Seimet - 26 Jul 2005 18:30 GMT
> I think you should use the KeyFactory class. Look at its Javadoc, there
> is an example for a DSA public key encoded using X509.
[quoted text clipped - 4 lines]
> KeyFactory keyFactory = KeyFactory.getInstance("RSA");
> PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);

Thank you, this helped me solve my problem!

Best regards,   Uwe

Signature

-----------------------------------------------------------------------
  Dr. Uwe Seimet                                 http://www.seimet.de



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.