×

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 convert list to String in Java

Sometimes, we need to transform a listing of characters into a string. A string is a chain of characters, so we will make a string from an individual array without...

4 minutes read.

Java Enum vs Class

Enumerations are used in programming languages to represent collections of named constants. For instance, the four suits in a deck of playing cards might represent four integrators named Club, Diamond,...

7 minutes read.

Beautiful Array in Java

In this section, we will be acknowledged about the beautiful array in Java, its characteristics, how it is achieved. What are the types of approaches and what are the tools...

8 minutes read.

Java delete directory

The File classes in Java may symbolize a directory or a file on the system. Inside the java.io package, the Files class is accessible. The File class has several helpful...

2 minutes read.

Quick Sort in Java

Quick Sort in Java Like merge sort, quick sort also uses the divide and conquer approach to sort the given array or list. In quick sort, the sorting of an array...

6 minutes read.

Sliding Window Problem in Java

A sliding window is used in computer science and data science to process large datasets. It involves breaking the dataset into smaller chunks or windows and then processing it in...

6 minutes read.

Bottom view of a binary tree in Java

The lowest nodes in their horizontal distance are present and referred to as the bottom view of a binary tree. The horizontal distance between the nodes of a binary tree...

4 minutes read.

Java Final Keyword

In Java, the last keyword is used to limit the user. The applications of the java final keyword have large range of usage in program development. Last can be: variablemethodclass A final...

3 minutes read.

How to Convert int to double in Java

How to Convert int to double in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

2 minutes read.

Java Create File

A File is a hypothetical way, which has no genuine presence. It is very much like while "using" that File that the major real activity of putting away something in...

5 minutes read.

What is String in Java?

What is String in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a Java...

4 minutes read.

Console Errors in Java

An unlawful motion taken through the person that reasons this system to act abnormally is amistake until this system is compiled or run; maximum programming mistakes pass unnoticed.The software is...

3 minutes read.

Balanced Prime Number in Java

This section will cover the definition of a balanced prime number as well as how to find one using a Java program. Balance Prime Number A prime number that is equivalent to...

5 minutes read.

Java Serialization

JAVA SERIALIZATION Serialization is a process by which objects can be represented as a sequence of bytes. These bytes have information about object's data, object's type and datatypes of members in...

3 minutes read.

Java Boolean hashCode() Method

The hashCode() method of Boolean class returns the hash code for the specified Boolean object or the given Boolean value. Syntax public int hashCode()public int hashCode(boolean value) Parameters The parameter ‘value’ represents the value...

2 minutes read.

Mutable and Immutable in Java

Java is a programming language in which everything is treated as an object. Its procedures and functions are centred around objects because it is an object-oriented programming language. Mutable and...

6 minutes read.

Longest Arithmetic Progression Sequence in Java

The task is to find the length of the largest sequence in an array to form an arithmetic progression. The array arr[] is given. Longest Arithmetic Progression Sequence in Java Algorithm set...

5 minutes read.

StringBuffer in Java

StringBuffer in Java Similar to StringBuilder, the Java StringBuffer class is also used to create modifiable or mutable strings. The StringBuilder class is synchronized, i.e., thread-safe. Java StringBuffer ConstructorThe StringBuffer class has...

7 minutes read.

Java String getChars() Method

Java String getChars() method copies characters from current String to the destination character array . Syntax: public void getChars(int srcBeginIndex, int srcEndIndex, char[] destination, int dstBeginIndex) Parameters: srcBegin - index of the first character...

1 minute read.

Java Database Connectivity with Oracle

JDBC: A Programmer can develop a complete application using the Java built-in API’s. So, for storing the data required for solving a real-world problem is stored into a database. To connect...

5 minutes read.