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
{
public static void main(String a[]) throws IOException
{
new Server().listen(1450);
}
public void listen(int port) throws IOException
{
ServerSocket serverSocket = new ServerSocket(1450);
new SocketHandler(serverSocket.accept()).start();
}
class SocketHandler extends Thread
{
Socket socket = null;
SocketHandler(Socket socket)
{
this.socket = socket;
}
public void 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 = new byte[fileSize * 8];
byte[] bytes = new byte[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
{
public static void 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 = new byte[(int)file.length()];
fileInputStream.read(bytes);
fileInputStream.close();
byte[] encryptedBytes = new byte[(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();
}
}
}
}