This is school work.
I'm given the problem of finding the private keys of both parties in a DH exchange. The numbers involved in the tests aren't big enough and the task is basically brute-force.
In the task, I can get the prime p
, generator g
and Alice's public key A
.
I'm also given the methods to encrypt a message and decrypt a message with a custom key.
Right now I've only gotten a
by simply looping through integers i=1...p
and checking if g^i mod p == g^A mod p
and promptly returning the first value that meets the requirement.
However, my solution isn't always true according to automated tests.
Anyone know how or even if it's possible to fins a
and b
with the given info?
Thanks to a third party, I managed to crack the DH code:
public Integer crackAlice() {
// TODO
Integer alicePrivate = 0;
int p = session.getP();
int g = session.getG();
int A = session.getAlicesPublicKey();
// A = g^a mod p
System.out.println("Alice public A: "+A);
String message = String.valueOf(156215);
for (int i = 1; i < p; i++) {
if (BigInteger.valueOf(g).pow(i).mod(BigInteger.valueOf(p)).equals(BigInteger.valueOf(A))) {
//System.out.println("\t\t\t\t"+BigInteger.valueOf(g).pow(i));
alicePrivate = i;
System.out.println("Potential Alice private a: "+i);
//break;
}
}
return alicePrivate;
}
and
public Integer crackBob() {
// TODO
Integer bobPrivate = 0;
Integer a = crackAlice();
int p = session.getP();
int g = session.getG();
int A = session.getAlicesPublicKey();
String mainMessage = "teade";
String msg = null;
try {
msg = session.getEncrypted(mainMessage);
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 1; i < p; i++) {
int ai = a*i;
int Ai = A*i;
//System.out.println("a*b = "+ai);
BigInteger bigintP = BigInteger.valueOf(p);
if (((BigInteger.valueOf(g).pow(a).mod(bigintP)).pow(i)).mod(bigintP)
.equals(((BigInteger.valueOf(g).pow(i).mod(bigintP)).pow(a)).mod(bigintP))) {
String decrypt = null;
try {
decrypt = session.getDecryptedWithCustomKey(msg, BigInteger.valueOf(g).pow(a*i).mod(bigintP).intValue());
} catch (Exception e) {
e.printStackTrace();
}
if (decrypt != null && decrypt.trim().equals(mainMessage)) {
bobPrivate = i;
break;
}
}
}
return bobPrivate;
}
I hope this will help out other with a similar problem.