converting SecretKey to String and String to SecretKey

In my client server program i tried to encrypt the message using SecretKey object ,using DES. simply for a trial i sent this key(encryption key) also on other end in the string format for decryption. now on the other end for decryption the init() is not accepting this key as it takes only the secretkey object. how to convert this key form string to Secretkey so that i'l be able to decrypt the message?

plz help. i need ur help. my e-mail id is jenniekiro@yahoo.com

[482 byte] By [trewqya] at [2007-11-26 15:27:03]
# 1
Post your code for both saving ad restoring the key.
sabre150a at 2007-7-8 21:42:51 > top of Java-index,Security,Cryptography...
# 2

//server side

//encryption key generation

KeyGenerator kg=KeyGenerator.getInstance("DES");

SecretKey key1 = kg.generateKey();

System.out.println("Random Key for encryption is:"+key1);

PrintWriter p=new PrintWriter(s.getOutputStream(),true);

p.println(key1);

//generating random key to be encrypted and to be sent

SecretKey key2 = KeyGenerator.getInstance("DES").generateKey();

System.out.println("the random KEY to be encrypted:"+key2);

String st=String.valueOf(key2);

byte[] kee = st.getBytes();

byte[] initVector = new byte[] { 0x10, 0x10, 0x01, 0x04, 0x01, 0x01, 0x01, 0x02 };

AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector);

Cipher m_encrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");

Cipher m_decrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");

m_encrypter.init(Cipher.ENCRYPT_MODE, key1, algParamSpec);

m_decrypter.init(Cipher.DECRYPT_MODE, key1, algParamSpec);

//SENDING

byte[] encryptedText = m_encrypter.doFinal(kee);

String st1=new String(encryptedText);

System.out.println("\n Encrypted text : "+st1);

PrintWriter pout1=new PrintWriter(s.getOutputStream(),true);

pout1.println(st1);

//Client side

//RECEIVING the Key for decryption

BufferedReader b2=new BufferedReader(new

InputStreamReader(s.getInputStream()));

String s1=b2.readLine();

System.out.println("Secret encryption Key from user A received is :

"+s1);

//I don't know this part how to do? so tried with bytes.

SecretKey kee=SecretKey.valueOf(s1);

byte[] kee=s1.getBytes();

System.out.println("key in byte is:"+kee);

//its showing the error in init()

byte[] initVector = new byte[] { 0x10, 0x10, 0x01, 0x04, 0x01,

0x01, 0x01, 0x02 };

AlgorithmParameterSpec algParamSpec = new

IvParameterSpec(initVector);

Cipher m_encrypter =

Cipher.getInstance("DES/CBC/PKCS5Padding");

Cipher m_decrypter =

Cipher.getInstance("DES/CBC/PKCS5Padding");

m_encrypter.init(Cipher.ENCRYPT_MODE,kee, algParamSpec);

System.out.println("test2");

m_decrypter.init(Cipher.DECRYPT_MODE, kee, algParamSpec);

//Receiving the encrypted key

BufferedReader br2=new BufferedReader(new

InputStreamReader(s.getInputStream()));

String str1=br2.readLine();

System.out.println("Secret encrypted Key from user A received is :

"+str1);

byte[] tx=str1.getBytes();

byte[] decryptedText = m_decrypter.doFinal(tx);

System.out.println("Decrypted Data is:");

System.out.println(new String(decryptedText));

trewqya at 2007-7-8 21:42:51 > top of Java-index,Security,Cryptography...
# 3

Argh.

Keys and ciphertext are not, not, NOT strings - they are bytes, often with values that cannot be printable-string-ified. This sort of thing:byte[] encryptedText = m_encrypter.doFinal(kee);

String st1=new String(encryptedText);

turns your ciphertext into garbage. Stop that.

Ask the key for its bytes. Base64-encode those bytes. NOW you have a String. At the other end, Base64-decode the string, to get the bytes back. Search this forum for Base64 - manymanymany examples have been posted, this is probably the most common (catastrophic) mistake we correct here.

Good luck,

Grant

ggaineya at 2007-7-8 21:42:51 > top of Java-index,Security,Cryptography...