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 is
the first client and this the second
one and Alice will pick this number as
her prime number d and this one as
her prime number q which is also a
divisor of (p - 1) and this will be
her subgroup generator alpha and this
one her private key d and she calculates
her public key beta and sends Bob her p, q, 
alpha and beta and Bob receives
those and he will pick those values for
his p q alpha and d and he calculates
his public key beta and sends his
information to Alice
and let's say she wants to send this and
let's mention that we are running here
in ascii mode since it is easier for
testing so in this mode Alice and Bob
will exchange ascii hash messages h(x)
so let's say she picks this as her ephemeral key and this will be her private
key that she needs to enter but let's
have her enter a made-up key and see
what happens
so she here she calculated her
signature param r and here she is
calculating her signature param s and
she is sending Bob her ASCII hash
message h(x) and her signature params
r and s and he receive those so this is
h(x) that she sends and he calculates w
u1 u2 and v and determines that v is not
congruent to r in mod q so the message
was not signed with Alice's private
key so let's have her send the same
message but this time sign it with her
private key so we use the same
ephemeral key and this is her private
key so Bob receives her message this
time and calculates w u1 u2 and v and
validates that v is congruent to r in
mod q so the message was signed with
Alice's private key and Eve here can
see
all the communications between Alice and
Bob so now she knows Alice's p q alpha
and beta and Bob's p q alpha and beta
and she can see the two messages sent
from Alice to Bob but in order to send
messages as being Alice she needs to
sign them and in order to do that she
needs to obtain Alice's private key and
to do that she needs to solve this
discrete logarithm problem
log base alpha of beta in mod p
and let's have Bob try to send this
ASCII hash message to Alice and have him
choose this ephemeral key so he
calculates r and his private key d
was this number
so Alice receives his ASCII hash message
and calculates w u1 u2 and v and
determines that the message was signed
with Bob's private key now before
proceeding with the code let's do a
quick summary and a proof so Alice wants
to send signed messages to Bob so she
starts by selecting a large prime number
p between two to the ten twenty three
and two to the ten twenty four and then
she selects a prime divisor q of p minus
one where q is between two to the 159 and
two to the 160 but for the purpose of
this tutorial I used much smaller
numbers for p and q and third Alice
selects the subgroup generator alpha and
she selects her private key d between
0 and the q and she calculates her
public key beta which is congruent to
alpha to the power of d in mod p and
she sends p q alpha and beta to Bob and
now each time she wants to send a signed
message to Bob she selects an ephemeral
key between 0 and q and then she
calculates r which is congruent to alpha
to the power of the ephemeral key in
mod p and all of that in mod q and
then she calculates s which is congruent
to the hash of the message x plus
the private key d multiplied by the
signature param r and she multiplies
all of that by the inverse of the
ephemeral key and all of that in mod
q and she sends the message x and
the signature params r and s to
Bob and Bob receives the signed
message and he calculates w which is
congruent to the inverse of s in mod
q and second he calculates u1
 which is congruent to w multiplied
by the hash of the message x in mod
q and third he calculates u2 which is
congruent to w multiplied by r in mod
q and forth he calculate v which is
congruent to alpha to the power of u1
multiplied by beta to the power of
u2 in mod p and all of that in
mod q and he validates the
signature by checking if v is congruent
to r in mod q and if that's the case
and we have a valid signature otherwise
we have an invalid signature and
finaly let's prove that the signature r s satisfies
this validation condition so we want to
start from here and arrive here so this
equation came from here and this
equation came from here so we rearrange
this equation we obtain the ephemeral
key is congruent to this and then
we have that w is congruent to this
and u1 is congruent to this and u2
is congruent to this so we use those
here and we get the ephemeral key is
congruent to u1 plus d times u2 in
mode q and we can raise alpha to
either side of the equation and reduce
mod p so we get alpha to the ephemeral key in mod p and alpha to u1 plus d u2
in mod p also and we have that beta is
congruent to alpha to the power of d in
mod p so we use that here
and finally we reduce both sides of the
equation in mod q and this turns
out to be r from here and this turns
out to be v from here so we get r is
congruent to v in mod q
I will
start by creating a new project
and here we need to do JSON so let's
pick up this jar from here
and we go to properties and Java build
path
and here 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 two classes a server class
with a main method
and a server thread class
and on the client side we also have two
classes a client class with a main
method and a client thread class
and both the client thread and the 
server thread classes will extend from
thread and we will start work on the
server side
and this class will have three instance
variables a server and a buffered
reader and a print writer and the
constructor takes in a socket and a server
instance and uses the socket to initialize
the buffered reader and the print
writer
and the server will be listening on
this port and we'll have two instance
variable a server socket and a set of
server threads and would use the
server socket in order to accept client
connections and dedicate a server thread
instance for each one of those
connections and then add that server
thread instance to the set of server
threads and call the start method on the
thread which will end up triggering a
call to this run method when the
operating system decides to do so and
let's add this method here that uses the print
writer to forward messages and it will
be called from here to forward
messages from Alice to Bob or the other
way around
and this is just a get method
and here when a message comes in
as a JSON object we check if that
message contains the public key for
Alice or Bob and we display what that
key is so p q alpha and beta and then if
that message contains x then
we indicate that Eve needs to solve this
discrete logarithm problem for Alice or
for Bob in order to sign messages as
being Alice or as being Bob so this
should do it for the server code
and here we will keep track of
this client name and p and q and
alpha and the private key d and the
public key beta and we will also keep
track of the other party name p q alpha
and beta and this flag indicates if we
are running in ascii mode and this one
if we are ready to send and receive
messages and we will have this is ready
flag method and those get and set
methods and this static method
checks if a number is prime and this one
hashes the message given a hashing
algorithm so the hash ends up being
signed and verified and not the
message itself so here we pick up the
hashing algorithm message digest
instance and then we add the bytes of
the message to the message digest then
we call digest to get the bytes for
the hash and here we encode the byte
data to hexadecimal format and then we
append it to the string buffer instance
we have and here we keep track of two
instance variables that we initialize
from the constructor which takes in a
socket and a client instance and we use
the socket to initialize the buffered
reader and we will have this method that
handles sending messages from this
client to the other client signed
messages and here we first check if we
are running in ASCII mode and we display
that message in that case and for
the purpose of this tutorial the server
will be running in localhost and
listening on this port
and we instantiate a client thread which
will handle receiving messages from the
server we call the start method on it
which will end up triggering a call to
this run method when the operating
system decides to do so
and this method handles the setup phase
so it prompts the user for the name and
for the prime number p and the prime
number q which is a divisor of p minus 1
and the generator alpha and his private
key d and it calculates the public key
beta
and it puts the name and p and q and
alpha and beta in a JSON object and
sends them to the other party so in the
communicate method we first make sure
that we handled the setup phase so
we stay in this always true loop until
this has been handled and we need a
method that does the pickup of the
message and hashes it and sign it and
sends it across so this method
does all that and we call it to send
messages from this always true loop
and we need two methods one to
handle the pickup of the ephemeral key
so it prompts the user for that and
another one to handle the pickup of the
signing key and it also prompts the user
for that and this method handles the
hashing of the message so here we are
using sha-256 and we only hash the
message if we are not in ASCII mode
otherwise we assume that the user has
entered h(x) so the hash instead of
the message since easier for testing and
here we first pick up the message from
the user and then we call the handle
message hashing to hash it and for the
purpose of this tutorial
I had the user enters the ephemeral key
and this method takes in the ephemeral key
and use it to calculate the signature
param r so this is how we calculate r r
is congruent to alpha to the power of
the ephemeral key in mod p and all of
that in mod q and we call this
method from here and this method
calculates the signature param s
so here we pick up the signing key and
this is how we calculate s
and this method sends the signed message
so x and the two signature params r
and s and we call those two methods
from here
so this should do it for this class
and here we need this method to
receive the other party p q alpha and
beta and we end up calling it from
this always true loop
otherwise if we are receiving a signed
message then we handle it here now this
method obtains the message hash this is
the value of x and we are using sha-256
to hash the message and return it and we
need those two helper methods this one
displays calculations for w and u1 and
u2 and v and this one validates the
message and indicates if the message is
valid so v is congruent to r in mod q
otherwise if the message is not valid so it was not signed with the
other party private key and this method
handles receiving the message so here we
calculate w u1 u2 and v and we display
the calculations and validate the
message and we end up calling
this method from here and here we obtained
the message hash so this should do it
for this class
and let's go here and change this and
make it default to false and update this one
need a + here
and finally let's go ahead and test run
the application so we'll have a server
and two clients
and this would be the server and this
the first client and this one the
second one and Alice will use this
number as her prime number p and this
one has her prime number q which is also
a divisor of p minus 1 and this will be
her subgroup generator alpha and this
one her private key d and she calculates
her public key beta and sends p q alpha
and beta to Bob and he will receive
those
and pics those numbers as his p q
alpha and d and he calculates his public
key beta and sends his information to
Alice
p q alpha and beta so let's say Alice
wants to send this message to Bob signed
message
so she calculate the hash for the
message and for the ephemeral key
let's say she picks this number and she
calculates the signature param r and
for d let's go ahead and pick a made-up
key for now so it should be this
number but let's go with this number and
see what happens so she calculates s
the other signature param and sends her
information or her signed message to Bob
so x r and s
and he receive those and calculate the
hash for the message and notice that the
two hashes are the same
and he calculates w u1 u2 and v and
determines that v is not congruent to r in
mod q so the message was not
signed with Alice's private key so let's
go ahead and have Alice send the same
message but this time sign it with her
private key so we'll use the same
ephemeral key and this time would use
her private key so she does the same the
same thing she calculates the signature
param s and sends her signed message
to Bob and he receives that and
calculate the hash for the message and
then calculate w u1 u2 and v and
validate that v is congruent to r in
mod q so the message was signed with
Alice's private key
and Eve can see all the communications
so she knows now Alice's p q alpha and
beta and Bob's p q alpha and beta and
she can see the messages sent from Alice
to Bob but in order to sign messages as
being Alice she needs to obtain
Alice's private key d and to do that she
needs to solve this discrete logarithm
problem
log base alpha of beta in mod p
and let's have Bob send this message to
Alice
and maybe choose this ephemeral key
and this was Bob private key d so
he calculates s and sends his signed
message to Alice and she does the same
thing receives it calculates the hash
then calculates w u1 u2 and v and
determines that v is congruent to r in
mod q so the message was signed with
Bob's private key
