-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_Diffie_Hellman_Key_Exchange_algorithm.java
More file actions
43 lines (32 loc) · 1.05 KB
/
5_Diffie_Hellman_Key_Exchange_algorithm.java
File metadata and controls
43 lines (32 loc) · 1.05 KB
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
public class DHK {
private static long power(long a, long b, long p) {
long result = 1;
a = a % p;
while (b > 0) {
if (b % 2 == 1)
result = (result * a) % p;
b = b >> 1;
a = (a * a) % p;
}
return result;
}
public static void main(String[] args) {
long q, a, xa, xb, ya, yb, ka, kb;
q = 23;
System.out.println("The value of q: " + q);
a = 9;
System.out.println("The value of a: " + a);
xa = 4;
System.out.println("The private key a for Alice: " + xa);
ya = power(a, xa, q);
System.out.println("The public key of Alice: " + ya);
xb = 3;
System.out.println("The private key b for Bob: " + xb);
yb = power(a, xb, q);
System.out.println("The public key of Bob: " + yb);
ka = power(yb, xa, q);
kb = power(ya, xb, q);
System.out.println("Secret key for Alice is: " + ka);
System.out.println("Secret key for Bob is: " + kb);
}
}