I have the line of code below to generate a private key:
int Xa = randomNo.nextInt(10000);
int Ya = (int) Math.pow(G, Xa) % P;
G
and P
are static numbers. Whereas Xa
is randomly generated. Every time I run the program, it gives me the same result for Ya
. Is this correct for Diffie-Hellman? I thought the private key had to be changed every time the algorithm was run.
I think the problem may be that you are overflowing double with your exponentiation, resulting in infinity, resulting in the same value every time (unless you are lucky enough to end up with a very low number returned for your exponent).
Also, be sure to use secure random to get your random value:
Edit: Code with debugging (the below works for me):
This can only give different results if
Xa
is different. How did you generate the value ofXa
? Chances are you've used a pseudo-random generator that typically need to be seeded. If you take the default seed each time (same seed each time) it will always return the same sequence of random numbers.Try seeding your generator with
System.currentTimeMillis();
Other people seem to have given good answers on the issue with your generation of random numbers, so I'll respond to your question "Is this correct for Diffie-Hellman?"
Your understanding of Diffie-Helman is a bit off I think. For one thing, you keep using the term 'private key' as though there is also a 'public key'. Diffie-Hellman key exchange is a technique used for exchanging one symmetric key. There isn't a private key and a public key, there is just a key that both parties are going to use to encrypt their messages. Moreover, you said that this is code for 'generating' a key. With Diffie-Hellman, it takes two to tango. This code isn't enough to generate the final product of the key. You'll need to send
Ya
to a 2nd party and get something back from that second party to finish the process. See below for more info.Your formula for generating
Ya
is correct, assuming thatXa
is what it is supposed to be. I'm a little concerned about your understanding of what you're supposed to do withXa
because you're reassigning it to a random value after you've generatedYa
. You will need to hang on toXa
in order to create the final version of the key.After you've generated
Ya
, you should be sending that to the other party. The other party will send you back some number in return (let's call thatR
). In order for you to create the final version of the symmetric key (let's call itSK
), you will need to calculate it asSo in a nutshell, don't recalculate
Xa
after you've calculatedYa
, otherwise you won't be able to generate the key. The process goes:Ya
(I'm just using this variable name because it's what you used).Ya
to some person.Ya
to (called this numberR
in example above).R
,Xa
, andP
. (See formula above forSK
)The problem is that the
Random
class in Java has a constructor with onelong
argument (called seed) that allows you to start the pseudorandom number sequence in a particular way.If you always use the same seed, you will always obtain the same sequence.
To solve the problem, try this:
In this way, the seed is always different, and the sequence changes everytime you call the above line.