EncryptedChat

Hello everyone, I am having a bit of trouble with my application. It is an encrypted chat client/server. It seems like the buffering is incorrect because the server recieves data one send too late, and the client, in turn, recieves data two sends too late. Thank you for your time.
Here is the client:
import java.awt.BorderLayout;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.swing.*;
import javax.swing.border.*;
public class EncryptedClient extends JFrame
     private byte[] Salt = {(byte)0x00, (byte)0xaa, (byte)0x1b, (byte)0xc2, (byte)0x34, (byte)0xde, (byte)0x5f, (byte)0xa6};
     private char[] Password;
     private int Port = 1024, Count = 20;
     private String IP = "127.0.0.1", Nick;
     private JTextArea TextArea;
     private JTextField TextField;
     private Socket Client;
     private ObjectOutputStream Out;
     private ObjectInputStream In;
     private EncryptedClient()
          super("Kaps's Encrypted Chat Client");
          try
               BufferedReader SystemIn = new BufferedReader(new InputStreamReader(System.in));
               System.out.print("Enter the IP of the server you would like to join: ");
               String CurrentLine = SystemIn.readLine();
               if(!CurrentLine.equals("local") && !CurrentLine.equals(""))
                    IP = CurrentLine;
               System.out.print("Enter the port of the server you would like to join: ");
               CurrentLine = SystemIn.readLine();
               if(!CurrentLine.equals(""))
                    Port = Integer.parseInt(CurrentLine);
               System.out.print("Enter your nick for the server you would like to join: ");
               Nick = SystemIn.readLine();
               System.out.print("Enter the password of the server you would like to join: ");
               Password = SystemIn.readLine().toCharArray();
               TextArea = new JTextArea();
               TextField = new JTextField();
               JPanel Panel = new JPanel(new BorderLayout());
               TextArea.setEditable(false);
               TextField.addActionListener(new ActionListener()
                    public void actionPerformed(ActionEvent e)
                         try
                              Out.writeObject(Nick + ": " + TextField.getText());
                              TextField.setText("");
                         catch(Throwable t) {}
               Panel.add(new JScrollPane(TextArea), BorderLayout.CENTER);
               Panel.add(TextField, BorderLayout.SOUTH);
               Panel.setBorder(new TitledBorder(new EtchedBorder(), "Kaps's Encrypted Chat Client"));
               addWindowListener(new WindowAdapter()
                    public void windowClosing(WindowEvent e)
                         try
                              System.exit(0);
                         catch(Throwable t) {}
               getContentPane().add(Panel);
               setSize(640, 480);
               setVisible(true);
               Client = new Socket(IP, Port);
               Out = new ObjectOutputStream(new CipherOutputStream(Client.getOutputStream(), GetCipher(Cipher.ENCRYPT_MODE)));
               In = new ObjectInputStream(new CipherInputStream(Client.getInputStream(), GetCipher(Cipher.DECRYPT_MODE)));
               Out.writeObject(Nick);
               while(Client.isBound() && Client.isConnected())
                    TextArea.append((String)In.readObject() + "\n");
          catch(Throwable t) {}
     public static void main(String[] args)
          try
               new EncryptedClient();
          catch(Throwable t) {}
     private Cipher GetCipher(int Mode)
          Cipher PBECipher = new NullCipher();
          try
               PBECipher = Cipher.getInstance("PBEWithMD5AndDES");
               PBECipher.init(Mode, SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(new PBEKeySpec(Password)), new PBEParameterSpec(Salt, Count));
          catch(Throwable t) {}
          return PBECipher;
}And the server:
import java.io.*;
import java.net.*;
import java.util.Vector;
import javax.crypto.*;
import javax.crypto.spec.*;
public class EncryptedServer
     private byte[] Salt = {(byte)0x00, (byte)0xaa, (byte)0x1b, (byte)0xc2, (byte)0x34, (byte)0xde, (byte)0x5f, (byte)0xa6};
     private char[] Password;
     private int Port = 1024, MaxClients = 1024, Count = 20;
     private ServerSocket Server;
     private Vector Clients;
     private EncryptedServer()
          try
               BufferedReader In = new BufferedReader(new InputStreamReader(System.in));
               System.out.print("Enter the port you would like to use for your server: ");
               String CurrentLine = In.readLine();
               if(!CurrentLine.equals(""))
                    Port = Integer.parseInt(CurrentLine);
               System.out.print("Enter the maximum number of clients you would like to have for your server: ");
               CurrentLine = In.readLine();
               if(!CurrentLine.equals(""))
                    MaxClients = Integer.parseInt(CurrentLine);
               System.out.print("Enter the password you would like to use for your server: ");
               CurrentLine = In.readLine();
               Password = CurrentLine.toCharArray();
               System.out.println("Server started.");
               Server = new ServerSocket(Port, MaxClients);
               Clients = new Vector();
               while(true)
                    Clients.addElement(new ThreadedSocket(Server.accept()));
          catch(Throwable t) {}
     public static void main(String[] args)
          new EncryptedServer();
     private Cipher GetCipher(int Mode)
          Cipher PBECipher = new NullCipher();
          try
               PBECipher = Cipher.getInstance("PBEWithMD5AndDES");
               PBECipher.init(Mode, SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(new PBEKeySpec(Password)), new PBEParameterSpec(Salt, Count));
          catch(Throwable t) {}
          return PBECipher;
     private void Echo(String Message)
          try
               System.out.println(Message);
               for(int ClientNumber = 0; ClientNumber < Clients.size(); ClientNumber++)
                    ((ThreadedSocket)Clients.elementAt(ClientNumber)).Out.writeObject(Message);
          catch(Throwable t) {}
     private class ThreadedSocket extends Thread
          public Socket Client;
          public ObjectOutputStream Out;
          public ObjectInputStream In;
          public ThreadedSocket(Socket SocketChosen)
               try
                    Client = SocketChosen;
                    Out = new ObjectOutputStream(new CipherOutputStream(Client.getOutputStream(), GetCipher(Cipher.ENCRYPT_MODE)));
                    In = new ObjectInputStream(new CipherInputStream(Client.getInputStream(), GetCipher(Cipher.DECRYPT_MODE)));
                    start();
               catch(Throwable t) {}
          public void run()
               try
                    String Nick = "", Message;
                    boolean Flag = true;
                    while(Client.isBound() && Client.isConnected())
                         Message = (String)In.readObject();
                         if(Flag)
                              Nick = Message;
                              Echo("Server: " + Nick + " [" + Client.getInetAddress().getHostName() + "] has joined this server.");
                              Flag = false;
                         else
                              Echo(Message);
                    Echo("Server: " + Nick + " has quit.");
                    Client.close();
                    Out.close();
                    In.close();
               catch(Throwable t) {}
}

Here is my new, updated version. I added the flush() method every time I write an Object or String, I made the Echo(String Message) method synchronized, and added Thread.yield() to two needed locations, but the application still will not work.
Server:
import java.io.*;
import java.net.*;
import java.util.Vector;
import javax.crypto.*;
import javax.crypto.spec.*;
public class EncryptedServer
     private byte[] Salt = {(byte)0x00, (byte)0xaa, (byte)0x1b, (byte)0xc2, (byte)0x34, (byte)0xde, (byte)0x5f, (byte)0xa6};
     private char[] Password;
     private int Port = 1024, MaxClients = 1024, Count = 20;
     private ServerSocket Server;
     private Vector Clients;
     private EncryptedServer()
          try
               BufferedReader In = new BufferedReader(new InputStreamReader(System.in));
               System.out.print("Enter the port you would like to use for your server: ");
               String CurrentLine = In.readLine();
               if(!CurrentLine.equals(""))
                    Port = Integer.parseInt(CurrentLine);
               System.out.print("Enter the maximum number of clients you would like to have for your server: ");
               CurrentLine = In.readLine();
               if(!CurrentLine.equals(""))
                    MaxClients = Integer.parseInt(CurrentLine);
               System.out.print("Enter the password you would like to use for your server: ");
               CurrentLine = In.readLine();
               Password = CurrentLine.toCharArray();
               System.out.println("Server started.");
               Server = new ServerSocket(Port, MaxClients);
               Clients = new Vector();
               while(true)
                    Clients.addElement(new ThreadedSocket(Server.accept()));
                    Thread.yield();
          catch(Throwable t) {}
     public static void main(String[] args)
          try
               new EncryptedServer();
          catch(Throwable t) {}
     private Cipher GetCipher(int Mode)
          Cipher PBECipher = new NullCipher();
          try
               PBECipher = Cipher.getInstance("PBEWithMD5AndDES");
               PBECipher.init(Mode, SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(new PBEKeySpec(Password)), new PBEParameterSpec(Salt, Count));
          catch(Throwable t) {}
          return PBECipher;
     private synchronized void Echo(String Message)
          try
               System.out.println(Message);
               for(int ClientNumber = 0; ClientNumber < Clients.size(); ClientNumber++)
                    ((ThreadedSocket)Clients.elementAt(ClientNumber)).writeMessage(Message);
          catch(Throwable t) {}
     private class ThreadedSocket extends Thread
          public Socket Client;
          public ObjectOutputStream Out;
          public ObjectInputStream In;
          public ThreadedSocket(Socket SocketChosen)
               try
                    Client = SocketChosen;
                    Out = new ObjectOutputStream(new CipherOutputStream(Client.getOutputStream(), GetCipher(Cipher.ENCRYPT_MODE)));
                    In = new ObjectInputStream(new CipherInputStream(Client.getInputStream(), GetCipher(Cipher.DECRYPT_MODE)));
                    start();
               catch(Throwable t) {}
          public void run()
               try
                    String Nick = "", Message;
                    boolean Flag = true;
                    while(Client.isBound() && Client.isConnected())
                         Message = (String)In.readObject();
                         if(Flag)
                              Nick = Message;
                              Echo("Server: " + Nick + " [" + Client.getInetAddress().getHostName() + "] has joined this server.");
                              Flag = false;
                         else
                              Echo(Message);
                         Thread.yield();
                    Echo("Server: " + Nick + " has quit.");
                    Client.close();
                    Out.close();
                    In.close();
               catch(Throwable t) {}
          public void writeMessage(String Message)
               try
                    Out.writeObject(Message);
                    Out.flush();
               catch(Throwable t) {}
}Client:
import java.awt.BorderLayout;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.swing.*;
import javax.swing.border.*;
public class EncryptedClient extends JFrame
     private byte[] Salt = {(byte)0x00, (byte)0xaa, (byte)0x1b, (byte)0xc2, (byte)0x34, (byte)0xde, (byte)0x5f, (byte)0xa6};
     private char[] Password;
     private int Port = 1024, Count = 20;
     private String IP = "127.0.0.1", Nick;
     private JTextArea TextArea;
     private JTextField TextField;
     private Socket Client;
     private ObjectOutputStream Out;
     private ObjectInputStream In;
     private EncryptedClient()
          super("Kaps's Encrypted Chat Client");
          try
               BufferedReader SystemIn = new BufferedReader(new InputStreamReader(System.in));
               System.out.print("Enter the IP of the server you would like to join: ");
               String CurrentLine = SystemIn.readLine();
               if(!CurrentLine.equals("local") && !CurrentLine.equals(""))
                    IP = CurrentLine;
               System.out.print("Enter the port of the server you would like to join: ");
               CurrentLine = SystemIn.readLine();
               if(!CurrentLine.equals(""))
                    Port = Integer.parseInt(CurrentLine);
               System.out.print("Enter your nick for the server you would like to join: ");
               Nick = SystemIn.readLine();
               System.out.print("Enter the password of the server you would like to join: ");
               Password = SystemIn.readLine().toCharArray();
               TextArea = new JTextArea();
               TextField = new JTextField();
               JPanel Panel = new JPanel(new BorderLayout());
               TextArea.setEditable(false);
               TextField.addActionListener(new ActionListener()
                    public void actionPerformed(ActionEvent e)
                         try
                              Out.writeObject(Nick + ": " + TextField.getText());
                              Out.flush();
                              TextField.setText("");
                         catch(Throwable t) {}
               Panel.add(new JScrollPane(TextArea), BorderLayout.CENTER);
               Panel.add(TextField, BorderLayout.SOUTH);
               Panel.setBorder(new TitledBorder(new EtchedBorder(), "Kaps's Encrypted Chat Client"));
               addWindowListener(new WindowAdapter()
                    public void windowClosing(WindowEvent e)
                         try
                              System.exit(0);
                         catch(Throwable t) {}
               getContentPane().add(Panel);
               setSize(640, 480);
               setVisible(true);
               Client = new Socket(IP, Port);
               Out = new ObjectOutputStream(new CipherOutputStream(Client.getOutputStream(), GetCipher(Cipher.ENCRYPT_MODE)));
               In = new ObjectInputStream(new CipherInputStream(Client.getInputStream(), GetCipher(Cipher.DECRYPT_MODE)));
               Out.writeObject(Nick);
               Out.flush();
               while(Client.isBound() && Client.isConnected())
                    TextArea.append((String)In.readObject() + "\n");
          catch(Throwable t) {}
     public static void main(String[] args)
          try
               new EncryptedClient();
          catch(Throwable t) {}
     private Cipher GetCipher(int Mode)
          Cipher PBECipher = new NullCipher();
          try
               PBECipher = Cipher.getInstance("PBEWithMD5AndDES");
               PBECipher.init(Mode, SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(new PBEKeySpec(Password)), new PBEParameterSpec(Salt, Count));
          catch(Throwable t) {}
          return PBECipher;
}

Similar Messages

Maybe you are looking for

  • SharePoint 2013 People Picker to Send Email

    I am having trouble finding a solid answer so I'm hoping someone here has it. Some questions get close, but something isn't matching up. Here is the need: User fills out a form in a SharePoint list. (the form does not ever open in InfoPath, though th

  • How do I copy or export my mobileme contacts into a hotmail account

    How do I copy or export my mobileme contacts into a hotmail account.  I thought this could be done through Outlook 2010 but it can't as far as I can see

  • Grid Control showing excessive amount of disk utilization.

    Hi Folks, I understand that Oracle doesn't support their products 100% on non Oracle VMs, but I thought I might take a stab at it. I decided to stick with a simple installation of Oracle Grid Control running on a Windows 2003 R2 32 bit os, here are t

  • Adding Alternative Unit of Measure to Outbound idoc

    Hi gurus, I need to add alternative unit of measure information to an outbound ORDERS idoc for each line item segment. How do I know what segments and qualifiers to use? Thanks! Will

  • Color vs browser problems

    Hi, I just uploaded a test page to a remote host. It uploaded fine, and looks fine on my mac, using safari. But when others look at it using Firefox or explorer, the background color doesn't match the image background colors. One of these folks said