This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes casting String to SecretKey object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "casting String to SecretKey object" Watch "casting String to SecretKey object" New topic
Author

casting String to SecretKey object

Manoj John
Ranch Hand

Joined: May 13, 2004
Posts: 36
Hi,

please give help urgent....
In my servlet i am encrypting one message (say userId or pswd)
for encryption i am using javax.crypto package algoritham used is DES
I am generating key as ..
-----------code--------
SecretKey key =KeyGenerator.getInstance("DES").generateKey();
------------------
Then converting it into String and passing it as requestparameter along with encrypted message.
(this is step i can't change ie i need to pass these as requestParameter)

In the second servlet for decryption i need this key ,I am getting that but
I am not able to cast it to SecretKey object
any solution related to this will be great for me..
or any method to reproduce the same key in the second servlet as in first servlet used for encryption.
Any method is available for generating a key with respect to a String given hardcoded...
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
The Security Guide in the JDK documentation gives an example that includes code converting a key to a byte array, and later converting the byte array back into a key. The example uses a public key, but secret keys are handled in an analogous way. All that remains to do is Base64 encoding it.

I will not ask what you hope to achieve by sending an encrypted message together with its key in an HTTP request :roll:

- Peter
[ July 28, 2004: Message edited by: Peter den Haan ]
 
Consider Paul's rocket mass heater.
 
subject: casting String to SecretKey object