×

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

Order of Execution of Constructors in Java Inheritance

Constructor in Java There are several distinctions between a method and a function object() in Java. The name of the function object()  is identical to the class name. There is no...

4 minutes read.

How to add double quotes in a string in Java

Strings are indicated using double-quotes. Double quotes are not printed; the values inside the double quotes are printed. There are different methods for adding double quotes to the string, such...

2 minutes read.

How many ways to create object in Java?

In this article, you will be acknowledged about the different ways to create an object in java. So far you construct an object from a class, as is common knowledge,...

6 minutes read.

Counting sort in Java

An array's elements are sorted using the counting sort method, which counts how many times each distinct element appears in the array. The count is kept in an auxiliary array,...

3 minutes read.

Shallow copy in Java

Java's most important task is making a copy or clone of an object. In this part, we'll talk about shallow copies in Java and how to make them of Java...

4 minutes read.

Monsoon Umbrella Problem in Java

The Monsoon Umbrella problem is a classic Java programming problem used to test the skills of a programmer. The problem involves writing a program to determine the number of umbrellas...

3 minutes read.

Sierpinski Number in Java

The Sierpinski triangle—is it a fractal? The Sierpinski Triangle fractals. A self-similar fractal is the Sierpinski triangle. It is made of an equilateral triangle with its residual area successively reduced by...

3 minutes read.

Java Extends keyword

Extends Extends is a keyword which is completely depended on the concept of the inheritance of the java programming language.To understand about of the keyword, we need to learn the concept...

3 minutes read.

Hierarchy of operators in Java

Operators are the most frequently used terminology in any area of programming, and it helps in various approaches to efficiently solve daily life problems by computer programming. The simple addition of...

4 minutes read.

Java program to determine whether all leaves are at same level

In this program, we must determine whether or not all of the binary tree's leaves are at the same level. If a node has no child nodes, it is said to...

3 minutes read.

Difference Between Access Specifiers and Modifiers in Java

Java employs access modifiers to restrict a class's data members, member functions, and constructor. Access modifiers are essential when creating Java program and applications. Access modifiers in Java include: defaultpublicprotectedprivate Default Access Modifiers Without...

4 minutes read.

Singleton class in Java

What is Singleton class in Java? Singleton means it is one. That means we can create only one instance or object of a class. For example, let us have a class...

3 minutes read.

Java Trim

Leading and leaving spaces are removed by the built-in Java String trim() method. Space seems to have the Unicode element of "x0040." Java trim() function looks for all of these...

3 minutes read.

How to download Eclipse for Java

Introduction: We write a java program, and when we want to run it, we need software to run it. Eclipse is a kind of software used to execute a JavaFX...

3 minutes read.

JDBC Program in Java

JDBC Program in Java JDBC is an API that defines how a client may access a database. It is a part of Java Standard Edition (Java SE). JDBC stands for Java...

4 minutes read.

Multithreading Program in Java

Multithreading Program in Java: Before discussing multithreading, it is important to discuss threads. Threads are the most fundamental part of a process. A process can have one or more threads....

4 minutes read.

Producer consumer problem in Java using Synchronised block

The producer-consumer problem in Java, commonly known as the bounded-buffer problem, is a well-known multi-process synchronization challenge where we attempt to synchronize many processes. Two processes are involved in the producer-consumer problem:...

3 minutes read.

Difference between JDK, JRE and JVM In Java

In the Java ecosystem, three primary components are often mentioned: JDK, JRE, and JVM. Here’s a breakdown of each: Java Development Kit (JDK) The Java Development Kit (JDK) is a comprehensive software...

2 minutes read.

Why we use static in Java

Many reserved keywords in Java cannot be used as variable names or identifiers. Java uses static keywords frequently because of its effective memory management system. In most cases, you must...

3 minutes read.

Replace character in string Java

Characters in Java In the package of Java language, there is a container class called Character. A single field of type char is contained in a Character object. For manipulating characters,...

4 minutes read.