Skip to content

Commit

Permalink
Finish writing the BigInteger implementation in Java
Browse files Browse the repository at this point in the history
  • Loading branch information
aburgd committed Jul 21, 2017
1 parent cf3009b commit bc5de1f
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions src/DHKEBI.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,45 @@ public class DHKEBI {
private static SecureRandom csprng = new SecureRandom();

public static void main(String[] args) {
// TODO fill out
System.out.println("Enter an option and press Return/Enter:");
System.out.println("1) public key\n2) shared secret");

int answer = stdin.nextInt();

switch (answer) {
case 1: {
BigInteger userPublicKey = publicKeyGeneration();
System.out.printf("Your public key is:\n%d",
userPublicKey);
break;
}
case 2: {
BigInteger userSharedSecret = sharedSecretGeneration();
System.out.printf("Your shared secret is:\n%d",
userSharedSecret);
break;
}
default: System.out.print("INVALID_ANS: Please enter 1 or " +
"2"); break;
}
}

// generation methods
private static long publicKeyGeneration() {
long publicKey, moduloLong, baseLong, secretLong;
moduloLong = getModuloLong();
baseLong = getBaseLong();
secretLong = getSecretLong();
private static BigInteger publicKeyGeneration() {
BigInteger publicKey, moduloLong, baseLong, secretLong;
moduloLong = longToBigInt(getModuloLong());
baseLong = longToBigInt(getBaseLong());
secretLong = longToBigInt(getSecretLong());
boolean failOrSuccess = checkFailure(moduloLong, baseLong,
secretLong);
System.out.printf("failOrSuccess: %b\n", failOrSuccess);
if (failOrSuccess) {
System.out.println("BAD_LONG: Error getting long");
System.exit(-1);
publicKey = -1;
publicKey = BigInteger.valueOf(-1);
return publicKey;
} else {
publicKey = (long)(pow((float)baseLong, (float)secretLong)) %
moduloLong;
publicKey = baseLong.modPow(secretLong, moduloLong);
return publicKey;
}
}
Expand All @@ -49,8 +68,8 @@ private static BigInteger sharedSecretGeneration() {
sharedSecret = BigInteger.valueOf(-1);
return sharedSecret;
} else {
publicKey = publicKey.modPow(secretLong, moduloLong);
return publicKey;
sharedSecret = publicKey.modPow(secretLong, moduloLong);
return sharedSecret;
}
}

Expand Down

0 comments on commit bc5de1f

Please sign in to comment.