Sending key using socket

hi

i m trying to send the key using sockets ..i made som changes to d original code by using the objects..but it shows d error :

BadPaddingException: javax.crypto.BadPaddingException: Given final block not properly padded

i m enclosing the codes : plz help me

SERVER CODE

import java.net.*;

import java.io.*;

import javax.crypto.*;

import javax.crypto.spec.*;

import java.security.*;

import java.security.spec.*;

import java.util.*;

class Server

{

publicstaticvoid main(String a[])throws IOException

{

new Server().listen(1450);

}

publicvoid listen(int port)throws IOException

{

ServerSocket serverSocket =new ServerSocket(1450);

new SocketHandler(serverSocket.accept()).start();

}

class SocketHandlerextends Thread

{

Socket socket =null;

SocketHandler(Socket socket)

{

this.socket = socket;

}

publicvoid run()

{

try

{

SecretKey secretKey =null;

File file =new File("file.jpg");

OutputStream fileOutputStream =new FileOutputStream(file);

ObjectInputStream in =new ObjectInputStream(socket.getInputStream());

int fileSize = in.readInt();

byte[] encryptedBytes =newbyte[fileSize * 8];

byte[] bytes =newbyte[fileSize];

secretKey = (SecretKey)in.readObject();

// Create Cipher

Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

desCipher.init(Cipher.DECRYPT_MODE, secretKey);

int i = -1;

while (true)

{

i = in.read(encryptedBytes, 0, encryptedBytes.length);

bytes = desCipher.doFinal(encryptedBytes);

if (i == -1)

break;

fileOutputStream.write(bytes, 0, i);

}

in.close();

fileOutputStream.flush();

fileOutputStream.close();

}

catch (IOException ioe)

{

ioe.printStackTrace();

}

catch(ClassNotFoundException cnfe)

{

cnfe.printStackTrace();

}

catch (NoSuchPaddingException e)

{

System.err.println("Padding problem: " + e);

}

catch (NoSuchAlgorithmException e)

{

System.err.println("Invalid algorithm: " + e);

}

catch (InvalidKeyException e)

{

System.err.println("Invalid key: " + e);

}

catch (IllegalBlockSizeException e)

{

System.err.println("IllegalBlockSizeException: " + e);

}

catch (BadPaddingException e)

{

System.err.println("BadPaddingException: " + e);

}

finally

{

try

{

socket.close();

}

catch (IOException ioe)

{

ioe.printStackTrace();

}

}

}

}

}

ClIENT CODE

import java.net.*;

import java.io.*;

import javax.crypto.*;

import javax.crypto.spec.*;

import java.security.*;

import java.security.spec.*;

import java.util.*;

class Client

{

publicstaticvoid main(String a[])

{

ObjectOutputStream out =null;

Socket socket =null;

try

{

// Create Key

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

SecretKey secretKey = kg.generateKey();

// Create Cipher

Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

desCipher.init(Cipher.ENCRYPT_MODE, secretKey);

socket =new Socket("localhost", 1450);

out =new ObjectOutputStream(socket.getOutputStream());

File file =new File("C:\\Haroot\\hfz.txt");

FileInputStream fileInputStream =new FileInputStream(file);

byte[] bytes =newbyte[(int)file.length()];

fileInputStream.read(bytes);

fileInputStream.close();

byte[] encryptedBytes =newbyte[(int)file.length()];

encryptedBytes = desCipher.doFinal(bytes);

out.writeInt((int)file.length());

out.writeObject((Object)secretKey);

out.write(encryptedBytes);

out.flush();

out.close();

}

catch(IOException e)

{

e.printStackTrace();

}

catch (NoSuchPaddingException e)

{

System.err.println("Padding problem: " + e);

}

catch (NoSuchAlgorithmException e)

{

System.err.println("Invalid algorithm: " + e);

}

catch (InvalidKeyException e)

{

System.err.println("Invalid key: " + e);

}

catch (IllegalBlockSizeException e)

{

System.err.println("IllegalBlockSizeException: " + e);

}

catch (BadPaddingException e)

{

System.err.println("BadPaddingException: " + e);

}

finally

{

try

{

socket.close();

}

catch(IOException e)

{

e.printStackTrace();

}

}

}

}

[9841 byte] By [haroota] at [2007-10-3 11:27:44]
# 1
You're writing the encrypted bytes as a byte[] object. Read it the same way, i.e.byte[] encryptedBytes = (byte[])in.readObject();Then do the doFinal. No loop required.
ejpa at 2007-7-15 13:53:53 > top of Java-index,Security,Cryptography...
# 2

hi..

i made the changes that was mentioned before but the thing is that now the error comes at runtime..since the encrypted bytes are recieved but that are diffrerent from the one that is send by the cleint.

at runtime the client program shows:

compile-single:

run-single:

SEC KEY:com.sun.crypto.provider.DESKey@fffe7b1e

NonEncrypted Bytes ::[B@1cd8669

Encryted :[B@119cca4

Encrypted Bytes ::[B@119cca4

and the server shows the following:

ENCRYPTED BYTES:[B@d9f9c3

ENCRYPTED BYTES:[B@d9f9c3

java.io.OptionalDataException

at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1310)

at java.io.ObjectInputStream.readObject(ObjectInputStream.java:339)

at Server$SocketHandler.run(Server.java:64)

The optional data exception is shown in the following line:

secretKey = (SecretKey)in.readObject();

the modification in the code was done in the following area of the server program:

int fileSize = in.readInt();

try

{

encryptedBytes = (byte[])in.readObject();

System.out.println("ENCRYPTED BYTES:" + encryptedBytes);

}

catch(ClassCastException cce)

{

System.out.println("e");

}

System.out.println("ENCRYPTED BYTES:" + encryptedBytes);

byte[] bytes = new byte[fileSize];

secretKey = (SecretKey)in.readObject();

System.out.println("SEC KEY" + secretKey);

// Create Cipher

Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

desCipher.init(Cipher.DECRYPT_MODE, secretKey);

Waiting for reply..

haroota at 2007-7-15 13:53:53 > top of Java-index,Security,Cryptography...
# 3

[code]

> out.writeInt((int)file.length());

> out.writeObject((Object)secretKey);

> out.write(encryptedBytes);

>int fileSize = in.readInt();

>encryptedBytes = (byte[])in.readObject();

>byte[] bytes = new byte[fileSize];

>secretKey = (SecretKey)in.readObject();

Have you considered reading things in the same order you write them?

ejpa at 2007-7-15 13:53:53 > top of Java-index,Security,Cryptography...
# 4
hi thx for replying..but i didnt understood your question.?
haroota at 2007-7-15 13:53:53 > top of Java-index,Security,Cryptography...
# 5

'ejp' is commenting on the fact that you

write the length

write the key

write the encrypted bytes

BUT when you read you

read the length

read the encrypted bytes

read the key

Now to spell it out, you should read in the same order as you write so you must read the key before the encrypted bytes because you write the key before the encrypted bytes.

sabre150a at 2007-7-15 13:53:53 > top of Java-index,Security,Cryptography...
# 6

hi

made the changes as mentioned in the last message but still the server side shows runtime errors and now the encrypted bytes are shown as null.

RUN TIME ERROR(SERVER SIDE)

compile-single:

run-single:

error

ENCRYPTED BYTES:null

SEC KEYnull

Invalid key: java.security.InvalidKeyException: No installed provider supports this key: (null)

BUILD SUCCESSFUL (total time: 3 seconds)

haroota at 2007-7-15 13:53:53 > top of Java-index,Security,Cryptography...
# 7

Your basic problem is not an encryption problem. Can I suggest that you stop trying to run and try first to walk. Create a very simple client and server. Make the client sent the server a number of bytes and make the server receive those bytes and print them.

On both client and server you will probably need to use a DataOutputStream to wrap the socket's OutputStream when sending the data and to use DataInputStream to wrap the socket's InputStream when reading the data. The classes have methods that will allow you to send a byte array by

1) send the length of the byte array

2) send the bytes

and then you can receive the byte array by

1) reading the length of the byte array

2) construct a byte array of length that in 1) in which to put the read bytes

3) read the bytes into the byte array of 2)

Once you have this working then you can consider adding the rest.

sabre150a at 2007-7-15 13:53:53 > top of Java-index,Security,Cryptography...
# 8
Hi,please I have the same problem.Ha hopefully somebody working Java code - to send a SecretKey over a network!I am Trying - 3 kinds but no aon works.I will be very very thankful for someone help.Thanks a lot !!
Majkaa at 2007-7-15 13:53:53 > top of Java-index,Security,Cryptography...