Professional Documents
Culture Documents
Theory:
The Diffie-Hellman Key Exchange algorithm is a fundamental method for two parties to
securely establish a shared secret key over an insecure communication channel. It was
introduced by Whitfield Diffie and Martin Hellman in 1976 and is a key component of
modern secure communication protocols. The algorithm relies on the mathematical properties
of modular exponentiation to achieve its goal.
The steps are:
1. The first party picks two prime numbers, g and p and tells them to the second party.
2. The second party then picks a secret number (a), and then it computes ga mod p and sends
the result back to the first party; suppose the result A. The secret number is not sent to
anyone, only the result is shared.
3. Then the first party does the same; it selects a secret number b and calculates the result B.
4. Then, this result is sent to the second party.
5. The second party takes the received number B and calculates Ba mod p
6. The first party takes the received number A and calculates Ab mod p Both parties will get
the same answer.
Source Code:
import java.util.*;
// create class DiffieHellmanAlgorithmExample to calculate the key for two persons
class diffie_hellman { // main() method start
public static void main(String[] args)
{
long P, G, x, a, y, b, ka, kb;
// create Scanner class object to take input from user
Scanner sc = new Scanner(System.in);
System.out.println("Both the users should be agreed upon the public keys G and P");
// take inputs for public keys from the user
System.out.println("Enter value for public key G:");
G = sc.nextLong();
System.out.println("Enter value for public key P:");
P = sc.nextLong();
// get input from user for private keys a and b selected by User1 and User2
System.out.println("Enter value for private key a selected by user1:"); a
= sc.nextLong();
System.out.println("Enter value for private key b selected by user2:");
b = sc.nextLong();
Output:
Submitted by:-
Name-Priya rai
Redg. No.- 2101020205
Branch-CSE(IOT&CS)
Group-8