×

Diffie Hellman Algorithm in Java

In this section, you will be acknowledged about Diffie Hellman algorithm clearly step wise along with an example and also an example program.

Diffie Hellman Algorithm

One of the most significant algorithms for creating a shared secret is Diffie-Hellman. We are able to communicate secretly when sending and receiving data across a public network by using the shared secret. For obtaining points and obtaining a security code using the parameters, we employ an elliptic curve.

  • We shall simply take into account four variables for the algorithm's sake of simple and practical implementation: one prime P as well as G (a primitive root of P), as well as two private values a and b.
  • Both P as well as G are readily accessible numbers. Users (let's suppose Alice and Bob) select confidential values a and b, create a key, and publicly trade it. The key is given to the other party, who then uses it to create a secret key, giving them access to the same confidential key for encryption.

Let us understand with step-by-step explanation.

User 1User 2
P, G are the publicly available keysP, G are the publicly available keys
The confidentially selected key is ‘a’The confidentially selected key is ‘b’
To generate key X= G^a modPTo generate key Y= G^b modP
After swapping keys, user 1 will be provided with yAfter swapping keys, user 2 will be provided with x
Using the key they were given, User1 creates a secret key: Ka = y^a modPUsing the key they were given, User1 creates a secret key: Kb = x^a modP.

The fifth step can be stated mathematically as follows:

Ka = Kb

It implies that the symmetrical encryption key to encrypt is known to both users.

Let us discuss an example to understand very clearly

Example:

  • Let us assume that user 1 and user 2 got public keys P=23 and g = 9
  • Let us assume that user1 selects 4 as confidential key. In the same manner user2 also selects 3 as confidential key.
  • The public value that has been calculated for user1 is
    X = (9^4 mod 23) = 6561 mod 23 = 6
  • The public value that has been calculated for user2 is
    Y = (9^3 mod 23) = 729 mod 23 = 16
  • User1 and user2 swap their public keys 6 and 16.
  • User1 shall obtain y=16 and user2 shall be obtained with x=6.
  • After the calculation of the symmetric keys of user1 and user2
    ka = y^a modP = 65536 mod 23 = 9
    kb = x^b modP = 216 mod 23 = 9
  • Hence, the common key is 9.

Now, let us write the program for this algorithm and execute.

File name: Hellman.java

// This application uses the Diffie-Hellman Key exchanging technique to determine the Key for two people.
class Hellman{

// Power method to provide the  value of a ^ b mod P
private static long power(long a, long b, long p)
{
if (b == 1)
return a;
else
return (((long)Math.pow(a, b)) % p);
}




public static void main(String[] args)
{
long P, G, x, a, y, b, ka, kb;

// The public keys G as well as P will be negotiated upon by both participants.

// A prime number P is taken
P = 23;
System.out.println("The value of P:" + P);

// A primitive root for P, G is taken
G = 9;
System.out.println("The value of G:" + G);

  //The user1 shall select his own confidential key
// The selected confidential key is a
a = 4;
System.out.println("The private key a for user1:" + a);

// Receives the obtained key
x = power(G, a, P);

// The user2 shall select his own confidential key
// The selected confidential key is b
b = 3;
System.out.println("The private key b for user2:" + b);


y = power(G, b, P);

// Obtaining a confidential key just after the keys are swapped
ka = power(y, a, P); // Confidential key for user1
kb = power(x, b, P); // Confidential key for user2

System.out.println("Confidential key for the user1 is:" + ka);
System.out.println("Confidential key for the user2 is:" + kb);
}
}

Output

The value of P:23
The value of G:9
The private key a for user1:4
The private key b for user2:3
Confidential key for the user1 is:9

Related Topics

How to Develop Programming Logic in Java?

Introduction In the world of software development, Java programming language is one of the most powerful programming languages that is used to create a wide range of applications. It includes desktop,...

18 minutes read.

Java Code Optimization

We encounter the idea of optimization while working on any Java application. It is essential that the code we write is not only clear and error-free but also optimized, meaning...

9 minutes read.

Convert list to array Java

One of the popular collection interfaces for storing an ordered collection is the List. The List interface may contain repeating groups and preserves the insertion order of entries. This article will...

4 minutes read.

Java Math sqrt() Method

The sqrt() method of Java Math class returns the accurately rounded positive square root of the specified double value. Syntax: public static double sqrt(double a) Parameters: The parameter ‘a’ represents the number whose square...

2 minutes read.

Hidden classes in Java

There specifically are some APIs available in the market that generally is harmful to be used in our programs specifically literally, and until JDK 15, there, for all intents and...

4 minutes read.

Enum Java Interview Questions

1. What exactly is Java? Java is a portable, high-level, object-oriented, robust, secure, platform-independent, multi-threaded, high-performance programming language. Developed in June 1991 by James Gosling, also known as Platform. 2. Enum is...

3 minutes read.

How to Convert boolean to String in Java

How to Convert boolean to String in Java There are two methods to convert boolean to String. Using valueOf(boolean) method Using toString(boolean) method For both the above methods, if the passes Boolean...

2 minutes read.

Java Thread class

Thread class The thread represents a part of the process. Every process can have multiple associated threads in which every thread may execute the same or different job. By default, each thread assigns...

16 minutes read.

Java Applications

The growth in technology is increasing rapidly, so some languages are used for developing them. Java is one such famous programming language which is having numerous applications. The Java Programming...

4 minutes read.

Java Null Keyword

Null is a term that is only used for literal values in Java. Although it appears to be a term, it is a literal opposite of true and false. Java's...

3 minutes read.

String Declaration in Java

A string is a group of characters. In Java, the string can be treated as both class and datatype. In Java programming, the String class have many advantages. Everything in...

3 minutes read.

Java instance variable

An instance in object-oriented programming (OOP) is a particular implementation of any object. Each realized version of an item, which might vary in several ways, is an instance. Instantiation is...

3 minutes read.

Catalan number in Java

In general mathematics, Catalan numbers can be defined as the sequence of natural numbers that frequently occur in counting problems often encountered in recursively defined objects. Mathematical formula of Catalan number Coming...

3 minutes read.

Java Thread Priority in Multithreading

As we realise, java, being object-situated, works inside a multithreading climate in which the string scheduler relegates the processor to a string in light of the need for a string....

6 minutes read.

Deque in Java

Deque in java collections with Example Deque is short for “double-ended queue.” It is a linear collection that extends the Queue interface and supports insertion and deletion of the element at both the...

3 minutes read.

Interchange Diagonal elements in Java

In this article, you will be very well acknowledged about what is a matrix with an example. You will also be acknowledged about how to interchange the diagonal elements in...

4 minutes read.

Different Ways to Take Input from User in Java

Any information provided to a system for use is known as user input. Any responsive software or application must include user input. In Java, there are 4 different ways to...

5 minutes read.

Narcissistic Number in Java

A Narcissistic number is made up of digits that have been added together and raised to powers equal to the number of digits in the original number. In those other...

3 minutes read.

Round Robin Scheduling Program in Java

A CPU scheduling technique is known as Round Robin (RR). Additionally, network schedulers employ it. It was created specifically for a time-sharing system. The temporal slicing scheduling algorithm is another...

4 minutes read.

Banking Application in Java

JDBC (Java Database Connectivity), which provides an API to connect to, execute, and fetch data from any databases, can be used to handle transactions in Java. There are several factors...

7 minutes read.