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 client and
here we are running in ASCII mode which
makes it easier for testing and later on
I will run it in regular mode so Alice
uses this number as p and this number as
alpha and this one as the private key d
and she calculates beta and sends p
alpha and beta to Bob and Bob
receives those p alpha and beta and he
will be using this as his p and this one
has his primitive element alpha and this
number as his private key d so he
calculates beta and sent his p alpha and
beta to Alice
and she will try to send this ASCII
message and she will choose this
ephemeral key and let's first try to
sign the message with a made-up key and see
what happens
so she calculates r and s the
signature parameters and sends the
message x with the calculated r and s to
Bob and he receives those and calculates t
so he calculates this so t congruent to
(beta^ r )*(r ^ s) all of that mod p and
he also calculates alpha to the power of
x in mode p and he determines that t is
not congruent to alpha to the power of x
in mod p which means that the message
was not signed with Alice's private key
so let's do the same thing except now
sign it with her private key so we'll
use the same ephemeral key and her
private key d is this number
so she does the same thing she
calculates r and s and sends x and r and
s the message with the parameters to Bob
and he receives those and calculate t
and alpha to the power of x and they're
both same so t is congruent to alpha to
the power of x in mode p which means
that we have a valid signature and
Eve can see Alice's p alpha and beta and
Bob's p alpha and beta and she can see
both messages that were sent from Alice
to Bob but in order to sign those
messages as being Alice
she needs to solve two discrete
logarithm problems then she can use d
and the ephemeral key to sign the messages
this way so s is congruent to x minus d
multiplied by r all of that multiplied by the
inverse of the ephemeral key in mode p
minus 1
now before proceeding with the code
let's do a quick summary and also a
proof so Alice wants to send signed messages to Bob so she starts by
selecting a large prime number p and
then picking a generator alpha in
Zp* or a subgroup of that and then
she picks a private key v from 2 to p
minus 2 and she calculates the public key
beta which is alpha to the power of d
in mod p and she sends p alpha and beta
to Bob and then she picks for each
message she picks an ephemeral key from
0 to p minus 2 such that the greatest
common divisor of the ephemeral key and
p minus 1 is 1 and then she calculates
the signature param for that message
so r and s and r is congruent to
alpha to the power of the ephemeral
key in mod p and s is congruent to x
the message minus d multiplied by R
multiplied by the inverse of the
ephemeral key in mode p minus 1 and she
sends x and the parameters r and s to
Bob and in order to validate that the
message was signed by Alice Bob
calculates this so beta to the power
of r multiplied by r ^ s he validates if
it's congruent to alpha to the power of
x in mod p and if so then we have a
valid signature otherwise we have an
invalid signature so what we want to
prove is that starting from here
alpha ^ x congruent to (beta ^ r)* (r ^s)
in mod p so if we start from here we
can end up in here and s is congruent to
x minus d times r multiplied by the
inverse of the ephemeral key in mode p
minus 1 so we replace beta by alpha to
the power of d and up with alpha to the
power of d to the power of r and we
replace r by alpha to the power of the
ephemeral key so we end up with alpha to
the power of the ephemeral key to the
power of s all of that in mode p so we
get this alpha to the power of d times r
multiplied by alpha to the power of the
ephemeral key times s all of that in
mod p and alpha to the power of d times
r plus the ephemeral key times s in
mod p and from fermat little
theorem we have this a to the power of m
is congruent to a to the power of m mod
p minus 1 all of that in mod p so we
use 1 & 2 this gives us that x is
congruent to d times r plus the
ephemeral key times s in mod p minus 1
and if we rearrange we end up with s is
congruent to x minus d times r
multiplied by the inverse of the
ephemeral key in mod p minus 1 so this
I will start by a creating a new
project
and we will need this jar file to do
JSON so we will go to properties and
Java build path
and we will have two packages one for
the server side and another one for the
client side
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 will 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 with the server side
and here we will have those three
instance variables a server and a
buffered reader and a print writer and
we will initialize those in the
constructor which takes in a socket and
a server instance and it uses the socket
to initialize the buffered reader and
the print writer and this method does
message forwarding so it uses this print
writer to do that
and the server will be listening on this
port and it has two instance variables
a server socket and a set of server
threads for handling communications
between this server and the different
clients that it is interacting with and
we will have this get method for the
set and this method does message
forwarding from Alice to Bob or the
other way around so it uses the forward
message method on the server thread and
in main we will instantiate a server
instance and a server socket instance
passing in this port number and Eve
indicates that she can see all
communications and when a connection
comes in from a client we accept it we
use the server socket to accept it and
we have a server thread that will handle
communications with that client and we
add that server thread instance to the
set of server threads and then we call
the start method which will end up
triggering a call to this run method
when the operating system decides to do
so
and here Eve indicates that she can
see p alpha and beta for Alice or Bob
and if we have a signed message then she
indicates that in order to change the
message and sign it again
she needs to solve two discrete
logarithm problems and obtain d and the
ephemeral key so she would obtain d by
solving this discrete logarithm problem
log base alpha of beta in mod p and she
would obtain the ephermal key or ephemeral key by solving this discrete
logarithm problem
log base alpha of r in mod p
so this should do it for the server side
and here we will keep track of this
client name and p and alpha and
the private key d and the public key
beta and the other party name and p and
alpha and beta and this flag indicates
if we are running in ASCII mode and this
one indicates if we are ready to send
and receive messages and we have get and
set methods for those and this one turns
character to ASCII and this ASCII to
character and this one checks if two
numbers are relatively prime and the
client thread keeps track of those two
instance variables the bufferedreader
and the client and they are initialized in
the constructor which takes in a socket
and a client instance and uses the socket
to initialize the bufferedreader and
this method handles the setup phase so it
prompts the user to enter the name and
p which is a prime number and a
primitive element alpha and a private
key d chosen from this set from 2 to p - 2 and it picks up those values
and calculates the public key beta and puts
everything in a JSON object so the name
and p and alpha and beta and sends it to
the other client and we will have this
method for signing and sending messages
and this one handles the communication so it
initially calls the handle setup phase
and when this happens we break out of
this always true loop and we go into
this other always true loop and here we
can sign and send messages and we
call this communicate method from here
so here we indicate if we are running
in ASCII mode and the server will be
running on localhost for the purpose of
this tutorial and on this port number
and we instantiate a client thread for
handling incoming messages and we call
the start method on it which will end up
triggering a call this run method when
the operating system decides to do so
and we call the communicate method
passing in this buffered reader
and this method handles the sending of
signed messages so it takes in a message
and r and s and packages those in a
JSON object and sends them and this
method calculates the signature params
so it takes in the message and a
bufferedreader and the ephemeral key and it
prompts the user to enter the private
key to sign the message with and it
calculates the signature params r and
 s and calls this method to send the
signed message and this method ends up
calling the calculate signature params
to sign and send messages so this
handles if we are not running in ASCII
mode so we call this static method
character to ASCII and here we prompt the
user for the ephemeral key and we check
if it is relatively prime with p minus 1
and i think this should do it for this class
and this method handles
verification of signature so it takes in
x and t the message and t and this is
the value for t and it calculates alpha
to the power of x in mod p and if t is
equal to alpha to the power of x then we
have a valid signature and otherwise
we have an invalid signature and this
method handles the receiving of the
message and calculates t and calls this
verify signature method passing in X
and t
and this is the code that receives the
JSON object and determines if it
contains the message or if it contains
the public key so here we set the name and
p and the Alpha and beta for the
other party and here we end up calling
the receive message method
so this should do it for this class
and finally let's go ahead and test run
the application so we will have a
server and two clients
and this will be the server and this is the 
first client and this the second one and
Alice will have this value for p and
this one for the primitive element alpha
and this one for the private key d and
she calculates the public key beta and sends
p alpha and beta to Bob and he receives
those and he in turn would use this
value for his p and this one for his
primitive element alpha and this for his
private key d and he calculate his beta
and sends his p alpha and beta to Alice
and let's say Alice wants to send this
message so she picks this ephemeral key
and let's go with a made-up key for now
and see what happens
so she signs the message with that made
up key and calculates signature
parameters r and s and sends the
message with r and s to Bob and he
receives those and calculates t and
calculates alpha to the power of x and
compares it to t and determines that t
is not congruent to alpha to the power
of x in mod p so the message was
not signed with Alice's private key so
let's do it again and this time let's
use the same ephemeral key but use the
private key for Alice this time
so she does the same thing she
calculates the signature params r and s
and sends the message with those params
to Bob and he received those and
calculates t and alpha to the power of x
 mod p and they're both the same so
he determines that we have a valid
signature so this message originated
from Alice and if he wants to send this
message with
this ephemeral key and let's have him
sign it with his private key
so Alice receives that massage
and calculates t and alpha to the power
of x and they're both the same so she
determines that we have a valid
signature and the message originated
from Bob and she sends the same message
with the same ephemeral key but with
a made-up private key then Alice would
determine that the message was not
signed with Bob's private key
