-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be6bcb4
commit 740385f
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |