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.
The problem is that the Random
class in Java has a constructor with one long
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:
Random randomNo = new Random(System.nanoTime());
int Xa = randomNo.nextInt(10000);
In this way, the seed is always different, and the sequence changes everytime you call the above line.
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-Hellman 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 that Xa
is what it is supposed to be. I'm a little concerned about your understanding of what you're supposed to do with Xa
because you're reassigning it to a random value after you've generated Ya
. You will need to hang on to Xa
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 that R
). In order for you to create the final version of the symmetric key (let's call it SK
), you will need to calculate it as
SK = (int)Math.pow(R, Xa) % P;
So in a nutshell, don't recalculate Xa
after you've calculated Ya
, 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 number R
in example above).R
, Xa
, and P
. (See formula above for SK
)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With