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 1 | User 2 |
P, G are the publicly available keys | P, G are the publicly available keys |
The confidentially selected key is ‘a’ | The confidentially selected key is ‘b’ |
To generate key X= G^a modP | To generate key Y= G^b modP |
After swapping keys, user 1 will be provided with y | After swapping keys, user 2 will be provided with x |
Using the key they were given, User1 creates a secret key: Ka = y^a modP | Using 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