-
Notifications
You must be signed in to change notification settings - Fork 0
/
User_B_Deffi_Hel.java
67 lines (47 loc) · 1.74 KB
/
User_B_Deffi_Hel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package cyberSecurity;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class User_B_Deffi_Hel {
static int moduloOf(int num,int pow,int n)
{
int result=1;
while(pow>0)
{
if(pow%2==1)
{
result=(result*num)%n;
}
num=(num*num)%n;
pow=pow/2;
}
return result%n;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try{
ServerSocket ss=new ServerSocket(6767);
Socket s=ss.accept();
int prime;
System.out.println("Enter public prime number (P): ");
prime= scanner.nextInt();
System.out.println("Enter Choosen primitive root (G)");
int g=scanner.nextInt();
System.out.println("User B enter your Secret key (a):");
int B = scanner.nextInt();
int y = moduloOf(g,B,prime);
System.out.println("User B your calculated public key is (Y):"+y);
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF(String.valueOf(y));
System.out.println("Recieved Public key of User A is (X) :"+str);
System.out.println();
System.out.println("Kb : " + moduloOf(Integer.parseInt(str),B,prime));
ss.close();
scanner.close();
}catch(Exception e){System.out.println(e);}
}
}