JCE - load key from string  
Author Message
chermisha





PostPosted: 2005-8-9 23:08:00 Top

java-programmer, JCE - load key from string Hi All,

We're trying to use JCE to encrypt some data. The issue is key storage.
We want to store it in some database, so the question is how to
re-create the key for decryption from varchar2 column?

The key in string form looks like this:

cryptix.jce.provider.key.RawSecretKey@3bfce353

This can be stored in the database easily. But how to load this into
KeyStore back?

TIA
Michael

 
Ben_





PostPosted: 2005-8-10 3:15:00 Top

java-programmer >> JCE - load key from string Hello,

Most samples you'll find store the key in a file. It's as simple as new
ObjectOutputStream(new FileOutputStream(file)).writeObject(key).

Now, you want to save a string into a database.

So you'd need to serialize the object into a ByteArrayOutputStream, convert
the bytes with Base64 and save the resulting string. And the other way round
to load it.

Just out of my head, not verified... :-)


 
chermisha





PostPosted: 2005-8-11 1:15:00 Top

java-programmer >> JCE - load key from string Hi Ben,

Yes, I know how to create key from file :-)

The solution I found looks like this (example is for TripleDES
encryption
key):

DESedeKeySpec desKeySpec = new DESedeKeySpec("[24-bit key
string]".getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
SecretKey key = keyFactory.generateSecret(desKeySpec);

This way the key is restored correctly. I think your solution works
too, but
I haven't tried it - I'm not sure it's any smaller or quicker?...

Thanks a lot
Michael

 
 
chermisha





PostPosted: 2005-8-11 1:16:00 Top

java-programmer >> JCE - load key from string Hi Ben,

Yes, I know how to create key from file :-)

The solution I found looks like this (example is for TripleDES
encryption
key):

DESedeKeySpec desKeySpec = new DESedeKeySpec("[24-bit key
string]".getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
SecretKey key = keyFactory.generateSecret(desKeySpec);

This way the key is restored correctly. I think your solution works
too, but
I haven't tried it - I'm not sure it's any smaller or quicker?...

Thanks a lot
Michael

Ben_ wrote:
> Hello,
>
> Most samples you'll find store the key in a file. It's as simple as new
> ObjectOutputStream(new FileOutputStream(file)).writeObject(key).
>
> Now, you want to save a string into a database.
>
> So you'd need to serialize the object into a ByteArrayOutputStream, convert
> the bytes with Base64 and save the resulting string. And the other way round
> to load it.
>
> Just out of my head, not verified... :-)