-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDH.java
More file actions
37 lines (34 loc) · 934 Bytes
/
Copy pathDH.java
File metadata and controls
37 lines (34 loc) · 934 Bytes
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
import java.math.*;
import java.util.Random;
//DIFFIE-HELLMAN KEY EXCHANGE
//HW2 Q2
public class DH{
/*
* prime: 293040877
* primitive root: 11
* Ya: 204397779
*/
static int privateKey(){
//Random rand = new Random();
//int Xb = rand.nextInt(293040877);
int Xb = 300;
return Xb;
}
static BigInteger publicKey(BigInteger Xb, BigInteger p){
BigInteger Yb = new BigInteger("11");
Yb = Yb.modPow(Xb, p);
return Yb;
}
static BigInteger secretKey(BigInteger Xb, BigInteger p){
BigInteger K = new BigInteger("204397779");
K = K.modPow(Xb, p);
return K;
}
public static void main(String args[]){
int pubKey = privateKey();
BigInteger Xb = BigInteger.valueOf(pubKey);
BigInteger prime = new BigInteger("293040877");
System.out.println("My Public Key is: " + publicKey(Xb, prime));
System.out.println("The Shared Secret Key is: " + secretKey(Xb, prime));
}
}