File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "casting String to SecretKey object" Watch "casting String to SecretKey object" New topic

casting String to SecretKey object

Manoj John
Ranch Hand

Joined: May 13, 2004
Posts: 36

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 ..
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
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 ]
I agree. Here's the link:
subject: casting String to SecretKey object
It's not a secret anymore!