this is a prebuilt version of the
application so if we go ahead and start
the server and two clients
and this will be the server and this is
the first client
and this is the second one and p here is
prime and alpha is a cyclic group
generator so both Alice and Bob
agree to share those two values for p and alpha
and for the purpose of this tutorial I
had the first user that logged in enter
the values
so in the previous diffie-hellman
tutorial i had a typo here so this is the
correct display here and here so here we
will be doing a deterministic encryption
scheme and what that means is let's say
so we send this message from Alice to
Bob and if we send it again we will be
sending the same cipher text
so a deterministic encryption scheme is
a cryptosystem which always produces the
same ciphertext for a given plaintext
and key even over separate executions of
the encryption algorithm and examples of
deterministic encryption algorithms
include RSA without encryption padding
and deterministic encryption can leak
information to an eavesdropper who may
recognize known ciphertext so this
tutorial is a precursor to Elgamal
algorithm tutorial which also uses or is
based on diffie-hellman and uses the
multiplicative masking but it is a
probabilistic encryption scheme so we
would not see the same ciphertext for
the same message I will start by
creating a new project
and this will be heavily refactored from
the previous diffie-hellman key exchange
project so we will have two packages 
a server one and a client one and
same as before in the server package we
will have two classes a server class
with a main method
and a server thread class
which will extend from thread and on the
client side we will have three classes
a client class with a main method
and a client thread class
and this will extend the thread
class and a diffie-hellman key exchange
helper class
which will contain all the
diffie-hellman key exchange logic and the
multiplicative message encryption and
decryption logic
and to do JSON we will use this
package as in the previous diffie-hellman
tutorial so we go to properties and Java
build path
and the server thread will have a server
and a buffered reader and a print
writer instance variables and those will
be initialized in the constructor
which will take in a socket and the
server instance so we will use the
socket to initialize the buffered reader
and we will use it also to initialize the
printer writer and the server will have a
port number that it will be listening on
and a set of server threads with the
different clients that it is interacting
and here in main we start the server
and the server socket given its port
number and we indicate that Passive
Eve is watching all communications and
we accept connections from various
clients and we'll have a server thread
dedicated to it and we put all the
server threads in this set and we start
that thread and when we do that the run
method will end up being executed when
the operating system decides to do so if
we go back here we will have those two
methods so this is just a get method and
this method forward messages from Alice
to Bob and the other way around and
let's add this forward message method
and we will have this code in the run
method which will handle incoming JSON
messages
and we'll open up those messages and
display their contents before forwarding
it to the other client
so here we are doing the forwarding so
this should do it on the server side and
next we will do the client side
now this class will keep track of the
client and p and alpha and the
private and the public keys and the
common key and the client is initialized
in the constructor and we will have those
get and set methods
and the client class will have those
three instance variables and get and set
methods for them and those two static
methods this one turns character to
ascii and this one turns ascii to
character and the client thread will
have those three instance variables and
we will initialize them in the
constructor
which also takes in a socket now the
client will have two methods
this one will handle everything until
obtaining a common key and this one will
handle communication using a common key
multiplicative mask
and in main we will start by instantiating
a client
and a diffie-hellman key exchange helper
instance passing in the client and for
the purpose of this tutorial the server
will be on localhost listening on this
port number so we instantiate a socket
and we instantiate and start a client
thread which will end up triggering a
call to the run method when the
operating system decides to do so on the
client thread so here we are passing
the socket and the client and the
diffie-hellman key exchange helper
instances and we use the socket to
initialize this print writer instance
variable and if we are ready to
communicate and we have obtained a
common key then we go ahead and call the
handle communicate method and both
those methods the pre communicate and
the communicate they take in a diffie-hellman
key exchange helper instance variable
and this method will initialize the
domain parameters
so here we prompt the user for the prime
number p and the generator alpha and their
name and we pick those up and for the
purpose of this tutorial I had the first
client that logs in enter the value for
p and alpha the agreed on value for
those so we put those values the name and
p and alpha in a JSON object and we send
it to the other client and this method
calculates the public key so we prompt the
user for a secret key and we use it
to calculate the public key and we put it in
a JSON object and send it to the other
client and this method calculates the common
key
and returns that key and those two
method do the encryption and decryption
using that common key so here we are
using that common key to multiply the
passed in message in order to encrypt it
and return everything as a string so we
put everything in the this string buffer
and return it as a string and to 
decrypt we're using the inverse of the
common key and mod p so we are
multiplying by that and returning the
decrypted message
now going to the client thread class
this method handles incoming messages
so we pick up the message and we decrypt
it using the common key and displaying
some information and turn it back into
character from ASCII and display what
happened and this is the code for the
run method
so here we are picking up p and 
alpha from the other client and here we
are picking up the public key and here
we are prompting the user to press ENTER
in order to generate the common key
otherwise if this is a message then we
are handling it here
and
this method handles all the pre
communications
for this client so here we are
initializing the domain parameters and
here we are calculating the public key
and here the common key and we return
a ready to communicate flag and this
method handles all the communications
so here we are telling the user that we
are ready to send and receive messages
so here we turn the character
message into ascii
and here we encrypt it and we display
some information and we send that
message to the other client in a JSON
object
and finally let's go ahead and test
run the application
so this will be the server
and this is the first client and this is
the second one so let's say Alice and
Bob agreed to use those two values
for p and alpha and Bob chooses his
secret key b as being 5 and
Alice chooses 3 for a and
Alice calculates her public key
A and Bob does the same thing
for his public key B and
then they both generate their common
keys which end up being the same 78 mod
467 and Passive Eve can see
everything except a
b and the calculated common
key and in order to figure out what the
common key is she needs to find what
b is or what a
is and in order to do either of those
she needs to solve a discrete logarithm
problem which is very hard for very
large numbers
so Alice will encrypt with the common
key and Bob here would decrypt with the
inverse of the common key and obtain
the message that Alice sent and if
Bob wants to send a message to Alice he
would encrypt with the common key and
Alice would decrypt with the inverse of
the common key
