Diffie Hellman Private Key

I have a line of code to create a private key:

int Xa = randomNo.nextInt(10000); int Ya = (int) Math.pow(G, Xa) % P; 

G and P are static numbers. While Xa randomly generated. Every time I run the program, it gives me the same result for Ya . Is this right for Diffie Hellman? I thought the secret key should change every time the algorithm runs.

+4
source share
4 answers

I think the problem may be that you overflow twice with your exponentiation, which leads to infinity, which leads to the same value every time (if you are not lucky to end up with a very small number returned for your indicator).

Also, be sure to use a safe random case to get your random value:

 Random random = new SecureRandom(); // If you use more than 100 here, then // with your value of 486 for G you will // end up with infinity when doing Math.pow(G,Xa). // Of course, this does not provide enough possible // values to be cryptographically secure. int Xa = random.nextInt(100); int Ya = (int) (Math.pow(G, Xa) % P); 

Edit: Debug code (below works for me):

 double G = 42; int P = 26; Random random = new SecureRandom(); int Xa = random.nextInt(100); double val = Math.pow(G, Xa); System.out.println("Xa: " + Xa); System.out.println("(double) Math.pow: " + val + " (int): " + (int) val); int Ya = (int) (val % P); System.out.println("Ya: " + Ya); 
+2
source

The problem is that the Random class in Java has a constructor with one long argument (called a seed) that allows you to run a sequence of pseudorandom numbers in a specific way.

If you always use the same seed, you will always get the same sequence.

To solve the problem, try the following:

 Random randomNo = new Random(System.nanoTime()); int Xa = randomNo.nextInt(10000); 

Thus, the seed is always different, and the sequence changes every time you call the above line.

+3
source

Other people seem to have given good answers to the question with your generation of random numbers, so I will answer your question: "Is this right for Diffie-Hellman?"

Your understanding of Diffie-Hellman is a little from me, I think. First, you continue to use the term "private key" as if there is also a "public key". Diffie-Hellman key exchange is a method used to exchange a single symmetric key. There is no private key and no public key, there is only a key that both parties are going to use to encrypt their messages. Moreover, you said that this is the code for "generating" a key. With Diffie Hellman, he takes two tangos. This code is not enough to create the final product key. You need to send Ya to the second side and get something from this second side to complete the process. See below for more details.

Your formula for generating Ya correct, assuming Xa is what it should be. I'm a little worried about your understanding of what you should do with Xa because you reassign it to a random value after you created Ya . To create the final version of the key, you will need to insert Xa .

After you have created Ya , you must send it to the other side. The other side will return a certain number in return (let me call it R ). To create the final version of the symmetric key (call SK on it), you will need to calculate it as

 SK = (int)Math.pow(R, Xa) % P; 

In short, do not recount Xa after you calculate Ya , otherwise you will not be able to generate the key. The process is going on:

  • Create Ya (I just use this variable name because this is what you used).
  • Send Ya someone.
  • Get some number from the person you sent Ya to (called this R number in the example above).
  • Calculate that there should be a symmetric key that you will use for encryption using R , Xa and P (See Formula above for SK )
+3
source

This can only give different results if Xa is different. How did you create the Xa value? Most likely, you used a pseudo-random generator, which usually needs to be sown. If you take the default seed every time (the same seed every time), it will always return the same sequence of random numbers.

Try loading the generator using System.currentTimeMillis();

+1
source

All Articles