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:
- Generate
Ya
(I'm just using this variable name because it's what you used).
- Send
Ya
to some person.
- Receive some number from the person you sent
Ya
to (called this number R
in example above).
- Calculate what the symmetric-key should be that you'll be using for encryption using
R
, Xa
, and P
. (See formula above for SK
)