SAFE-CHAT By Thang Tran Table of Contents I. What is Safe-Chat? A. Internet Insecurity B. Safe-Chat and Encryption C. Java II. Safe-Chat Development Team III. Requirements IV. Inputs and Outputs V. Project Timeline VI. How To Use Safe-Chat VII. Appendix A - SafeClient.java VIII. Appendix B – SafeServer.java IX. Appendix C – SafeCrypt.java X. References XI. What is Safe-Chat? With many people using the Internet for communication, privacy becomes the top concern for users. Either at home or work, your data can be intercepted. Many companies are employing software to intercept emails and chat messages from their own employees. Hackers can intercept data on transit with data sniffing tools. The only way for secure and private communication is with encryption. Safe-Chat provides a solution to today’s privacy problems. Safe-Chat is an Internet chat program. Safe-Chat will encrypt a user’s message upon sending the message, using the user’s encryption key. Upon receipt of an encrypted message, Safe-Chat will decrypt the message for the recipient. Encryption before the message can leave the user’s computer will ensure your data security. Only upon receipt of a specified user can Safe-Chat decrypt the message. Safe-Chat uses Instant Message style of communication and network tunneling. Users connect to another through a point to point secure tunnel. To avoid third party ease dropping and encryption key interception, point to point communication was employed. Safe-Chat is built on Java technology to provide platform independence. Thus allowing Safe-Chat to work on Windows computers, Unix computers, Macintosh computers, Pocket PCs, Palms, cellular phones with a JVM, and many other devices. Java will allow easy distribution of Safe-Chat to many platforms and users. Safe-Chat Development Team Thang Tran Project Manager Designer Developer/Programmer Tester Distributor Recorder REQUIREMENTS Internet Connection Computer or Handheld device with a Java Virtual Machine INPUTS User Name Plain Text Chat Message OUTPUTS Decrypted Plain Text Chat Message TIMELINE HOW TO USE SAFE-CHAT Startup server side Wait for a connection to establish Setup Client Side Once connected you may begin to chat securely To terminate the chat session, type “/quit” in the chat window (any side can terminate the session). SAFECLIENT.JAVA // Created by Thang Tran // Program: SafeChat.java // Description: This is the user interface client to Safe-Chat. // SafeClient impliments SafeCrypt for // encryption and decryption. // // Last Updated: // Reason Updated: import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SafeClient extends JFrame { private JTextField enter; private JTextArea display; ObjectOutputStream output; ObjectInputStream input; String message = ""; public SafeClient() { super( "SafeClient" ); Container c = getContentPane(); enter = new JTextField(); enter.setEnabled( false ); enter.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { sendData( e.getActionCommand() ); } } ); c.add( enter, BorderLayout.SOUTH ); display = new JTextArea(); c.add( new JScrollPane( display ), BorderLayout.CENTER ); setSize( 300, 400 ); show(); } public void runSafeClient() { Socket client; try { // Create a Socket to make connection. display.setText( "Attempting connection\n" ); client = new Socket( InetAddress.getByName( "127.0.0.1" ), 5000 ); display.append( "Connected to: " + client.getInetAddress().getHostName() ); // Get the input and output streams. output = new ObjectOutputStream( client.getOutputStream() ); output.flush(); input = new ObjectInputStream( client.getInputStream() ); // Process connection. enter.setEnabled( true ); do { try { message = (String) input.readObject(); // Decrypt the data stream message = SafeCrypt.decrypt(message); display.append( "\n" + message ); display.setCaretPosition( display.getText().length() ); } catch ( ClassNotFoundException cnfex ) { display.append( "\nUnknown object type received" ); } } while ( !message.equals( "SERVER>>> /quit" ) ); // Step 4: Close connection. display.append( "\nClosing connection.\n" ); output.close(); input.close(); client.close(); } catch ( EOFException eof ) { System.out.println( "Server terminated connection" ); } catch ( IOException e ) { e.printStackTrace(); } } private void sendData( String s ) { String eBuffer = ""; String temp = ""; try { message = s; // Encrypt the message temp = "CLIENT>>> " + s; eBuffer = SafeCrypt.encrypt(temp); output.writeObject(eBuffer ); output.flush(); display.append( "\nCLIENT>>>" + s ); } catch ( IOException cnfex ) { display.append( "\nError writing object" ); } } public static void main( String args[] ) { SafeClient app = new SafeClient(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); app.runSafeClient(); } } BSAF SAFESERVER.JAVA // Created by Thang Tran // Program: SafeSafeClient.java // Description: This is the chat server to Safe-Chat. // SafeServer impliments SafeCrypt for // encryption and decryption. // // Last Updated: // Reason Updated: import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SafeServer extends JFrame { private JTextField enter; private JTextArea display; ObjectOutputStream output; ObjectInputStream input; String message = ""; public SafeServer() { super( "SafeServer" ); Container c = getContentPane(); enter = new JTextField(); enter.setEnabled( false ); enter.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { sendData( e.getActionCommand() ); } } ); c.add( enter, BorderLayout.SOUTH ); display = new JTextArea(); c.add( new JScrollPane( display ), BorderLayout.CENTER ); setSize( 300, 400 ); show(); } public void runSafeServer() { ServerSocket server; Socket connection; int counter = 1; try { // Create a SafeServerSocket. server = new ServerSocket( 5000, 100 ); while ( true ) { // Wait for a connection. display.setText( "Waiting for connection\n" ); connection = server.accept(); display.append( "Connection " + counter + " received from: " + connection.getInetAddress().getHostName() ); // Get input and output streams. output = new ObjectOutputStream( connection.getOutputStream() ); output.flush(); input = new ObjectInputStream( connection.getInputStream() ); enter.setEnabled( true ); do { try { message = (String) input.readObject(); message = SafeCrypt.decrypt(message); display.append( "\n" + message ); display.setCaretPosition( display.getText().length() ); } catch ( ClassNotFoundException cnfex ) { display.append( "\nUnknown object type received" ); } } while ( !message.equals( "CLIENT>>> /quit" ) ); // Step 5: Close connection. display.append( "\nUser terminated connection" ); enter.setEnabled( false ); output.close(); input.close(); connection.close(); ++counter; } } catch ( EOFException eof ) { System.out.println( "Client terminated connection" ); } catch ( IOException io ) { io.printStackTrace(); } } private void sendData( String s ) { String eBuffer = ""; String temp = ""; try { message = s; // Encrypt the message temp = "SERVER>>> " + s; eBuffer = SafeCrypt.encrypt(temp); output.writeObject(eBuffer ); output.flush(); display.append( "\nSERVER>>>" + s ); } catch ( IOException cnfex ) { display.append( "\nError writing object" ); } } public static void main( String args[] ) { SafeServer app = new SafeServer(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); app.runSafeServer(); } } SAFECRYPT.JAVA // Created by Thang Tran // Program: SafeCrypt.java // Description: This program will encrypt and decrypt data. // // Last Updated: // Reason Updated: import java.util.*; import java.lang.Object; import java.lang.Math; import java.lang.Integer; import java.lang.*; import java.io.*; public class SafeCrypt { static public char keyval = 'x'; public SafeCrypt() { } // Encrypt data static public String encrypt(String inbuff) { char buffer[] = new char[inbuff.length()]; String edata; // Convert over to an array of chars buffer = inbuff.toCharArray(); for (int i = 0; i < inbuff.length(); i++) { buffer[i] = (char)(buffer[i]^keyval); } // Convert back to a string edata = String.copyValueOf(buffer); return edata; } // Decrypt data static public String decrypt(String inbuff) { char buffer[] = new char[inbuff.length()]; String ddata; // Convert over to an array of chars buffer = inbuff.toCharArray(); for (int i = 0; i < inbuff.length(); i++) { buffer[i] = (char)(buffer[i]^keyval); } // Convert back to a string ddata = String.copyValueOf(buffer); return ddata; } } REFERENCES Deitel H.M and P.J. Deitel, “Java How To Program – Covers Java 2 Introducing Swing,” Third Edition. Pentice Hall. 1999.