Write a Java program to implement the Diffie-Hellman Key Exchange mechanism | Diffie Hellman Key Exchange in Java | Cryptography
AIM:
Implement the Diffie-Hellman Key Exchange
mechanism using HTML and JavaScript.
HARDWARE
AND SOFTWARE REQUIREMENT:
1. Intel based Desktop PC: - RAM of
512 MB
2. Notepad/Notepad ++
editor
3. Net beans / Eclipse
THEORY:
Ø Diffie Hellman (DH) key exchange algorithm is a way for
securely exchanging cryptographic keys over internet(Public channel). Keys are
not actually exchanged – they are jointly derived.
Ø
If 1st Person and 2nd
Person want to communicate with each other, they first agree between them a
large prime number P, and a
generator (or base) G (where 0 < G
< P).
Ø 1st Person select a
secret integer “a” (his private key) and then calculates x=Ga mod P (which is his public key). 2nd
Person chooses his private key “b’,
and calculates his public key y=Gb mod P
Ø
1st Person and 2nd
Person exchange values of x and y.
Ø
1st Person and 2nd
Person compute symmetric keys by using
Ka=ya
mod P and kb= xb mod P
Ø Example:
Step 1 : 1st Person and 2nd Person get
public numbers P = 23,G = 9
Step 2: 1st Person selected a private key a
= 4 and 2nd Person selected a private key b = 3
Step 3: 1st Person and 2nd Person compute public
values
1st Person : x =(94 mod 23) = (6561 mod 23) =
6
2nd Person: y = (93 mod 23) = (729 mod
23) = 16
Step 4: 1st Person and 2nd Person exchange public
numbers
Step 5: 1st Person receives public key y =16 and 2nd
Person receives public key x = 6
Step 6: 1st Person and 2nd Person compute symmetric
keys
1st
Person : ka = ya
mod p = 65536 mod 23 = 9
2nd Person: kb = xb mod p = 216
mod 23 = 9
Step 7: 9 is the shared secret
SOURCE
CODE:
import java.util.*;
class DiffieHellman
{
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("Enter 1st secret
number(1st Person)");
int a=sc.nextInt();
System.out.println("Enter 2nd secret number(2nd Person)");
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("1st Person
and 2nd Person can communicate with each other!!!");
System.out.println("They share
a secret number = "+S_A);
}
else
{
System.out.println("1st Person
and 2nd Person cannot communicate with each other!!!");
}
}
}
OUTPUT:
VIVA QUESTIONS:
1. What
is JAVA?
2. What
is Diffie-Hellman Algorithm?
3. What
is java script?
4. What
is Cryptography?
5. What
is Encryption?
6. What
is Decryption?
Comments
Post a Comment