Skip to content

Instantly share code, notes, and snippets.

@jaideepjagyasi
Last active August 11, 2017 04:54
Show Gist options
  • Save jaideepjagyasi/98d818018a0e906344eb3767a54db29f to your computer and use it in GitHub Desktop.
Save jaideepjagyasi/98d818018a0e906344eb3767a54db29f to your computer and use it in GitHub Desktop.
//Client
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
/*
@author Jagyasi
*/
public class DH_CSCL4 {
public static int BintoDec(int n){
int result=0;
int pow_val=0;
while(n>0){
int rem=n%10;
result+=((int)Math.pow(2, pow_val))*rem ;
pow_val++;
n=n/10;
}
return result;
}
public static boolean isPrime(int n) {
boolean flag = false;
for (int i = 2; i < n; i++) {
if (n % i == 0) {
flag = false;
break;
} else {
flag = true;
}
}
return flag;
}
public static void main(String[] args) throws IOException {
Socket s=new Socket("localhost",6646);
Scanner in=new Scanner(System.in);
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
int gen,prime;
prime = ThreadLocalRandom.current().nextInt(2, 9 + 1);
System.out.println("Prime"+prime);
gen=ThreadLocalRandom.current().nextInt(1,prime);
System.out.println("Generator"+gen);
if(isPrime(prime)){
dout.writeInt(gen);
dout.writeInt(prime);
int a=ThreadLocalRandom.current().nextInt(2,8);
int ApubKey=(int) ((Math.pow(gen, a)) % prime);
System.out.println("efsdf "+ApubKey);
dout.writeInt(ApubKey);
int BpubKey=din.readInt();
System.out.println(BpubKey+" frsfdsf");
System.out.println("Enter numerical data...");
int Adat=in.nextInt();
int coded=Integer.parseInt(Integer.toBinaryString(Adat));
int binApub=Integer.parseInt(Integer.toBinaryString(ApubKey));
int packet=coded ^ binApub;
dout.writeInt(BintoDec(packet));
System.out.println("Sent...");
}else {
System.out.println("error....");
}
}
}
//Server
import java.util.concurrent.ThreadLocalRandom;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
* @author Jagyasi
*/
public class DH_CSCL4_2 {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(6646);
Socket s = ss.accept();
int gen = 0, prime = 0;
int ApubKey;
DataInputStream din = new DataInputStream(s.getInputStream());
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
gen = din.readInt();
prime = din.readInt();
ApubKey=din.readInt();
int b=ThreadLocalRandom.current().nextInt(2,8);
int BpubKey=(int) (Math.pow(gen, b)%prime);
dout.writeInt(BpubKey);
int packet=din.readInt();
int decoded=Integer.parseInt(Integer.toBinaryString(packet));
int binBpub=Integer.parseInt(Integer.toBinaryString(BpubKey));
int data=decoded^binBpub;
System.out.println("Recieved....."+DH_CSCL4.BintoDec(data));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment