0% found this document useful (0 votes)
49 views6 pages

Experiment 7: IT117-Jaimish Trivedi

The document describes a program that implements the Diffie-Hellman key exchange algorithm for encryption and decryption. It contains code for a DHAlgo class that performs the key exchange, and Server and Client classes that establish a connection, exchange public keys to generate a shared secret key, and send encrypted messages between each other. The server accepts a client connection, they generate public/private keys and a session key, and then exchange encrypted messages before closing the connection.

Uploaded by

Jaimish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views6 pages

Experiment 7: IT117-Jaimish Trivedi

The document describes a program that implements the Diffie-Hellman key exchange algorithm for encryption and decryption. It contains code for a DHAlgo class that performs the key exchange, and Server and Client classes that establish a connection, exchange public keys to generate a shared secret key, and send encrypted messages between each other. The server accepts a client connection, they generate public/private keys and a session key, and then exchange encrypted messages before closing the connection.

Uploaded by

Jaimish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

IT117-Jaimish Trivedi

EXPERIMENT 7

AIM:​ Write a program to implement the Diffie-Hellman Key exchange algorithm and perform
encryption and decryption.

Deffie-Hellman Algorithm Class:

import​ java.util.*;
import​ java.math.BigInteger;

class​ ​DHAlgo​{
​//Initializing global q and alpha variables
​private​ ​static​ BigInteger q = ​new​ BigInteger(​"761"​);
​private​ ​static​ BigInteger alpha = ​new​ BigInteger(​"6"​);
​//Keys
​private​ BigInteger publicKey;
​private​ BigInteger privateKey;
​public​ BigInteger sessionKey;

​//Constructor
​public​ ​DHAlgo​(​int​ key){
​this​.privateKey = BigInteger.valueOf(key);
​this​.publicKey = calcPublicKey();
}

​//Helper function to calculate public key based on the given private key
​private​ BigInteger ​calcPublicKey​(){
​return​ DHAlgo.alpha.modPow(privateKey, DHAlgo.q);
}

​//Getter method
​public​ BigInteger ​getPublicKey​(){
​return​ ​this​.publicKey;
}

​//Getter method
​public​ BigInteger ​getPrivateKey​(){
​return​ ​this​.privateKey;
}

​//Helper function to generate session key


​public​ ​void​ ​generateSessionKey​(BigInteger Y, BigInteger X){
​this​.sessionKey = Y.modPow(X, DHAlgo.q);
}

​//Encryption
​public​ ​int​ ​encrypt​(​int​ text){
​return​ BigInteger.valueOf(text).add(​this​.sessionKey).mod(DHAlgo.q).intValue();
}
IT117-Jaimish Trivedi

​//Decryption
​public​ ​int​ ​decrypt​(​int​ text){
​return​ BigInteger.valueOf(text).subtract(​this​.sessionKey).mod(DHAlgo.q).intValue();
}
}

Server Class:

import​ java.net.*;
import​ java.io.*;
import​ java.math.BigInteger;
import​ java.lang.*;
import​ java.util.*;

public​ ​class​ ​ServerDH​{


​private​ Socket socket = ​null​;
​private​ ServerSocket server = ​null​;
​private​ DataInputStream in = ​null​;
​private​ DataOutputStream out = ​null​;
​private​ FileWriter fw = ​null​;

​//Constructor
​public​ ​ServerDH​(​int​ port){
​try​{
​//Starting the server in listening mode
server = ​new​ ServerSocket(port);
System.out.println(​"\tServer started"​);
System.out.println(​"\tWaiting for a client ..."​);

​//Establishing connection with client


socket = server.accept();
System.out.println(​"\tClient accepted"​);

in = ​new​ DataInputStream(​new​ BufferedInputStream(socket.getInputStream()));


out = ​new​ DataOutputStream(socket.getOutputStream());

​//Getting private key from server and generating its public key
Scanner sc = ​new​ Scanner(System.in);
System.out.print(​"Enter server's private key : "​);
DHAlgo dh = ​new​ DHAlgo(sc.nextInt());
​int​ publicKey = dh.getPublicKey().intValue();
System.out.println(​"\tServer's public key : "​ + publicKey);
IT117-Jaimish Trivedi

​try​{
​//Getting public key from client
String clientPublicKey = in.readUTF();
out.writeUTF(​""​+publicKey);
System.out.println(​"\tClient's Public Key : "​ + clientPublicKey);

​//Generating session key using client's public key and server's private key
dh.generateSessionKey(​new​ BigInteger(clientPublicKey), dh.getPrivateKey());
System.out.println(​"\tSession key : "​ + dh.sessionKey);

System.out.println(​"\tWaiting for encrypted text from client"​);

​//Getting cipher text from client and decrypting it


String cipher = in.readUTF();
System.out.println(​"Cipher text from Client : "​+cipher);
System.out.println(​"Plain text from Client :
"​+dh.decrypt(Integer.parseInt(cipher)));

​//Getting plain text from Server


System.out.print(​"Enter a plain text : "​);
​int​ plain_text = sc.nextInt();

System.out.println(​"\tEncrypted text sent to the client"​);

​//Writing cipher text of server back to the client


out.writeUTF(​""​+dh.encrypt(plain_text));

}​catch​(IOException i){
System.out.println(i);
}
System.out.println(​"\tClosing connection"​);

socket.close();
in.close();
}​catch​(IOException i){
System.out.println(i);
}
}

​//Driver main
​public​ ​static​ ​void​ ​main​(String args[]){
​new​ ServerDH(​5000​);
}
}
IT117-Jaimish Trivedi

Client Class:

import​ java.util.*;
import​ java.net.*;
import​ java.io.*;
import​ java.math.BigInteger;
import​ java.lang.*;

public​ ​class​ ​ClientDH​{


​// initialize socket and input output streams
​private​ Socket socket = ​null​;
​private​ DataInputStream input = ​null​;
​private​ DataOutputStream out = ​null​;

​public​ ​ClientDH​(String address, ​int​ port){


​try​{
​//Establishing connection with server
socket = ​new​ Socket(address, port);
System.out.println(​"\tConnected"​);
input = ​new​ DataInputStream(socket.getInputStream());
out = ​new​ DataOutputStream(socket.getOutputStream());
}
​catch​(Exception e){
e.printStackTrace();
}

​//Getting private key from client and generating its public key
Scanner sc = ​new​ Scanner(System.in);
System.out.print(​"Enter client's private key : "​);
DHAlgo dh = ​new​ DHAlgo(sc.nextInt());
​int​ publicKey = dh.getPublicKey().intValue();
System.out.println(​"\tClient's public key : "​ + publicKey);

​try​{
​//Sending client's public key to the server
out.writeUTF(​""​+publicKey);

​//Getting public key from server


String serverPublicKey = (String)input.readUTF();
System.out.println(​"\tServer's public key : "​ + serverPublicKey);

​//Generating session key using server's public key and client's private key
dh.generateSessionKey(​new​ BigInteger(serverPublicKey), dh.getPrivateKey());
System.out.println(​"\tSession key : "​ + dh.sessionKey);

​//Getting plain text from Client


System.out.print(​"Enter a plain text : "​);
​int​ plain_text = sc.nextInt();

​ /Sending encrypted text to the server


/
out.writeUTF(​""​+dh.encrypt(plain_text));
System.out.println(​"\tEncrypted text sent to the server"​);
IT117-Jaimish Trivedi

System.out.println(​"\tWaiting for encrypted text from server"​);

​//Getting encrypted text from server and decrypting it


String cipher = input.readUTF();
System.out.println(​"Cipher text from Server : "​+cipher);
System.out.println(​"Plain text from Server :
"​+dh.decrypt(Integer.parseInt(cipher)));

input.close();
out.close();
socket.close();
}​catch​(IOException i){
System.out.println(i);
}
}

​//Driver main
​public​ ​static​ ​void​ ​main​(String args[]){
​new​ ClientDH(​"127.0.0.1"​, ​5000​);
}
}

Server Output:
IT117-Jaimish Trivedi

Client Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy