aspose file tools*
The moose likes Security and the fly likes How to store PublicKey and PrivateKey in database?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Security
Bookmark "How to store PublicKey and PrivateKey in database??" Watch "How to store PublicKey and PrivateKey in database??" New topic
Author

How to store PublicKey and PrivateKey in database??

Jahnvi Vyas
Ranch Hand

Joined: May 03, 2006
Posts: 36
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..
Aryan Khan
Ranch Hand

Joined: Sep 12, 2004
Posts: 290

You can use normal varchar field and encode the keys in lets say base64 and store it and while retreiving decode.

Ahmad


OCP/MCP/SCJP/SCWCD/IBM XML/SCMAD/SCEA-1
Jahnvi Vyas
Ranch Hand

Joined: May 03, 2006
Posts: 36
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
greg stark
Ranch Hand

Joined: Aug 10, 2006
Posts: 220
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



Nice to meet you.
Jahnvi Vyas
Ranch Hand

Joined: May 03, 2006
Posts: 36
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

Joined: Sep 12, 2004
Posts: 290

Download Bouncy Castle provider free from internet.

Ahmad
Lasse Bergstr�m
Greenhorn

Joined: Mar 15, 2005
Posts: 2
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to store PublicKey and PrivateKey in database??
 
Similar Threads
Store PrivateKey PublicKey in DB
StreamCurruptedException
How to store PublicKey and PrivateKey in database??
public key retrieval from DB incorrect
Generating Public and Private keys.