-
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.
- Loading branch information
0 parents
commit cf3009b
Showing
3 changed files
with
364 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,67 @@ | ||
# Created by .ignore support plugin (hsz.mobi) | ||
### JetBrains template | ||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm | ||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 | ||
|
||
# User-specific stuff: | ||
.idea | ||
*.iml | ||
|
||
# Sensitive or high-churn files: | ||
.idea/**/dataSources/ | ||
.idea/**/dataSources.ids | ||
.idea/**/dataSources.xml | ||
.idea/**/dataSources.local.xml | ||
.idea/**/sqlDataSources.xml | ||
.idea/**/dynamic.xml | ||
.idea/**/uiDesigner.xml | ||
|
||
# Gradle: | ||
.idea/**/gradle.xml | ||
.idea/**/libraries | ||
|
||
# Mongo Explorer plugin: | ||
.idea/**/mongoSettings.xml | ||
|
||
## File-based project format: | ||
*.iws | ||
|
||
## Plugin-specific files: | ||
|
||
# IntelliJ | ||
/out/ | ||
|
||
# mpeltonen/sbt-idea plugin | ||
.idea_modules/ | ||
|
||
# JIRA plugin | ||
atlassian-ide-plugin.xml | ||
|
||
# Crashlytics plugin (for Android Studio and IntelliJ) | ||
com_crashlytics_export_strings.xml | ||
crashlytics.properties | ||
crashlytics-build.properties | ||
fabric.properties | ||
### Java template | ||
# Compiled class file | ||
*.class | ||
|
||
# Log file | ||
*.log | ||
|
||
# BlueJ files | ||
*.ctxt | ||
|
||
# Mobile Tools for Java (J2ME) | ||
.mtj.tmp/ | ||
|
||
# Package Files # | ||
*.jar | ||
*.war | ||
*.ear | ||
*.zip | ||
*.tar.gz | ||
*.rar | ||
|
||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
hs_err_pid* |
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,155 @@ | ||
// Only use Random for example purposes! | ||
// Use security.SecureRandom for production! | ||
// Actually, this entire thing is for example purposes | ||
// You've been warned | ||
|
||
import java.security.SecureRandom; | ||
import java.util.Scanner; | ||
import java.util.stream.LongStream; | ||
import java.math.BigInteger; | ||
import static java.lang.Math.*; | ||
|
||
public class DHKE { | ||
|
||
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:"); | ||
System.out.println("1) public key\n2) shared secret"); | ||
|
||
int answer = stdin.nextInt(); | ||
|
||
switch (answer) { | ||
case 1: { | ||
long userPublicKey = publicKeyGeneration(); | ||
System.out.printf("Your public key is:\n %d", | ||
userPublicKey); | ||
break; | ||
} | ||
case 2: { | ||
long userSharedSecret = sharedSecretGeneration(); | ||
System.out.printf("Your shared secret is:\n%d", | ||
userSharedSecret); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
private static long publicKeyGeneration() { | ||
long publicKey, moduloLong, baseLong, secretLong; | ||
moduloLong = getModuloLong(); | ||
baseLong = getBaseLong(); | ||
secretLong = 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; | ||
return publicKey; | ||
} else { | ||
publicKey = (long)(pow((float)baseLong, (float)secretLong)) % | ||
moduloLong; | ||
return publicKey; | ||
} | ||
} | ||
|
||
private static long sharedSecretGeneration() { | ||
long sharedSecret, secretLong, moduloLong, publicKey; | ||
publicKey = getPublicKey(); | ||
secretLong = getSecretLong(); | ||
moduloLong = 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 = -1; | ||
return sharedSecret; | ||
} else { | ||
publicKey = (long)(pow((float)secretLong, (float)secretLong) | ||
) % moduloLong; | ||
return publicKey; | ||
} | ||
} | ||
|
||
private static byte[] newRandomBytes() { | ||
byte[] byteArray = new byte[4]; // make some bytes | ||
csprng.nextBytes(byteArray); // get some new secure bytes | ||
return byteArray; | ||
} | ||
|
||
private static long bytesToLong(byte[] byteArray) { | ||
long newLong = 0; | ||
for (int i = 0; i < byteArray.length; i++) { | ||
newLong += ((long) byteArray[i] & 0xffL) << (8 * i); | ||
} | ||
newLong = abs(newLong); | ||
return newLong; // in case of negative | ||
} | ||
|
||
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 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; | ||
} | ||
|
||
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 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 boolean checkFailure(long... longValues) { | ||
return LongStream.of(longValues).anyMatch(item -> | ||
item < 0); | ||
} | ||
} |
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,142 @@ | ||
import java.security.SecureRandom; | ||
import java.math.BigInteger; | ||
import java.util.stream.LongStream; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
import static java.lang.Math.*; | ||
|
||
public class DHKEBI { | ||
private static Scanner stdin = new Scanner(System.in); | ||
private static SecureRandom csprng = new SecureRandom(); | ||
|
||
public static void main(String[] args) { | ||
// TODO fill out | ||
} | ||
|
||
// generation methods | ||
private static long publicKeyGeneration() { | ||
long publicKey, moduloLong, baseLong, secretLong; | ||
moduloLong = getModuloLong(); | ||
baseLong = getBaseLong(); | ||
secretLong = 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; | ||
return publicKey; | ||
} else { | ||
publicKey = (long)(pow((float)baseLong, (float)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 { | ||
publicKey = publicKey.modPow(secretLong, moduloLong); | ||
return publicKey; | ||
} | ||
} | ||
|
||
// get methods | ||
private static byte[] newRandomBytes() { | ||
byte[] byteArray = new byte[4]; | ||
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 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); | ||
|
||
if (valueList.contains(1) || valueList.contains(-1)) { | ||
return true; // failure | ||
} else { | ||
return false; // not failure | ||
} | ||
} | ||
} |