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 ?)

[351 byte] By [dsvr_sameer] at [2007-9-26 5:24:06]
# 1

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 > top of Java-index,Security,Cryptography...
# 2

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

despina at 2007-6-29 19:31:15 > top of Java-index,Security,Cryptography...
# 3
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 > top of Java-index,Security,Cryptography...