• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to store PublicKey and PrivateKey in database??

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am developing web application using JSP and MySql 5.1. I want to store PublicKey as well PrivateKey in my database.

The data type which I use to store key is BLOB. But I dont know how to store or retrive this. I tried with byte[]. By doing this I am able to store and retrive key, but it is as byte[] datatype. I want to retrive this as PublicKey or PrivateKey and there are no ways to convert byte[] to and Key type.

So, where am I goin wrong?? Are there any other ways to store and retrive Key??? And which datatype I should use for field??

Thanks.

Jahnvi..
 
Ranch Hand
Posts: 290
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use normal varchar field and encode the keys in lets say base64 and store it and while retreiving decode.

Ahmad
 
Jahnvi Vyas
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ahmad.

My actual problem is not how to store a key but to retrive it. Let me tell you exactly what I am trying to do:

1. I am generating PublicKey and PrivateKey from KeyPairGenerator and fetching them with getPublicKey() and getPrivateKey(). Then I am storing them in database where my field type is BLOB.

2. Now I want to fetch this key in byte[]. But there are no ways to convert byte[] into PrivateKey and PublicKey. I want them in key form because I want to further sign data with this PrivateKey and to initialize Signature object I must pass argument as PrivateKey (not as byte[]).

I hope now you got the actual problem. If you have further suggestions then I will be very much thankful.

Thanks.

Jahnvi
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the getEncoded() method to get the byte[] from the keys. To go the other way, getting an RSAPublicKey or RSAPrivateKey from a byte[], use the appropriate KeyFactory methods and encoded key specs, e.g. for RSAPrivateKey

and for RSAPublicKey

 
Jahnvi Vyas
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Greg.

I tried that code (RSAPublicKey....) and it works. But now another problem occured. When I am signing message with this key I am getting error:

--> java.security.InvalidKeyException :No Installed provider supports this key:sun.security.rsa.RSAPrivateCrtKeyImpl

So, do I need to install it or simply if I update my jdk version(if it is the cause for the error) will give me proper result?

Thanks..

Jahnvi.
 
Aryan Khan
Ranch Hand
Posts: 290
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Download Bouncy Castle provider free from internet.

Ahmad
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by greg stark:
Use the getEncoded() method to get the byte[] from the keys. To go the other way, getting an RSAPublicKey or RSAPrivateKey from a byte[], use the appropriate KeyFactory methods and encoded key specs, e.g. for RSAPrivateKey

and for RSAPublicKey



Hi,

I applied your code to my example:

rsaKeyFac = KeyFactory.getInstance("RSA");
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(client_privatekey.getEncoded());
RSAPublicKey pubKey = (RSAPublicKey )rsaKeyFac.generatePublic(keySpec);

However I get the following error when executing the last line:
Exception in thread "main" java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: algid parse error, not a sequence

Any expeience from this?

Best regards

Lasse
 
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic