0

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);
                }
            } 
        }
    }
}
Bruno Rohée
  • 3,436
  • 27
  • 32
Hash
  • 1
  • 4
  • Please format your code by selecting it and pressing CTRL+K. – EboMike Nov 30 '10 at 00:59
  • Btw, what is your question? You're saying you're not sure how to print the values, but you're calling `System.out.println`. So what's happening? Are they not printed out? – EboMike Nov 30 '10 at 01:00

4 Answers4

1

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);        
    }
}
Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
  • @Ebo Mike it is not printing value – Hash Nov 30 '10 at 01:03
  • @amir none of those condition work :( ... and S is supposed to be a prime no. – Hash Nov 30 '10 at 01:07
  • @Ebo Mike: it is building successfully but not printing value so I cant verify what I wish to do – Hash Nov 30 '10 at 01:09
  • See my answer. The s/i condition doesn't make any sense. – EboMike Nov 30 '10 at 01:10
  • @user524547: `s` may be "supposed to be" a prime number, but your code won't make it so. The line `s = generator.nextInt(50000);` will cause `s` to be some random integer between 0 and 50000. Since your code that actually prints the outputs is wrapped in an `if` block that tests `if(s == 1 || s == 2)` you will only ever run it 1/25000 of the time. And as EboMike points out, your `if(s/i != (int)(s/i))` will prevent it from running anything else on those other times either. – Daniel Pryden Nov 30 '10 at 01:16
  • can you point me to a general syntax of prime no. if i am formulating it wrong . – Hash Nov 30 '10 at 01:22
  • I will try my program again with right formulation of prime no. – Hash Nov 30 '10 at 01:34
1

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.)

EboMike
  • 76,846
  • 14
  • 164
  • 167
  • Its the core of the Diffie Hellman algorithim that you need to find the larget factors of s that is a whole number. As he is looping through all values of i to half of s the program will surly find at least one factor when i = 1 and probably a higher value. – James Anderson Nov 30 '10 at 01:13
  • True, except that `i` and `s` are both integers, so `s/i` returns an integer, and `(int)(s/i)` returns the same integer. – EboMike Nov 30 '10 at 01:16
1

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.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121
1

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?

James Anderson
  • 27,109
  • 7
  • 50
  • 78
  • I am trying to make S as a prime no. and 1 and 2 are prime no.s – Hash Nov 30 '10 at 01:18
  • Please read the post carefully and think about it! You are selectign a random number s between 1 and 50,000. You then execute the body of your code only if s has value 1 or 2! – James Anderson Nov 30 '10 at 02:04