Skip to content

Instantly share code, notes, and snippets.

@Zelakolase
Created August 5, 2021 17:59
Show Gist options
  • Save Zelakolase/b118796bc679a50460b7e18a4b6ae6fc to your computer and use it in GitHub Desktop.
Save Zelakolase/b118796bc679a50460b7e18a4b6ae6fc to your computer and use it in GitHub Desktop.
package lib;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.math.BigInteger;
import java.util.Random;
public class DH {
public static String Server(DataInputStream DIS , DataOutputStream DOS) {
String secret = "";
try {
double sec = Math.abs(new Random().nextInt(10 + 1) + 5); // Secret Num , NOT FOR SHARE
// START : p and g Agreement
BigInteger BIp = BigInteger.probablePrime(new Random().nextInt(8 + 1) + 6, new Random()); // The prime num
int p = (int) Math.abs(BIp.longValue()); // Prime as int
double g = new PRM().getPrimitiveRoots(p).get(0); // g , which is primitive root modulo of p
//log.i("P = "+p+" , G = "+g+" , "+"Secret = "+sec);
DOS.writeUTF(p+","+g);
// END
// Send g^sec mod p , also known as pub_1
//log.i("A Public Key = "+Math.pow(g, sec)%Double.valueOf(p));
DOS.writeUTF(""+Math.pow(g, sec)%Double.valueOf(p)); // How I convert double datatype to a String , Shhhh.. don't tell anyone
double pub_2 = Double.parseDouble(DIS.readUTF()); // The public key of the other side
//log.i("B Public Key = "+pub_2);
// Calculate key
secret = ""+Math.pow(pub_2, sec) % Double.valueOf(p); // Secret
}catch(Exception e) {
}
return secret;
}
public static String Client(DataInputStream DIS , DataOutputStream DOS) {
String secret = "";
try {
double sec = Math.abs(new Random().nextInt(10 + 1) + 5); // Secret Num , NOT FOR SHARE
// START : p and g Agreement
String pgs = DIS.readUTF();
String[] pg = pgs.split(","); // get p and g from server
double p = Double.parseDouble(pg[0]);
double g = Double.parseDouble(pg[1]);
// END
double pub_1 = Double.parseDouble(DIS.readUTF()); // pub_1 , the public key of the other side
// Send g^sec mod p , also known as pub_2
DOS.writeUTF(""+Math.pow(g, sec)%p);
// Calculate key
secret = ""+Math.pow(pub_1, sec) % p;
}catch(Exception e) {
}
return secret;
}
}
/*
* Primitive Root Modulo Class
*/
package lib;
import java.math.BigInteger;
import java.util.ArrayList;
public class PRM {
ArrayList<Integer> lcf = new ArrayList<Integer>();
ArrayList<Integer> primRoots = new ArrayList<Integer>();
public ArrayList<Integer> getPrimitiveRoots(int n){
if(isPrime(n)){
n = n-1;
}else{
return null;
}
ArrayList<Integer> lowestCommonFactors = lowestCommonFactors(n);
ArrayList<Integer> dividers = new ArrayList<Integer>();
for (int i = 0; i < lowestCommonFactors.size(); i++) {
dividers.add(n/lowestCommonFactors.get(i));
}
loop: for (int i = 2; i <= n; i++) {
for (int j = 0; j < dividers.size(); j++) {
BigInteger modded = performModPow(i, dividers.get(j),n+1);
if(modded.compareTo(new BigInteger("1")) == 0){
continue loop;
}
if(j == dividers.size()-1){
primRoots.add(i);
}
}
}
return primRoots;
}
public BigInteger performModPow(int powee, int power, int mod){
BigInteger bigPowee = new BigInteger(powee +"");
BigInteger bigPower = new BigInteger(power+"");
BigInteger bigMod = new BigInteger(mod+"");
return bigPowee.modPow(bigPower,bigMod);
}
public ArrayList<Integer> lowestCommonFactors(int number){
int count;
for (int i = 2; i<=(number); i++) {
count = 0;
while (number % i == 0) {
number /= i;
count++;
}
if (count == 0) {
continue;
}
lcf.add(i);
}
return lcf;
}
public boolean isPrime(int x){
if(x<2){return false;}
for (int i = 2; i <= x/2; i++) {
if(x%i == 0){ return false; }
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment