forked from sPredictorX1708/Ultimate-Java-Resources
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiffie_Hellman.java
More file actions
30 lines (29 loc) · 799 Bytes
/
Diffie_Hellman.java
File metadata and controls
30 lines (29 loc) · 799 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
import java.util.*;
public class Diffie_Hellman
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter modulo(p)");
int p=sc.nextInt();
System.out.println("Enter primitive root of "+p);
int g=sc.nextInt();
System.out.println("Choose 1st secret no(DEEPANSHU)");
int a=sc.nextInt();
System.out.println("Choose 2nd secret no(GOYAL)");
int b=sc.nextInt();
int A = (int)Math.pow(g,a)%p;
int B = (int)Math.pow(g,b)%p;
int S_A = (int)Math.pow(B,a)%p;
int S_B =(int)Math.pow(A,b)%p;
if(S_A==S_B)
{
System.out.println("DEEPANSHU and GOYAL can communicate with each other....");
System.out.println("They share a secret no = "+S_A);
}
else
{
System.out.println("DEEPANSHU and GOYAL cannot communicate with each other....");
}
}
}