How to retrieve public & private keys from database ?
hi,
I am using RSA algorithm to implement security.
The generated public and private keys, I am storing in
database. but when i am trying to retrieve them, it
is throwing ClassCastException. how to get the stored
keys from the database ? (how to cast them into
public and private key interfaces ?)
To save the key you can convert them into a byte array and then to a string. Like this:
KeyGenerator generator = KeyGenerator.getInstance( "DES" );
generator.init( new SecureRandom() );
Key key = generator.generateKey();
byte[] keyBytes = key.getEncoded();
String keyStringData = new String( key.getEncoded() );
You can then save this string to a nvarchar. To turn the string back into a key do the following:
byte[] keyBytes = keyStringData.getBytes();
key = SecretKeyFactory.getInstance( "DES" ).generateSecret( new DESKeySpec( keyBytes ) );
I hope this helps. If you know how to use a public key to encrypt and private to decrypt, then let me know.
Thanks,
Carlo
bakara at 2007-6-29 19:31:15 >

Hey,
I know this is a pretty old one, but I'm still seeking for the onsawer to that question, how to retrieve RSA keys from database.
I tried the code provided, tried to change it but I still cannot get it to work. I've also read some of other answers trying to figure out a way to do this, but no luck. I want a clean and easy version please.
Thank you
Reply #1 was wrong six years ago and it is still wrong. String is not a container for binary data. You need to base-64-encode the bytes as a String, not just convert it directly, and base-64-decode it in the other direction.
ejp at 2007-6-29 19:31:15 >
