NS9_userA.java 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.*;
  4. public class NS9_userA {
  5. public static int find_pow(int a, int b,int c) {
  6. int i,final_ans=1;
  7. for(i=0;i<b;i++)
  8. final_ans=(final_ans*(a%c))%c;
  9. return final_ans%c;
  10. }
  11. public static void main(String[] args) throws IOException {
  12. Scanner sc = new Scanner(System.in);
  13. System.out.println("Enter value of P");
  14. int P = sc.nextInt();
  15. System.out.println("Enter value of a");
  16. int a = sc.nextInt();
  17. System.out.println("Enter value of Xa");
  18. int Xa = sc.nextInt();
  19. int Ya;
  20. Ya=find_pow(a,Xa,P);
  21. ServerSocket ss = null;
  22. Socket s = null;
  23. ss = new ServerSocket(7777);
  24. s = ss.accept();
  25. DataOutputStream dos = new DataOutputStream(s.getOutputStream());
  26. DataInputStream dis = new DataInputStream(s.getInputStream());
  27. dos.writeUTF(Integer.toString(Ya));
  28. String Yb = dis.readUTF();
  29. int k;
  30. k=find_pow(Integer.parseInt(Yb),Xa,P);
  31. System.out.println("Key is "+k);
  32. }
  33. }