Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading & Writing Records in java.IO

 
Sumayah Abdul
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're after the KeyFactory API...
 
Sumayah Abdul
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic