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??
You can use normal varchar field and encode the keys in lets say base64 and store it and while retreiving decode.
Joined: May 03, 2006
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.
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.
Joined: May 03, 2006
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?
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
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