at the moment I am working on a chat application, where more than two clients can communicate at the same time.
I just got started with the encryption so please be bear with me.
My goal is to implement end-to-end-encryption so I've done some research on that and I found about
Diffie–Hellman key exchange. I have a brief idea how it works, also plenty of questions.
On wikipedia it says that it works as following:
- We decide on a big prime number, we'll call it
- g has to be a
primitive prime root of P.
- Each client picks an arbitrarily private key which follows this condition:
1<= Private_key <= P
- They send their private keys which are generated as following:
(g^private_key) mod Pover the server
- the secret shared key is
(User2PublicKey^User1PrivateKey) mod Pand vice versa
(User1PublicKey^User2PrivateKey) mod P
My questions to you are:
- Does this work on a multi-client chat? I had some attempts on paper to emulate a connection for three clients and I couldn't get the same shared key. If so could you provide me an example for that please?
- I know it may sound silly, but P, g and the keys are just integers? Like P could be 139, g 37 and so on?
- Instead of encrypting the messages with the recipient's public key, couldn't I use the secret shared key for that? To avoid encrypting the message for each client connected.
- P and g could be generated on the server side?
- Could anybody provide me an example of implementing this in Java?