Diffie Hellman Key Exchange Algorithm (Java)

import java.util.*;
class DiffieHellman
public static void main(String args[])
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the value of Xa & Xb”);
int Xa=sc.nextInt();
int Xb=sc.nextInt();
System.out.println(“Enter a Prime no. p”);
int p=sc.nextInt();
System.out.println(“Enter Primitive Root a, such that a<p”);
int a=sc.nextInt();
int Ya=(int)((Math.pow(a,Xa))%p);
int Yb=(int)((Math.pow(a,Xb))%p);
int Ka=(int)((Math.pow(Yb,Xa))%p);
int Kb=(int)((Math.pow(Ya,Xb))%p);
System.out.println(“Transmission successful”);
System.out.println(“Transmission failed”);


4 thoughts on “Diffie Hellman Key Exchange Algorithm (Java)

  1. Hello Mr. Gajara,
    Your code is simple and easily understandable. One small suggestion would be to get the random numbers from the users and then compute Xa and Xb, and from there, it is perfect. Thank you for your code

    1. Hey Alexandria,

      Glad that it helps. Good suggestion, I had written this some 4 years back, haven’t been doing much coding since past 2 years. Could you just add the modification in the comment?

      Also, I had prefer Darshan šŸ™‚

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.