Printing debug values in a cryptographic exchange
I have created this basic program to implement the Diffie-Hellman algorithm. I want s
to be a randomly generated prime number, and q
to be a randomly generated integer. sk1
and sk2
are shared keys which are also generated randomly. pk1
and pk2
are modulus calculated values which I have to prove are equal to each other. I think my formula is correct but I am not sure how to print the values of s
, q
, sk1
, sk2
, pk1
and pk2
to verify my requirements. My code is pasted below and I would really appreciate if anyone could help me with the print statements.
package javaapplication1;
import java.util.Random;
import java.math.*;
public class DH {
public static void main(String [] arg) {
int s, q;
double sk1, sk2, pk1 = 0, pk2 = 0;
Random generator = new Random();
s = generator.nextInt(50000);
q = generator.nextInt(50000);
sk1 = generator.nextInt();
sk2 = generator.nextInt()开发者_开发问答;
if(s==1 || s==2) {
for(int i = 2; i< (int)(s/2); i++) {
if(s/i != (int)(s/i)) {
double a= Math.pow(q,sk1);
pk1 = a%s;
double b= Math.pow(q, sk2);
pk2 = b%s;
if(pk1==pk2) {
System.out.println("true");
}
System.out.println(s);
System.out.println(q);
System.out.println(sk1);
System.out.println(sk2);
System.out.println(pk1);
System.out.println(pk2);
}
}
}
}
}
You're only ever doing anything if if(s/i != (int)(s/i))
. Since both s
and i
are integers, it's unlikely that this condition will ever be true.
(To clarify: With "unlikely", I mean that this condition couldn't possibly be true. s/i
returns an integer, and (int)(s/i)
will return the exact same integer.)
Your print statements are inside of your if case. Have you considered that s != 1 or s != 2
?
package javaapplication1;
import java.util.Random;
import java.math.*;
public class DH {
public static void main(String [] arg) {
int s, q;
double sk1, sk2, pk1 = 0, pk2 = 0;
Random generator = new Random();
s = generator.nextInt(50000);
q = generator.nextInt(50000);
sk1 = generator.nextInt();
sk2 = generator.nextInt();
if(s==1 || s==2) {
for(int i = 2; i< (int)(s/2); i++) {
if(s/i != (int)(s/i)) {
double a= Math.pow(q,sk1);
pk1 = a%s;
double b= Math.pow(q, sk2);
pk2 = b%s;
if(pk1==pk2) {
System.out.println("true");
}
}
}
}
System.out.println(s);
System.out.println(q);
System.out.println(sk1);
System.out.println(sk2);
System.out.println(pk1);
System.out.println(pk2);
}
}
As s an intger generated by:
s = generator.nextInt(50000);
There is a 25,000 to 1 chance that :
if(s==1 || s==2) {
will ever be true. Perhaps yo umeant if s > 2?
Don't ever use floating point arithmetics for cryptography. Especially for exponentiation you need results with more precision than a double
can give you. Use BigInteger
instead.
While you are testing, you should not use "new" random numbers each time. Use new Random(0)
instead so that you can reproduce your computations the next time you start the debugger.
How do you guarantee that s
is a prime number? I cannot see it from the code.
Please indent your code properly and consistently. Since you are using Eclipse, all you need to do is to press Ctrl-Shift-F once.
精彩评论