Skip to content

Commit

Permalink
Add Diffie Hellman Key Exchange
Browse files Browse the repository at this point in the history
  • Loading branch information
iamrohitsuthar committed May 27, 2021
1 parent be6bcb4 commit 740385f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
36 changes: 36 additions & 0 deletions ICS/Assignment3/AliceDH.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.io.*;
import java.math.BigInteger;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class AliceDH
{
public static void main(String[] args) throws IOException
{
ServerSocket server = new ServerSocket(8088);
System.out.println("Waiting for connection on port " + server.getLocalPort());
Socket soc = server.accept();
System.out.println("Accepted connection from " + soc.getRemoteSocketAddress());
DataInputStream dis = new DataInputStream(soc.getInputStream());
DataOutputStream dos = new DataOutputStream(soc.getOutputStream());

int P = dis.readInt();
int G = dis.readInt();
int y = dis.readInt();
System.out.println("P = " + P + " \nG = " + G + "\nBob's Public key(y) = " + y);

Scanner sc = new Scanner(System.in);
System.out.print("Enter Private key : ");
int a = sc.nextInt();

//x = (G ^ a) mod P
BigInteger x = (BigInteger.valueOf(G).pow(a)).mod(BigInteger.valueOf(P));
System.out.println("Alice's Public key(Ya) = " + x);
dos.writeInt(x.intValue());

//ka = (y ^ a) mod P
BigInteger ka=(BigInteger.valueOf(y).pow(a)).mod(BigInteger.valueOf(P));
System.out.println("Shared Key : "+ ka.intValue());
}
}
49 changes: 49 additions & 0 deletions ICS/Assignment3/BobDH.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class BobDH
{
private static Socket soc;
private static Scanner sc;

public static void main(String[] args) throws UnknownHostException, IOException
{
int P,G;
int b;

soc = new Socket("127.0.0.1",8088);
System.out.println("Connected to "+soc.getRemoteSocketAddress());
DataInputStream dis = new DataInputStream(soc.getInputStream());
DataOutputStream dos = new DataOutputStream(soc.getOutputStream());
sc = new Scanner(System.in);

System.out.print("Enter Modulus (P) Prime Number: ");
P = sc.nextInt();
System.out.print("Enter Base (G) Primitive Root of P: ");
G = sc.nextInt();

dos.writeInt(P);
dos.writeInt(G);

System.out.print("Enter Private key : ");
b = sc.nextInt();

//y = (G ^ b) mod P
BigInteger y = (BigInteger.valueOf(G).pow(b)).mod(BigInteger.valueOf(P));
System.out.println("Bob's Public key(y)= " + y.intValue());
dos.writeInt(y.intValue());

//kb = (x ^ b) mod P
int x = dis.readInt();
System.out.println("Alice's Public key(Ya) = " + x);
BigInteger kb = (BigInteger.valueOf(x).pow(b)).mod(BigInteger.valueOf(P));

System.out.println("Shared Key: " + kb.intValue());
soc.close();
}
}

0 comments on commit 740385f

Please sign in to comment.