Skip to main content

A simple Java program to illustrate the Deffie-Hellman (DH) key exchange protocol.



           Deffie-Hellman key exchange protocol is a method which allows two parties to establish a shared secret key (session key) over an insecure channel without having to send that key through the channel. This is achieved by using private and public keys.This key can then be used to encrypt the messages sent between those two parties using a symmetric key cipher.

The algorithm is as follows.







This is the complete code written in Java.


import java.math.BigInteger;
public class DH{

public static void main(String[] args){
BigInteger g=new BigInteger("5");    //Let g be 5(a primitive root modulo 23)
BigInteger p=new BigInteger("23");   //Let p be 23(a prime number)
if (args.length==1){
BigInteger privateKey=new BigInteger(args[0]);  // Your Private key 
BigInteger publicKey=g.modPow(privateKey,p);    // Calculating the public key relevant to your private key
System.out.println("Public Key: "+publicKey.toString());
}
else if(args.length==2){
BigInteger privateKey=new BigInteger(args[0]);  // Your Private key
BigInteger publicKey=new BigInteger(args[1]);   // Public key of the other party
BigInteger sessionKey=publicKey.modPow(privateKey,p);  // Calculating the session key
System.out.println("Session Key: "+sessionKey.toString());
}
        else{
            System.out.println("Please provide the valid number of arguments."); //If the correct number of arguments are not provided
        }
}
}


This is a sample output that you get when running the program in command prompt in Windows.



When you give the private key of one party, it will calculate his/her public key.
Then when you give the private key of one party along with the public key of the other party, The session key(shared secret key) will be provided.
Note that this session key is the same for both parties. (It is not sent through the network, instead calculated by the 2 parties themselves.)

Comments

  1. Thank you akke, me prashnema assignment ekakata dunna, mama mekama upload kala

    ReplyDelete
  2. Apitath dunna ��

    ReplyDelete
  3. ada dnna tyna ekaneh mee..

    ReplyDelete
  4. Like karanna thenak nathi nisa Share karala Comment ekak demma, Hebai den batch ekama plagiarism ahu weida danne na... Thank you so much. #UCSCපවුල

    ReplyDelete
  5. Peya dekak late submission, hebai meka nattan ehemawat karanna hambawenne naha <3

    ReplyDelete
  6. ponnaMguizu William Singleton click here
    conhahoupo

    ReplyDelete

Post a Comment

Popular posts from this blog

How to import the Public Certificate of one WSO2 product to the trust store of another?

To demonstrate this point, I will use the 2 products WSO2 API Manager 2.1.0 (referred as APIM from here onwards) and WSO2 Enterprise Integrator 6.1.1 (referred as EI from here onwards). When using EI as the Business Process Server during configuration of Workflows in APIM, one step to perform is to import the public certificate of EI to the truststore of APIM [1]. So now let's see how this can be done. Step 1: Go to <EI_HOME>/repository/resources/security/ folder and execute the following keytool command. This command is used to export the public certificate of EI as a certificate file called wso2carbon.cer. Since the default keystore in EI is wso2carbon.jks, we have specified it as the keystore and the default alias is wso2carbon. Provide wso2carbon as the keystore password when prompted as it is the default password. After executing the above command from within the security folder in EI, you will see that a file with the name of wso2carbon.cer is created...

How to connect my database instance with elastic beanstalk instance in AWS?

If you have deployed your web application in Elastic Beanstalk in AWS and now you need to connect a database to this instance, and your database is actually residing in a different instance, how can you actually connect them? It's easy with Elastic Beanstalk. I will explain an example scenario that I used for connecting my elastic beanstalk web application with another instance containing my MongoDB database. By looking at this, you can customize as per your need. Don't worry. This is easy. :) The only things you need here are the details about the 1. Database name that you need to connect to. Ex:- "myDB" 2. Port at which the database instance is listening. EX:- In the case of MongoDB, the listening port is 27017 3. Host name of your database instance. EX:- Like localhost, in this case, it will be the Public DNS of your database instance 4. The password of your database if exists. First these details need to be set as environment variables in Elastic Be...