Server_Diffie_Hellman.java 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. import java.io.*;
  2. import java.net.ServerSocket;
  3. import java.net.Socket;
  4. import java.util.Scanner;
  5. public class Server_Diffie_Hellman {
  6. public static void main(String[] args) throws IOException
  7. {
  8. long alpha=7,P=11,K,Xb=23,Ya,Yb;
  9. ServerSocket servsocket = new ServerSocket(4500);
  10. System.out.println("Server is ready");
  11. Yb = (long) (Math.pow(alpha, Xb)%P);
  12. Socket socket = servsocket.accept();
  13. OutputStream outputStream = socket.getOutputStream();
  14. PrintWriter printwrite = new PrintWriter(outputStream, true);
  15. InputStream inputStream = socket.getInputStream();
  16. BufferedReader receiveRead = new BufferedReader(new InputStreamReader(inputStream));
  17. String receiveMessage;
  18. if((receiveMessage = receiveRead.readLine()) != null)
  19. {
  20. System.out.println("Ya is "+receiveMessage+" Yb is "+Yb);
  21. }
  22. Ya = Long.parseLong(receiveMessage);
  23. K = (long) (Math.pow(Ya, Xb)%P);
  24. printwrite.println(Yb);
  25. System.out.println("Key is "+K);
  26. printwrite.flush();
  27. }
  28. }