trouble with my java encryption
<p>hi I  am having some trouble with my my encrypted chat system </p>

<p>i am trying to get a encryption key safely over a network by encrypting it ... the chat sever sends the encrypted key and the client sends the encryption back after it has been encrypted AGAIN the sever then decrypts it with the key it encrypted it ... this is where it gets stuck it seems to be having trouble decrypting the double encrypted string.... have a look at my code..</p>

<p>if you run this code (just run server and the client in seperate windows and it will work) and click the SYNC onthe server window first (i have yet to get it working both ways)</p>

<p>then sync on the client window.... read the command lines you will see my problem! </p>

<p>the main error i get is this </p>

<blockquote>
  <p><strong>Given final block not properly padded</strong> any help??</p>
</blockquote>

<p>I just need to fix the decryption issue with the padding ? how to i put no padding into the encryption/ decryption (that encryption class is at the bottom)</p>

<p><strong>SwingChatServer.java</strong></p>

<pre><code>   import java.awt.*;
import java.net.*;              
import java.awt.event.*;   
import javax.swing.*;     
import javax.swing.event.*;
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.spec.*;
import java.util.UUID;
import chat.*;
public class SwingChatServer extends SwingChatGUI
{
PrintWriter out;
BufferedReader in;
BufferedReader stdin;
String inputLine, outputLine, collect;
public ButtonHandler bHandler = new ButtonHandler();
public ButtonHandler bH = new ButtonHandler();
public String rgk;
public String stk;
public String lls;
public int sen;   
public SwingChatServer (String title)
{
  super (title);
  bHandler = new ButtonHandler ();
  sendButton.addActionListener (bHandler);
  synco.addActionListener (bH);
  keymaker();
}
private class ButtonHandler implements ActionListener
{
  public void actionPerformed (ActionEvent event)
  {

  if(event.getSource()==sendButton)
  {
   outputLine = txArea.getText ();
   System.out.println ("Server &gt; " + outputLine);
try {

    DesEncrypter encrypter = new DesEncrypter(rgk);
    String encrypted = encrypter.encrypt(outputLine);
System.out.println("" + encrypted);
out.println (encrypted);

} catch (Exception e) {
   //out.println (outputLine);
}
  }
  if(event.getSource()==synco)
  {
   System.out.println("YOUR PARTNER WISHES TO MAKE THIS A PRIVATE MATTER, CLICK SYNC TO ENCRYPT ALL MESSAGES");
   stk = rgk;
   System.out.println("this is the key which will encrypt the new key ---&gt; " + stk);
   keymaker();
   System.out.println("this is the key which will be encrypted ---&gt; " + rgk);
  try {
   DesEncrypter encrypter = new DesEncrypter(stk);
           String encrypted = encrypter.encrypt(rgk);
   System.out.println("this is how the key looks encrypted ---&gt; " + encrypted);
   out.println (encrypted);
   out.println("test");
   sen = 1;
  } catch (Exception e) {
   System.out.println("error");
  }

  }

         }
}

public void run () throws IOException
{
  ServerSocket serverSocket = null;

  try
            {
                 serverSocket = new ServerSocket (4444);
            }
            catch (IOException e)
            {
                 System.err.println ("Could not listen on port: 4444.");
                 System.exit (1);
            }
            Socket clientSocket = null;
            try
            {
                 clientSocket = serverSocket.accept ();
            }
            catch (IOException e)
            {
                 System.err.println ("Accept failed.");
                 System.exit(1);
            }
         out = new PrintWriter (clientSocket.getOutputStream (), true);
         in = new BufferedReader (new InputStreamReader (clientSocket.getInputStream ()));
         //stdin = new BufferedReader (new InputStreamReader (System.in));
            out.println ("Welcome to the Chat Server\n");
            while ((inputLine = in.readLine ()) != null)
            {

  lls = inputLine;
  if (sen == 1)
  {
  System.out.println("this should be the encrypted, encryption ---&gt; " + lls);

  try {
   System.out.println("test..1..." + inputLine + " and now for stk " + stk);  
   DesEncrypter encrypters = new DesEncrypter(stk);
   System.out.println("peeka BOO");
   String decrypteds = encrypters.decrypt(inputLine);
   System.out.println(decrypteds);
   //sen = 0;
   //stk = null;
   } catch (Exception e) {
    System.out.println(e.getMessage()); 
   }
  } else {

  System.out.println ("Server &lt; " + inputLine);
try {
   DesEncrypter encrypter = new DesEncrypter(rgk);

   String decrypted = encrypter.decrypt(inputLine);
   System.out.println("" + decrypted);
   rxArea.setText (decrypted);
   } catch (Exception e) {
   }
  collect = (collect +" \n"+ inputLine);
                 rxArea.setText (collect);

            }
}
            out.close();
            in.close();
            clientSocket.close();
            serverSocket.close();
    }
    public static void main(String[] args) //throws IOException
    {

  SwingChatServer f = new SwingChatServer ("Chat Server Program");

  f.pack ();
  f.setVisible(true);
  try
  {
   f.run ();
  }
  catch (IOException e)
  {
   System.err.println("Couldn't get I/O for the connection.");
   System.exit(1);
  }
    }
public void keymaker()
{

String uuid = UUID.randomUUID().toString();
rgk = uuid;
}
}
</code></pre>

<p><strong>SwingChatClient.java</strong></p>

<pre><code>import java.awt.*;
import java.net.*;              
import java.awt.event.*;   
import javax.swing.*;     
import javax.swing.event.*;
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.spec.*;
import java.util.UUID;
import chat.*;
public class SwingChatClient extends SwingChatGUI
{
         static Socket socket = null;
         static PrintWriter out = null;
         static BufferedReader in = null;
         public ButtonHandler bHandler, bH;
public String rgk;
public String stk;
public String lls;
public int sen;

public SwingChatClient (String title)
{
  super (title);
  bHandler = new ButtonHandler();
  bH = new ButtonHandler();
  sendButton.addActionListener( bHandler );
  synco.addActionListener( bH );
  keymaker();
}
private class ButtonHandler implements ActionListener
{
  public void actionPerformed (ActionEvent event)
  {
  if (event.getSource()==sendButton)
  {
            String outputLine;
          outputLine = txArea.getText ();
          System.out.println ("Client &gt; " + outputLine);
   out.println (outputLine);
  }
  if (event.getSource()==synco)
  {
   System.out.println("YOUR PARTNER WISHES TO MAKE THIS A PRIVATE MATTER, CLICK SYNC TO ENCRYPT ALL MESSAGES");
   stk = rgk;
   System.out.println("this is the key which will encrypt the new key " + stk);


  try {
   DesEncrypter encrypter = new DesEncrypter(stk);
           String encrypted = encrypter.encrypt(lls);
   System.out.println("" + encrypted);
   out.println (encrypted);
   sen = 1;
  } catch (Exception e) {
   System.out.println("error");
  }
  }
  }
}
public void run () throws IOException
{
  try
  {
   socket = new Socket ("localhost", 4444);
   out = new PrintWriter (socket.getOutputStream (), true);
   in = new BufferedReader (new InputStreamReader (socket.getInputStream ()));
  }
  catch (UnknownHostException e)
  {
   System.err.println ("Don't know about host: .");
   System.exit(1);
  }
  catch (IOException e)
  {
   System.err.println ("Couldn't get I/O for the connection to: .");
   System.exit (1);
  }
  String fromServer;
  while ((fromServer = in.readLine ()) != null)
  {
   System.out.println ("this should be encrypted " + fromServer);
   lls = fromServer;

   if (sen == 1)
   {

   try {
    DesEncrypter encrypter = new DesEncrypter(stk);
    String decrypted = encrypter.decrypt(fromServer);
    rgk = decrypted;
    sen = 0;
    stk = null;
    System.out.println("hello???");
   } catch (Exception e) {
   }

   } else {
   try {
   DesEncrypter encrypter = new DesEncrypter(rgk);

   String decrypted = encrypter.decrypt(fromServer);
   System.out.println("hmm..." + decrypted);
   rxArea.setText (decrypted);
   } catch (Exception e) {
   }
   if (fromServer.equals ("Bye.")) break;
  }
}
  out.close();
  in.close();
  socket.close();
}
public static void main(String[] args)
{
   SwingChatClient f = new SwingChatClient ("Chat Client Program");

  f.pack ();
  f.setVisible(true);
  try
  {
   f.run ();
  }
  catch (IOException e)
  {
   System.err.println("Couldn't get I/O for the connection to:");
   System.exit(1);
  }
}
public void keymaker()
{

String uuid = UUID.randomUUID().toString();
rgk = uuid;
}
}
</code></pre>

<p><strong>SwingChatGUI.java</strong></p>

<pre><code>import java.awt.*;               
import java.awt.event.*;   
import javax.swing.*;     
import javax.swing.event.*;
public class SwingChatGUI extends JFrame
{
public JButton sendButton, synco;
public JTextArea txArea, rxArea;

  public Container container;

public JPanel n1, s1;


public SwingChatGUI (String title)
{
  super (title);

            container = getContentPane();                       
            container.setLayout( new FlowLayout() );

  txArea = new JTextArea (6, 40);

  rxArea = new JTextArea (6, 40);

  sendButton = new JButton ("Send");
  synco = new JButton ("sync");

  container.add (rxArea);
  container.add (txArea);
  container.add (sendButton);
  container.add (synco);
}

public static void main (String[] args)
{
  Frame f = new SwingChatGUI ("Chat Program");
  f.pack ();
  f.setVisible(true);
}
}
</code></pre>

<p><em><strong>this is now the package chat.</em>*</strong></p>

<p><strong>DesEncrypter.java</strong></p>

<pre><code>package chat;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class DesEncrypter {
  Cipher ecipher;
  Cipher dcipher;
  byte[] salt = { (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32, (byte) 0x56, (byte) 0x35,
          (byte) 0xE3, (byte) 0x03 };
  public DesEncrypter(String passPhrase) throws Exception {
    int iterationCount = 2;
    KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
    SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
    ecipher = Cipher.getInstance(key.getAlgorithm());
    dcipher = Cipher.getInstance(key.getAlgorithm());
    AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
    ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
    dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
  }
  public String encrypt(String str) throws Exception {
    return new BASE64Encoder().encode(ecipher.doFinal(str.getBytes()));
  }
  public String decrypt(String str) throws Exception {
    return new String(dcipher.doFinal(new BASE64Decoder().decodeBuffer(str)));
  }
}
</code></pre>
