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
-
Just purchased a song through iTunes on my MacBook which I have owned for years using the same Apple ID and all of the sudden I am not authorized to listen to this song only? Why? How do I fix it cause it's making me very mad...
-
My Ipad continues to sound off a loud trumpet fanfare periodically without reason. Is this a significant malware? If so, can I find it and delete it?
-
I have this TV and have been hooked up to cable, then to Direct TV for the last two years. I want to hook up to an Antenna however wanted to know if I need a digital converter box. Also if someone can tell me how to hook this up easily. I am more vis
-
How do I command a single video track (with many clips) to an action?
Hi, This is my first post in this forum, and I am a beginner on FCE. I want to take one of my video tracks and reduce the size to 1/4 and put it in the upper right corner of the screen. I want my screen split into 4 equal sections. My first track has
-
Cross using of MovieClips and externally constructed MovieClips in 2 SWF-Files
Hello, I have two SWF-Movies. One is exported into eht other as Child. Can I instance the Objects of the two libraries in all places, where ActionScript is possible? (In Keyframes of the MovieClips and in external class definitions.)