ECC_Client.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import java.io.*;
  2. import java.net.Socket;
  3. import java.util.Scanner;
  4. public class ECC_Client {
  5. static long P,A,B,Gx,Gy,N;
  6. public static void main(String[] args) throws IOException
  7. {
  8. long random,Cx=0,Cy=0;
  9. int count=1,len=0;
  10. Scanner scan=new Scanner(System.in);
  11. long[] result=new long[2],input=new long[2],SharedSec=new long[2];
  12. System.out.println("Enter P");
  13. P=scan.nextLong();
  14. System.out.println("Enter a");
  15. A=scan.nextLong();
  16. System.out.println("Enter b");
  17. B=scan.nextLong();
  18. System.out.println("Enter Gx");
  19. Gx=scan.nextLong();
  20. System.out.println("Enter Gy");
  21. Gy=scan.nextLong();
  22. System.out.println("Enter N");
  23. N=scan.nextLong();
  24. System.out.println("Enter random number between 1 to "+(N-1));
  25. random=scan.nextLong();
  26. result[0]=Gx;
  27. result[1]=Gy;
  28. for(int i=1;i<random;i++)
  29. {
  30. result=Same_Points_Addition(result);
  31. count++;
  32. }
  33. System.out.print("Public key is: ");
  34. System.out.print("("+result[0]+",");
  35. System.out.print(result[1]+")\n");
  36. if(result[0]<0)
  37. result[0]=-result[0];
  38. if(result[1]<0)
  39. result[1]=-result[1];
  40. Socket soc = new Socket("localhost", 6009);
  41. System.out.println("Client is ready");
  42. OutputStream outputStream = soc.getOutputStream();
  43. PrintWriter printwriter = new PrintWriter(outputStream, true);
  44. InputStream inputStream = soc.getInputStream();
  45. //Receive Data
  46. len=inputStream.read();
  47. for(int i=0;i<len;i++)
  48. {
  49. input[i]=inputStream.read();
  50. }
  51. //Send Data
  52. len=result.length;
  53. outputStream.write(len);
  54. for(int i=0;i<len;i++)
  55. {
  56. outputStream.write((int) result[i]);
  57. }
  58. printwriter.flush();
  59. //
  60. Cx=input[0];
  61. Cy=input[1];
  62. System.out.println("Partial Key is: ("+Cx+","+Cy+")");
  63. SharedSec[0]=Cx;
  64. SharedSec[1]=Cy;
  65. for(int i=1;i<random;i++)
  66. {
  67. SharedSec=Same_Points_Addition(SharedSec);
  68. }
  69. if(SharedSec[0]<0)
  70. SharedSec[0]=-SharedSec[0];
  71. if(SharedSec[1]<0)
  72. SharedSec[1]=-SharedSec[1];
  73. System.out.println("Shared Secret is ("+SharedSec[0]+","+SharedSec[1]+")");
  74. }
  75. static long[] Same_Points_Addition(long[] input)
  76. {
  77. long[] result=new long[2];
  78. double lambda=0;
  79. long x=input[0],y=input[1];
  80. if(y!=0)
  81. lambda=(((3*(x*x))+A)/(2*y));
  82. result[0]=(long) (((lambda*lambda)-x-x)%P);
  83. result[1]=(long) (((lambda*(x-result[0])-y))%P);
  84. return result;
  85. }
  86. }
  87. /*
  88. *OUTPUT:
  89. ------------------------------------------
  90. *Server:
  91. Enter P
  92. 13
  93. Enter a
  94. 2
  95. Enter b
  96. 4
  97. Enter Gx
  98. 3
  99. Enter Gy
  100. 5
  101. Enter N
  102. 23
  103. Enter random number between 1 to 22
  104. 5
  105. Public key is: (2,6)
  106. Server is ready
  107. Partial Key is: (9,10)
  108. Shared Secret is (9,10)
  109. ------------------------------------------
  110. Client:
  111. Enter P
  112. 13
  113. Enter a
  114. 2
  115. Enter b
  116. 4
  117. Enter Gx
  118. 3
  119. Enter Gy
  120. 5
  121. Enter N
  122. 23
  123. Enter random number between 1 to 22
  124. 13
  125. Public key is: (9,10)
  126. Client is ready
  127. Partial Key is: (2,6)
  128. Shared Secret is (9,10)
  129. */