-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move java files; add package declation to java files
- Loading branch information
Showing
4 changed files
with
225 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,45 @@ | ||
package java; | ||
|
||
import java.math.BigInteger; | ||
import java.util.Scanner; | ||
import org.apache.commons.codec.binary.Hex; | ||
|
||
public class DHKEBI { | ||
|
||
private static Scanner stdin = new Scanner(System.in); | ||
|
||
public static void main(String[] args) { | ||
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 = 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 = 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; | ||
} | ||
} | ||
} |
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,32 @@ | ||
package java; | ||
|
||
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); | ||
} | ||
} | ||
} |
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 @@ | ||
package java; | ||
|
||
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; | ||
} | ||
} | ||
} |
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,99 @@ | ||
package java; | ||
|
||
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)); | ||
} | ||
} |