Client_Diffie_Hellman.java 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import java.io.*;
  2. import java.net.Socket;
  3. import java.util.Scanner;
  4. public class Client_Diffie_Hellman {
  5. public static void main(String[] args) throws IOException
  6. {
  7. long P,alpha,Xa,Ya,K,Yb;
  8. Scanner sc=new Scanner(System.in);
  9. System.out.println("Enter Alpha:");
  10. alpha=sc.nextLong();
  11. System.out.println("Enter P:");
  12. P=sc.nextLong();
  13. System.out.println("Enter Xa");
  14. Xa=sc.nextLong();
  15. Ya = (long) (Math.pow(alpha, Xa)%P);
  16. Socket soc = new Socket("localhost", 4500);
  17. OutputStream outputStream = soc.getOutputStream();
  18. PrintWriter pwrite = new PrintWriter(outputStream, true);
  19. InputStream inputStream = soc.getInputStream();
  20. BufferedReader receiveRead = new BufferedReader(new InputStreamReader(inputStream));
  21. System.out.println("Client is ready");
  22. String receiveMessage;
  23. pwrite.println(Ya);
  24. pwrite.flush();
  25. if((receiveMessage =receiveRead.readLine()) != null)
  26. {
  27. System.out.println("Ya is "+Ya+" Yb is "+receiveMessage);
  28. }
  29. Yb = Long.parseLong(receiveMessage);
  30. K = (long) (Math.pow(Yb, Xa)%P);
  31. System.out.println("Key is "+K);
  32. }
  33. }
  34. /*
  35. Server:
  36. Enter Alpha:
  37. 13
  38. Enter P:
  39. 23
  40. Enter Xb
  41. 13
  42. Server Available For Key Exchange
  43. Ya is 1 Yb is 8
  44. Key is 1
  45. Client:
  46. Enter Alpha:
  47. 13
  48. Enter P:
  49. 23
  50. Enter Xa
  51. 11
  52. Client Available For Key Exchange
  53. Ya is 1 Yb is 8
  54. Key is 1
  55. */