JAVA   87

KarnTestClient

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

  1. import java.io.*;
  2. import java.net.*;
  3.  
  4. public class KarnTestClient {
  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 {
  12.          dhe = new DiffieHellmanExchange("DHKey");
  13.       } catch (Exception e) {
  14.          System.out.println("Error in getting DHKey from file.");
  15.          System.exit(1);
  16.       }
  17.  
  18.       try {
  19.          // Connect to the server
  20.          Socket connect = new Socket("localhost", 8280);
  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.          // Encrypt plaintext from the command line and send it to Server
  29.          String plaintext = arg[0];
  30.          System.out.println("Client: plaintext:"+plaintext+"\n");
  31.          String ciphertext = karn.encrypt(plaintext);
  32.          System.out.println("Client: ciphertext:"+ciphertext+"\n");
  33.          out.println(ciphertext);
  34.  
  35.          // Leave
  36.       } catch (Exception e) {
  37.          System.out.println("Yikes!");
  38.       }
  39.    }
  40. }

Raw Paste


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