GeeCON Prague 2014*
The moose likes Java in General and the fly likes Reading & Writing Records in java.IO Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Reading & Writing Records in java.IO" Watch "Reading & Writing Records in java.IO" New topic
Author

Reading & Writing Records in java.IO

Sumayah Abdul
Greenhorn

Joined: Nov 14, 2008
Posts: 21
Hello everyone,

I need to store session keys of type java.security.Key in a permanent storage.

I was hoping to store them in binary files. but the problem is that I can only read/write primitive types and byte[].

I was able to get the byte[] of the key using
but how can I read this byte[] into java.security.Key which is an interface only?

is there any other way to store Key objects in permanent storage?

Your help is greatly appreciated..
thank you.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

I think you're after the KeyFactory API...


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Sumayah Abdul
Greenhorn

Joined: Nov 14, 2008
Posts: 21
Martijn Verburg wrote:I think you're after the KeyFactory API...

Thank you for replying..
I did check the key factory but it only generate private and public keys and I want a symmetric key.
I tried to check the java.Security.Spec but the classes that are instantiable are specs for private and public keys.

can you please show me how i can use the keyfactory to generate symmetric keys..

thank you
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40


To generate a symmetric key, you need the encoding and the algorithm, which you can get from key like so...



You can, of course, save these components to a file... Later, after you read these back from the file, you can regenerate your symmetric key as follows...



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40


BTW, I forgot to mention... but I think secret keys are serializable. This means that you can wrap an ObjectOutputStream to your FileOutputStream, and use it to write the key directly to the file.... and to reverse, wrap an ObjectInputStream to your FileInputStream.

Henry
Sumayah Abdul
Greenhorn

Joined: Nov 14, 2008
Posts: 21
hello again,
I just stumbled on Object Streams which worked. Object Streams read and write objects to files.
following are the functions I used to read and write to files in case anyone needed them.
Sumayah Abdul
Greenhorn

Joined: Nov 14, 2008
Posts: 21
Henry Wong wrote:
BTW, I forgot to mention... but I think secret keys are serializable. This means that you can wrap an ObjectOutputStream to your FileOutputStream, and use it to write the key directly to the file.... and to reverse, wrap an ObjectInputStream to your FileInputStream.

Henry


I missed your post by minutes but Thank you for your efforts. Problem solved.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Hey cool! I've never worked with saving off keys etc (as you can tell my KeyFactory pointer was well off the mark), interesting stuff!
Sumayah Abdul
Greenhorn

Joined: Nov 14, 2008
Posts: 21
Henry Wong wrote:
To generate a symmetric key, you need the encoding and the algorithm, which you can get from key like so...



You can, of course, save these components to a file... Later, after you read these back from the file, you can regenerate your symmetric key as follows...



Henry


Thank you for this too. it solved another problem where i need to seal session keys with public keys. I didn't know about the SecretKeySpec under javax.crypto.spec. because I was looking under the java.security.spec.* only.

Thank you again.
 
GeeCON Prague 2014
 
subject: Reading & Writing Records in java.IO