JAVA   101

KarnTestServer

Guest on 14th August 2022 06:45:48 AM

  1. import java.io.*;
  2. import java.net.*;
  3.  
  4. public class KarnTestServer {
  5.    public static void main (String arg[]) {
  6.       BufferedReader in;
  7.       PrintWriter out;
  8.       DiffieHellmanExchange dhe = null;
  9.  
  10.       // Use Diffie Hellman to create a shared secret
  11.       try {  dhe = new DiffieHellmanExchange("DHKey"); }
  12.       catch (Exception e) {
  13.          System.out.println("Error getting DHKey from file.");
  14.          System.exit(1);
  15.       }
  16.  
  17.       try {
  18.          // Wait for a connection from a client then connect
  19.          ServerSocket socket = new ServerSocket(8280);
  20.          Socket connect = socket.accept();
  21.          in = new BufferedReader(
  22.                   new InputStreamReader(connect.getInputStream()));
  23.          out = new PrintWriter (connect.getOutputStream(), true);
  24.  
  25.          // Build a Karn encryptor from the shared secret
  26.          Karn karn = new Karn(dhe.computeSecret(in, out));
  27.  
  28.          // Receive encrypted message from client and decrypt
  29.          String ciphertext = in.readLine();
  30.          System.out.println("Server: ciphertext:"+ciphertext+"\n");
  31.          String plaintext = karn.decrypt(ciphertext);
  32.          System.out.println("Server: plaintext:"+plaintext);
  33.  
  34.          // Leave
  35.       } catch (Exception e) {
  36.          System.out.println("Whoops! - no network");
  37.       }
  38.    }
  39. }

Raw Paste


Login or Register to edit or fork this paste. It's free.