File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to serialize a java.security.key

 
sudheer kiran
Ranch Hand
Posts: 237
Firefox Browser MyEclipse IDE Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all my requirement is i need to store a key value as string .

i need this because i nee to store that key as a string into a map.

map.put("pwd","keyValueAsString");

is it possible..suggest me please.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By "java.security.key", do you mean a byte[]? You can use base-64 encoding to convert that to a String. But maps can hold any kind of object (including byte[]), so I may be misunderstanding what you're trying to do.
 
sudheer kiran
Ranch Hand
Posts: 237
Firefox Browser MyEclipse IDE Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ulf Dittmer,

i cannot change map from map<String,String>..as map is used every where in my project;
so i need to convert a "java.security.key' object to String Object and store into map.
and i should retrieve and convert it back to Key object. is that possible.

thank you for the reply.



 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sudheer kiran wrote:hi Ulf Dittmer,

i cannot change map from map<String,String>..as map is used every where in my project;
so i need to convert a "java.security.key' object to String Object and store into map.
and i should retrieve and convert it back to Key object. is that possible.


java.security.Key has a method getEncoded() which return a byte[] array representing the bytes of the key. As Ulf says, you can then Base64 (or Hex or even ASCII85) encode these bytes to create a String. Recovery of the key can then be achieved by creating of an appropriate SecretKeySpec instance using the bytes of the decoded String and specifying the type of the key. You should note that SecretKeySpec extends java.security.Key so as long as you are talking of a SecretKey and not an RSA or DSA key then you don't have to go through any contortions involving KeyGenerator or the like.

Don't even think about just creating a String directly from the bytes of the key. String is not a suitable container for binary data and keys most definitely are binary data.

 
sudheer kiran
Ranch Hand
Posts: 237
Firefox Browser MyEclipse IDE Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi James your solution resoled my problem,it's all working very fine ,very much thanks to you.
thank you Ulf Dittmer.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic