import java.net.*; import java.io.*; import java.security.*; import java.security.spec.*; import javax.crypto.*; import javax.crypto.spec.*; import javax.swing.*; //import skip.java; public class Client { Socket s; InputStream in; OutputStream out; Client() { } public void Communication () { try { String host = "127.0.0.1"; //int port = Integer.parseInt( args[1] ); int port = 4444; // String outfile = args[2]; // Create a Diffie-Hellman key pair (public and private). KeyPairGenerator kpg = KeyPairGenerator.getInstance( "DH" ); kpg.initialize(Skip.sDHParameterSpec); KeyPair LukekeyPair = kpg.genKeyPair(); // Create socket and contact host. Socket s = new Socket( host, port ); // Wait to be recognized. DataInputStream myin = new DataInputStream( s.getInputStream() ); DataOutputStream myout = new DataOutputStream( s.getOutputStream() ); // Send our public key to host. byte[ ] keyBytes = LukekeyPair.getPublic().getEncoded(); myout.writeInt( keyBytes.length ); myout.write( keyBytes ); // Accept public key from host (length, key in bytes). keyBytes = new byte[ myin.readInt( ) ]; myin.readFully( keyBytes ); KeyFactory kf = KeyFactory.getInstance( "DH" ); X509EncodedKeySpec x509Spec = new X509EncodedKeySpec( keyBytes ); PublicKey HanPublicKey = kf.generatePublic(x509Spec); // Generate the secret session key. KeyAgreement ka = KeyAgreement.getInstance( "DH" ); ka.init( LukekeyPair.getPrivate() ); ka.doPhase( HanPublicKey, true ); byte[] secret = ka.generateSecret(); // in c:\jdk1.2.2\jre\classes\edu.shsu.util.BASE64 // System.out.println( edu.shsu.util.BASE64.encode(secret) ); // FileOutputStream fout = new FileOutputStream(outfile); // First create a key specification from the password, then the key. //byte[] desKeyData = password.getBytes(); DESKeySpec desKeySpec = new DESKeySpec(secret); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey desKey = keyFactory.generateSecret(desKeySpec); System.out.println( BASE64.encode(secret) ); // establish conection //s = new Socket("127.0.0.1", 4444); //initiate I/O stream in = s.getInputStream(); out = s.getOutputStream(); //communication... SendMessage("Connection established... Thank you."); GetMessage(); SendMessage("Good Morning This is a Test from Client\n"); GetMessage(); SendMessage("Thank you..."); GetMessage(); //close connection out.close(); in.close(); s.close(); } catch (Exception e) { } } public static void main(String arg[]) { Client client = new Client(); client.Communication(); } public void SendMessage(String msg) { try { byte b[] = new byte [100]; b = msg.getBytes(); out.write(b); out.flush(); } catch(Exception e) {} } // public void SendMessage(int msg) /// { /// try /// { // out.write(msg); // out.flush(); // } // catch(Exception e) {} // } public void GetMessage() //void GetMessage() { try { //String outfile = "Out1.txt"; FileOutputStream fout = new FileOutputStream(outfile); // Read the initialization vector. int ivSize = myin.readInt(); byte[] iv = new byte[ivSize]; myin.readFully(iv); IvParameterSpec ivps = new IvParameterSpec(iv); // use Data Encryption Standard Cipher des = Cipher.getInstance("DES/CBC/PKCS5Padding"); des.init(Cipher.DECRYPT_MODE, desKey, ivps); // Accept the encryped transmission, decrypt, and save in file. byte[] input = new byte[64]; while (true) { int bytesRead = myin.read(input); if (bytesRead == -1) break; byte[] output = des.update(input, 0, bytesRead); if (output != null) { // fout.write(output); System.out.print("Server --> " + new String(output) ); } } byte[] output = des.doFinal(); if (output != null) { // fout.write(output); System.out.print("Server --> " + new String(output) ); } // fout.flush(); // fout.close(); myout.close(); myin.close(); } // { byte b[] = new byte[100]; // in.read(b); // System.out.println("Server --> "+ new String(b)); // } catch(Exception e){} // } // fout.flush(); // fout.close(); // myout.close(); // myin.close(); } }