Skip to content

Commit

Permalink
Feature classes (#2)
Browse files Browse the repository at this point in the history
* add DigestTools class and methods; print key and secret digests

* add Generation and LongOps classes to clean up DHKEBI_J

* make DigestTools class and getDigest method package-private

* add DigestPrinter to public key and shared secret cases for printing

* make DigestTools package-private

* fix printing and digest logic errors
  • Loading branch information
aburgd authored Jul 24, 2017
1 parent 1690795 commit 062c3bc
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 133 deletions.
150 changes: 17 additions & 133 deletions src/DHKEBI_J.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import java.security.SecureRandom;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import static java.lang.Math.*;
import org.apache.commons.codec.binary.Hex;

public class DHKEBI_J {

private static Scanner stdin = new Scanner(System.in);
private static SecureRandom csprng = new SecureRandom();

public static void main(String[] args) {
System.out.println("Enter an option and press Return/Enter:");
Expand All @@ -17,143 +14,30 @@ public static void main(String[] args) {

switch (answer) {
case 1: {
BigInteger userPublicKey = publicKeyGeneration();
System.out.printf("Your public key is:\n%d",
BigInteger userPublicKey = Generation.publicKeyGeneration();
byte[] userKeyDigest = DigestTools.getDigest
(userPublicKey, "SHA");
String userKeyHex = Hex.encodeHexString(userKeyDigest);
System.out.printf("Your public key is:\n%d\n",
userPublicKey);
System.out.print("Your public key's digest is:\n");
DigestTools.digestPrinter(userKeyHex);
break;
}
case 2: {
BigInteger userSharedSecret = sharedSecretGeneration();
System.out.printf("Your shared secret is:\n%d",
BigInteger userSharedSecret = Generation.sharedSecretGeneration();
byte[] userSecretDigest = DigestTools.getDigest
(userSharedSecret, "SHA");
String userSecretHex = Hex.encodeHexString
(userSecretDigest);
System.out.printf("Your shared secret is:\n%d\n",
userSharedSecret);
System.out.print("Your shared secret's digest is:\n");
DigestTools.digestPrinter(userSecretHex);
break;
}
default: System.out.print("INVALID_ANS: Please enter 1 or " +
"2"); break;
}
}

// generation methods
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 = BigInteger.valueOf(-1);
return publicKey;
} else {
publicKey = baseLong.modPow(secretLong, moduloLong);
return publicKey;
}
}
private static BigInteger sharedSecretGeneration() {
BigInteger sharedSecret, secretLong, moduloLong, publicKey;
publicKey = longToBigInt(getPublicKey());
secretLong = longToBigInt(getSecretLong());
moduloLong = longToBigInt(getModuloLong());
boolean failOrSuccess = checkFailure(moduloLong, secretLong,
publicKey);
System.out.printf("failOrSuccess: %b\n", failOrSuccess);
if (failOrSuccess) {
System.out.println("BAD_LONG: Error getting one or more long" +
" values");
System.exit(-1);
sharedSecret = BigInteger.valueOf(-1);
return sharedSecret;
} else {
sharedSecret = publicKey.modPow(secretLong, moduloLong);
return sharedSecret;
}
}

// get methods
private static byte[] newRandomBytes() {
byte[] byteArray = new byte[8];
csprng.nextBytes(byteArray);
return byteArray;
}
private static long bytesToLong(byte[] byteArray) {
long longForm = 0;
for (int i = 0; i < byteArray.length; i++) {
longForm += ((long) byteArray[i] & 0xffL) << (8 * i);
}
return abs(longForm);
}
private static long getLong(char ans, String longType) {
long get;
if (ans == 'Y' || ans == 'y') {
System.out.printf("Please enter your %s long:\n", longType);
get = stdin.nextLong();
return get;
} else if (ans == 'n' || ans == 'N'){
get = bytesToLong(newRandomBytes());
System.out.printf("Your %s long is %d\n", longType, get);
return get;
} else {
return -1;
}
}
private static long getPublicKey() {
long publicKey = 0;
System.out.println("Do you have a public key? (Y/n)");
char ans = stdin.next().charAt(0);
if (ans == 'Y' || ans == 'y') {
System.out.println("Please enter your public key:");
publicKey = stdin.nextLong();
} else if (ans == 'n' || ans == 'N'){
System.out.print("BAD_ANS: Please re-run the program after " +
"generating a public key");
publicKey = -1;
System.exit((int)publicKey);
}
return publicKey;
}
private static long getBaseLong() {
long baseLong;
System.out.println("Do you have a shared base? (Y/n)");
char ans = stdin.next().charAt(0); // make String answer a char
baseLong = getLong(ans, "base");
return baseLong;
}
private static long getSecretLong() {
long secretLong;
System.out.println("Do you have a secret long? (Y/n)");
char ans = stdin.next().charAt(0); // make String answer a char
secretLong = getLong(ans, "secret");
return secretLong;
}
private static long getModuloLong() {
long moduloLong = 0;
System.out.println("Do you have a modular long? (Y/n)");
char ans = stdin.next().charAt(0); // make String answer a char
moduloLong = getLong(ans, "modulo");
return moduloLong;
}

// call after getting longs
private static BigInteger longToBigInt(long longValue) {
BigInteger newBigInt;
newBigInt = BigInteger.valueOf(longValue);
return newBigInt;
}
private static boolean checkFailure(BigInteger... bigIntValues) {
// drop the values into an array
BigInteger[] valueArray = new BigInteger[bigIntValues.length];
for (int item = 0; item < bigIntValues.length; item++) {
valueArray[item] = bigIntValues[item];
}
// put the array into a list
List<BigInteger> valueList = Arrays.asList(valueArray);

// failure
// not failure
return valueList.contains(BigInteger.valueOf(1))
|| valueList.contains(BigInteger.valueOf(-1));
}
}
30 changes: 30 additions & 0 deletions src/DigestTools.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

class DigestTools {
static byte[] getDigest(BigInteger bigIntValue, String...
instanceAlgo) {
if (instanceAlgo[0].isEmpty()) instanceAlgo[0] = "SHA-256";
String biString = bigIntValue.toString();
MessageDigest digest = null;
try {
digest = MessageDigest.getInstance(instanceAlgo[0]);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return digest.digest(biString.getBytes(StandardCharsets
.UTF_8));
}

static void digestPrinter(String digestHex) {
// left padding for odd-length strings
if (digestHex.length() % 2 != 0) digestHex = " " + digestHex;
for (int i = 0; i < digestHex.length(); i += 2) {
char currentChar = digestHex.charAt(i);
char nextChar = digestHex.charAt(i+1);
System.out.printf("%c%c ", currentChar, nextChar);
}
}
}
47 changes: 47 additions & 0 deletions src/Generation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Scanner;

class Generation {
private static Scanner stdin = new Scanner(System.in);
private static SecureRandom csprng = new SecureRandom();

// generation methods
static BigInteger publicKeyGeneration() {
BigInteger publicKey, moduloLong, baseLong, secretLong;
moduloLong = LongOps.longToBigInt(LongOps.getModuloLong());
baseLong = LongOps.longToBigInt(LongOps.getBaseLong());
secretLong = LongOps.longToBigInt(LongOps.getSecretLong());
boolean failOrSuccess = LongOps.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 = BigInteger.valueOf(-1);
return publicKey;
} else {
publicKey = baseLong.modPow(secretLong, moduloLong);
return publicKey;
}
}
static BigInteger sharedSecretGeneration() {
BigInteger sharedSecret, secretLong, moduloLong, publicKey;
publicKey = LongOps.longToBigInt(LongOps.getPublicKey());
secretLong = LongOps.longToBigInt(LongOps.getSecretLong());
moduloLong = LongOps.longToBigInt(LongOps.getModuloLong());
boolean failOrSuccess = LongOps.checkFailure(moduloLong, secretLong,
publicKey);
System.out.printf("failOrSuccess: %b\n", failOrSuccess);
if (failOrSuccess) {
System.out.println("BAD_LONG: Error getting one or more long" +
" values");
System.exit(-1);
sharedSecret = BigInteger.valueOf(-1);
return sharedSecret;
} else {
sharedSecret = publicKey.modPow(secretLong, moduloLong);
return sharedSecret;
}
}
}
97 changes: 97 additions & 0 deletions src/LongOps.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.security.SecureRandom;

import static java.lang.Math.abs;

class LongOps {
private static Scanner stdin = new Scanner(System.in);
private static SecureRandom csprng = new SecureRandom();

// get methods
private static byte[] newRandomBytes() {
byte[] byteArray = new byte[8];
csprng.nextBytes(byteArray);
return byteArray;
}
private static long bytesToLong(byte[] byteArray) {
long longForm = 0;
for (int i = 0; i < byteArray.length; i++) {
longForm += ((long) byteArray[i] & 0xffL) << (8 * i);
}
return abs(longForm);
}
private static long getLong(char ans, String longType) {
long get;
if (ans == 'Y' || ans == 'y') {
System.out.printf("Please enter your %s long:\n", longType);
get = stdin.nextLong();
return get;
} else if (ans == 'n' || ans == 'N'){
get = bytesToLong(newRandomBytes());
System.out.printf("Your %s long is %d\n", longType, get);
return get;
} else {
return -1;
}
}
static long getPublicKey() {
long publicKey = 0;
System.out.println("Do you have a public key? (Y/n)");
char ans = stdin.next().charAt(0);
if (ans == 'Y' || ans == 'y') {
System.out.println("Please enter your public key:");
publicKey = stdin.nextLong();
} else if (ans == 'n' || ans == 'N'){
System.out.print("BAD_ANS: Please re-run the program after " +
"generating a public key");
publicKey = -1;
System.exit((int)publicKey);
}
return publicKey;
}
static long getBaseLong() {
long baseLong;
System.out.println("Do you have a shared base? (Y/n)");
char ans = stdin.next().charAt(0); // make String answer a char
baseLong = getLong(ans, "base");
return baseLong;
}
static long getSecretLong() {
long secretLong;
System.out.println("Do you have a secret long? (Y/n)");
char ans = stdin.next().charAt(0); // make String answer a char
secretLong = getLong(ans, "secret");
return secretLong;
}
static long getModuloLong() {
long moduloLong = 0;
System.out.println("Do you have a modular long? (Y/n)");
char ans = stdin.next().charAt(0); // make String answer a char
moduloLong = getLong(ans, "modulo");
return moduloLong;
}

// call after getting longs
public static BigInteger longToBigInt(long longValue) {
BigInteger newBigInt;
newBigInt = BigInteger.valueOf(longValue);
return newBigInt;
}
public static boolean checkFailure(BigInteger... bigIntValues) {
// drop the values into an array
BigInteger[] valueArray = new BigInteger[bigIntValues.length];
for (int item = 0; item < bigIntValues.length; item++) {
valueArray[item] = bigIntValues[item];
}
// put the array into a list
List<BigInteger> valueList = Arrays.asList(valueArray);

// failure
// not failure
return valueList.contains(BigInteger.valueOf(1))
|| valueList.contains(BigInteger.valueOf(-1));
}
}

0 comments on commit 062c3bc

Please sign in to comment.