JAVA   38

DH Server

Guest on 14th August 2022 06:42:36 AM

  1. // Sample use of Java classes MessageDigest and SecureRandom.  See the Java
  2. // API for more information.  Recall, MessageDigest supplies hash objects and
  3. // SecureRandom supplies a crytographically strong random number (a large
  4. // number with a high probability of being prime).
  5. import java.io.*;
  6. import java.math.BigInteger;
  7. import java.net.*;
  8.  
  9. public class DH_Server {
  10.    public static void main (String arg[]) {
  11.       InputStreamReader isr;
  12.       BufferedReader in;
  13.       PrintWriter out;
  14.  
  15.       try {
  16.          DiffieHellmanExchange dh = new DiffieHellmanExchange("DHKey");
  17.          
  18.          // Wait for a connection from a client then connect
  19.          ServerSocket socket = new ServerSocket(8284);
  20.          Socket connect = socket.accept();
  21.          isr = new InputStreamReader(connect.getInputStream());
  22.          in = new BufferedReader(isr);
  23.          out = new PrintWriter (connect.getOutputStream(), true);
  24.            
  25.          // Receive public key from the client
  26.          BigInteger pkey = new BigInteger(in.readLine());
  27.          System.out.println("Server: client's public key = "+pkey.toString());
  28.          dh.s_secret = pkey.modPow(dh.x, dh.key.p);      
  29.          out.println(dh.x_pub.toString());
  30.          System.out.println("Server: server's public key = "+dh.x_pub.toString());
  31.          System.out.println("Server: secret = "+dh.s_secret);
  32.  
  33.          // Leave
  34.          in.close();
  35.          out.close();
  36.          socket.close();
  37.       } catch (Exception e) {
  38.          System.out.println("Whoops! - no network");
  39.       }
  40.    }
  41. }

Raw Paste


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