this is a prebuilt
version of the application so if we go
ahead and run the server and two clients
and this will be the server and this the
first client and this one the second one
and Alice will pick this number as
her prime number p and this one has her
alpha primitive element and for her
private key d from this set she will
pick this number and she calculates her
public key beta and sends all that
information to Bob so b alpha and beta
and bob in turn will pick this as
his prime number p and this number as
his primitive element alpha and this one
as his private key d and he calculates
his public key beta and sends his
information to Alice and here we are
running in ASCII mode so Alice and Bob
can only exchange ASCII messages so
let’s say Alice wants to send this asii
message to Bob and let’s use this as her
one-time private key for this message
so she calculates her one-time public
key or ephermal key for this message
and the one-time masking key for this
message and she is using i to do that
and the alpha for Bob and the b for Bob
and she uses the masking key to encrypt
the message and obtain this ciphertext y
and she sends the ephermal key and the
ciphertext to Bob
and Bob in turn receives the ephermal key
and the ciphertext and uses the ephermal
key to calculate the masking key for
this message and then uses the inverse
of the masking key to decrypt the
ciphertext message and obtain
the original message x
and if Bob wants to send this ASCII
message to Alice and using this one-time
private key so he does the same thing
he calculates the ephermal key and the
masking key for this message and uses
the masking key to encrypt the message
and sends the encrypted message and the
ephermal key to Alice and she receives
those and uses the ephermal key to
calculate the one-time masking key and
uses the inverse of the masking key to
decrypt the message y and obtain the
original message x
and Passive Eve can see all the
communications so now she has p alpha
and beta for Alice and same thing
p alpha and beta for Bob and she can
see this encrypted message with the
ephermal key going from Alice to Bob but
in order to decrypt it she needs to
obtain d or i and in order to do that
she need to solve one of two discrete
logarithm problems so either she has to
obtain d and solve this discrete
logarithm problem so log base alpha of
beta in mod p and then uses d to
calculate the masking key
and then uses the masking key to decrypt
the message and obtain x so using the
inverse of the masking key or she could
solve this discrete logarithm problem
log base alpha of the ephermal key in
mod p and obtain i then uses i to
obtainin the masking key and same thing
uses the inverse of the masking key to
decrypt the message y and same
thing here
for the message coming from Bob and
going to Alice
and before proceeding with the code
let’s do a quick summary and let’s also
show that when we do decryption we
obtain the original value of x so the
setup here is Bob wanting to send a
message to Alice an encrypted message to
Alice so we start with Alice selecting a
large prime number p and then she picks
a generator alpha in Zp* or a
subgroup of that and third she picks a
private key d in 2 to p-2 and then
she calculates the public key beta so alpha
to the power of d in mod p and she
sends p and alpha and beta to
Bob and Bob starts by picking a
private key i in 2 to p-2 and then he
calculates the ephermal key which is
alpha to the power of i in mod p
and third he calculates the masking
key which is beta from here to the
power of i in mod p and he encrypts
the message x using the masking key so
x multiplied by the masking key in mod p
will give us the encrypted message y and
he sends y and ephermal key to Alice and
Alice starts by calculating the masking
key so this would be congruent to the
ephermal key to the power of d in mod p
and then she uses the inverse of the
masking key to decrypt the
message y so here we want to show that
y multiplied by the inverse of the
masking key in mode p will end up giving
us x so it’s congruent to x so we start
y and we replace that with x
multiplied by the masking key and for the
masking key here we replace that with
the ephermal key to the power of d so we
have the inverse of that and we replace
the masking key with beta to the
power of i here and the ephermal key with
alpha to the power of i and we replace
beta with alpha to the power of d so we
end up with alpha to the power of d to
the power of i and here alpha to the
power of i to the power of d the
inverse of that so we have x multiplied
by alpha to the power of di minus di so
x multiplied by alpha to the power of 0
which would give us back x in
mode p I will start by creating a
new Java project
and to do JSON
I used this jar on this previous project
I will copy it and go to properties and
Java build path
and we will have two packages one for
the server code and another one for the
client code and on the server side we
will have 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 a client class
with a main method
and a client thread class and it
will extend from thread and the Elgamal
logic will be in this class
and this class will have those three
instance variables which will be
initialized in this constructor that
takes in a socket and a server and here
we are using the socket to initialize
the buffered reader and we are also
using it to initialize the print
writer and the server will have two
instance variables a server socket
and a set of server threads and it will
be listening on this port for incoming
client connections and when a new
connection comes in it will accept it
and we will have a server thread that
will handle communications with that
client and we will add that server
thread to the set of server threads and
we would call the start method which
will end up triggering a calls to this
run method when the operating system
decides to do so
and let’s add this method here that
forwards messages from Alice to Bob and
the other way around
so here this server thread is handling
communications with a specific client so
it’s picking up JSON messages and
indicating if it is a message that
indicates what are the public key for
this client then Passive Eve indicates
that she can see that so p alpha and
beta otherwise if it is a message that
contains encrypted message and the
ephermal key then passive Eve indicates
that in order to decrypt that message
she needs to obtain d or i and in order
to do that she needs to solve one of two
discrete logarithm problems so to obtain
d she needs to solve this one and to
obtain i she needs to solve this one and
when she if she is able to solve this
one then she can use it to calculate the
masking key and then she can use the
masking key to decrypt the message and
same with i if she obtains i then she
can use it to calculate the masking key
and then use the masking key to decrypt
the message or the inverse of the
masking key
so this should do it on the server side
and on the client side
this class will have those instance
variables so it will keep track of its
name and the other party name and p and
alpha and d and beta for this client
and p and alpha and beta for the
other client and this flag indicates
that this client is ready to send and
receive messages and this flag indicates
the type of messages that we can send
and receive so if it is true then we can
only send and receive ASCII messages and
here we have get and set methods for
those instance variables and we have two
static methods this one turns character
to ASCII and this one turns ascii to
character and the client thread will
have those two instance variables and
the constructor takes in a socket and a
client and we use the socket to initialize
this instance variable
and the elgamal helper class will have a
client instance variable and it is
initialized in the constructor
and this method handles communication
so here we indicate if we are running in
ascii mode and for the purpose of this
tutorial we will be running on localhost
and the server will be listening on this
port and when we call start here on
the client thread we will end up
triggering a call to this run method
when the operating system decides to do
so
and this method handles incoming
messages so here we are picking
up p and alpha and the name and
beta from the other client and in
this case we indicate that we are ready
to send and receive messages and here we
will have those two static methods to
handle encryption and decryption of
messages
and this method handles the setup phase
so picking up p and alpha and the
private key d from the user
and sending them to the other client in
a JSON object
and this method that handles incoming
messages ends up calling
the decrypt message this static method
and this is the code for this method
so here we are sending the ephermal key
and the encrypted message as a JSON object
and here we pick up the one time private
key from the user and we use that to
calculate the ephermal key and the masking key for this message and here we
call the static encrypt message on the
elgamal helper class
and we call this method from here

and finally let’s go ahead and test run
the application
so this will be the server
and this one the first client and this
the second one
and Alice will pick this p prime
number and this primitive element alpha
and this private key d and she
calculates her public key beta and
sends her p alpha and beta to Bob and
Bob picks this p and this alpha and this
p and he calculates his public key
beta and send all that information to
Alice and if Alice wants to send this
message to Bob and maybe she picks this
private key
so she calculates her the public key for
this message or the ephermal key and
the masking key and maps the characters
to ASCII and encrypts the ASCII message
using the masking key and sends the
encrypted message and the ephermal key to
Bob
and he receives those and calculates the
one-time masking key for this message
using the ephermal key and decrypt the
message using the inverse of the masking
key and obtain the original or maps
the ASCII to character and obtains the
original message coming in from Alice
and let’s say he sends this message to
Alice and maybe picks this one-time
private key so same thing he calculates
the one time ephermal key and
calculates the one time masking key and
then maps the characters to ASCII and uses
the masking key to encrypt the message and obtain y the encrypted message and
sends the ephermal key and the encrypted
message to Alice and she receives those
calculates the one-time masking key and then uses the inverse of the masking key to
decrypt the message and obtain x and
then maps the ascii to character and
obtains the original message coming in
from Bob
and Passive Eve can see all the
communications so she have p alpha and
beta for Alice and p and alpha and
beta for Bob and she can see this
message coming from Alice and going to
Bob and in order to decrypt it she
needs to solve one of two discrete
logarithm problems and obtain d or i and
so she can solve this discrete logarithm
problem
log base alpha of beta in mod p and
obtain d and then use d to calculate the
masking key and use the masking key or the inverse of the masking key to decrypt
y and obtain x or she can solve this
discrete logarithm problem and obtain i
and use i to obtain the masking key and
same thing use the inverse of the
masking key to obtain the original
message x and here we’re doing the same
thing for a message coming from Bob and
going to Alice