this is a prebuilt version of the
application so if we run the server and
two clients
so this will be the server and this
the first client and this one the
second one so Alice picks those two
prime numbers and calculate n and phi of
n and she picks this public exponent
from this set where the inverse of e
exists
meaning the gcd of e and phi of n is
equal to 1 so she ends up with this
public key made up of m and e and this
private key
and Bob picks those two prime numbers
and calculate n and phi of n
and he picks this public exponent from
this set where the inverse of e exists
meaning the gcd of e and phi of n is equal to 1 so he ends up with this public key and
this private key
so now both Alice and Bob can send and
receive signed messages from each
other so let's say Alice wants to send
this message to Bob so she starts by
mapping the characters to ASCII and
obtaining the ASCII message and then
she needs to enter her private key to
sign the message with so this key so
let's first try with a made-up key and
see what happens so she ends up with
this signature s and this signed message
made up of the message and the signature
so x and s and the she sends that to Bob
and he receives that signed message and
uses Alice's public key to determine if
the calculated message x' is the
same as the message x so he calculates x'
and compares it to x and it is
different so this is different from this
so he concludes that the message was not
signed with Alice's private key so
let's try now to send the same message
but sign it with Alice's private key so
191
so now Bob receives that signed message
and uses Alice's public key to determine
if the calculated message x' is
the same as the message x so this turns
out to be x' and it is actually the
same as x so the message was signed with
Alice's private key now here we did
not encrypt the message so we do not
have any confidentiality since Eve can
see the message and since only Alice and
Bob have access to their private keys so
using those keys to sign messages
authenticate those messages as having
been signed by Alice or Bob and for Eve
to change the message she needs to have
access to Alice's private key and Bob's
private key otherwise if she did
change x then x and x' would not
match so we do have message integrity
since the message was not changed in
transit now before proceeding with the
code let's do a quick introduction so
this was the name of the original RSA
paper "A  method for obtaining digital
signatures and public key cryptosystems"
so digital signatures was part of RSA
from the start so our goal is to build a
prime factorization based digital
signatures and public key cryptosystem
so we start by multiplying two
large prime numbers p and q and
obtaining the result n now it is not
computationally feasible to figure out p
and q from n so in our cryptosystem n
has to be known and p and q unknowns and we want to set up the system in such a
way that there exist a private key d
that is the inverse of the public key e
and where it is very hard to figure out
d from e in mode something now if the
something is known then we can use the
extended Euclidean algorithm to figure
out the inverse and this was the
extended Euclidean algorithm and it is
used to compute the inverse modulo of an
integer so for example to compute the
inverse of 7 in mode 19 we end up with
11 as the inverse so the something has
to be the unknown in our crypto
system meaning it has to be based on p
and q but we also know that for two
prime numbers p and q the Euler Phi
function Phi of n is equal to p minus 1
times q minus 1 so this was euler phi
function so phi of m denotes the number of
integers in Zm that are relatively
prime to m where Zm is equal to the
set of integers from 0 to m minus 1
and two numbers are relatively prime if
their gcd is equal to 1 for example if m
is equal to 15 then Z of 15 is this
set from 0 to 14 so we take the gcd of 0
and 15 and 1 and 15 and 2 and 15
etc and we end up with those GCDs
equalling to 1 so we have eight of them
so Phi of 15 is equal to 8 now if m is
very large it is not computationally
feasible to
count GCDs so we use those formulas
for m and Phi of m so m is equal to p1
to the power of e1 times p2 to the
power of e2 times etc pn to the power of en
and this is Phi of m where pi is a
distinct prime number and ei is a
positive integer for example if m is 200
then we end up with 5 to the power of 2
times 2 to the power of 3 so p1 is 5 p2
is 2 and e1 is 2 and e2 is 3 so we
end up with Phi of 200 is equal to 80
now if p is prime than Phi of p is equal
to p to the power of 1 minus p to the
power of 0 so it is equal to p minus 1
and the GCD of p and q is equal to 1
means that Phi of p times q is equal
to Phi of p times Phi of q and if n
is equal to p times q and p and q are
primes then Phi of n is equal to Phi of
P times Phi of Q so Phi of n is equal to
p minus 1 times q minus 1 from here so
if m is equal to 15 then we end up with
Phi of 15 is equal to 8 so same what we
obtained by counting GCDs
and we know that for very large numbers
it is not computationally feasible to
obtain all GCDs and count the
relatively prime numbers so the idea is
to use phi of n as the something and set
up the system such that d is the inverse
of e in mod Phi of n and e is in the
set of 1 to Phi of n minus 1 so we end
up with those 5 steps so the public key
will be n and e and the private key will
be d and we encrypt by using the public
key and we decrypt by using the private
key and for digital signature it is the
reverse we sign using the private key
and we verify using the public key so we
use the private key d to sign x in Zn
from 0 to n minus 1 and we obtain s the
signature is congruent to X to the power
of d in mod n and we verify the
signature s in Zn from 0 to n minus
1 by using the public key n and e and we
obtain x' is congruent to s to the
power of e in mod n and if x' is
congruent to x in mod n then we have a
valid signature and if x' is not
congruent to x in mod n then we have an
invalid signature I will start by
creating a new project
and here we will have two packages
one for the server and another one for
the client
and on the server side 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
which will extend from thread and this
class will contain the RSA digital
signature logic
and the server thread will have
those three instance variables a
server and a buffered reader and a
print writer and the constructor takes
in a socket and a server and it uses the
socket to initialize buffered reader and
the print writer and we have this method
that forwards messages
and the server will accept connections
on this port and it will have a set of
server threads for the different clients
that this server is interacting with so
while this server is running
we accept connections from clients and
instantiate server threads for those
connections and add those server threads
to this set and start the server thread
and here Eve indicates that she can see
all the communications and this is just
a get method for the server threads and
this method does message forwarding from Alice to Bob or the other way around
and here when we call start we end up
triggering a call to this run method
when the operating system decides to do
so
actually I forgot to pick up this
package from here
so we'll go to properties and Java build
path
so if the JSON message coming in through
the server contains the public key then
I indicate what that public key is so
n and e and indicate that we need
the private key d and to obtain it we
need to do prime factorization of n into
p and q so if we obtain p and q then we
can obtain Phi of n and if we obtain Phi
of n then we can obtain the private key
d
so this should do it on the server side
and the client will have those instance
variables
and get and set methods for them
so we will keep track of the username
for this client and for the other client
that this user is interacting with and
n for this client and for the other
client and the public exponent for this
client and for the other client and
private the key for this client now
going here we will have those two
methods for calculating d the private
key and Phi so p minus 1 and q minus 1
and this method checks if a number is
prime and this one checks if two numbers
are relatively prime and this one turns
character to ASCII and this one ASCII to
character
and this method handles picking up p and
q from the user and the user name and
here we make use of the isPrime method
and we set n to be p times q and we
calculate and set Phi and we prompt the
user for the public exponent and we make
use of the is relatively prime method
and we end up calling this method from
here we calculate and set d the
private key and send the public key to
the other user and this method
handles the receiving of public keys
so here we are picking up e and n
the public key and the name from the
JSON message and this method handles the signing of a message
and this one handles the sending of signed
messages
so here we prompt the user to enter the
key to sign the message with and we call
the sign message method and we send the
signed message so this is the message
and this is the signature and this method
validates the signature on the signed
message so we use the public key of the
other client to determine if the
calculated message x' is the same
as the message x and here we calculate
x' and compare it to x
and this method handles the receiving of
messages so it makes use of this method
here
and we indicate if the message was
signed with the other party private
key or it was not
so this should do it for this class
and here we will have those two instance
variables and we will initialize them in
the constructor
which takes in a socket and a client and
uses the socket to initialize the
bufferedreader instance and the server
will be listening on this port
and we instantiate and start a client
thread which will end up triggering a
call to this run method when the
operating system decides to do so so if
the incoming JSON message contains the
public key then we call the handle
receive public key and if it contains
the signed message then we call the
handle receive message
and here we handle key generation and
the user entering and sending signed
messages
and the finally let's go ahead and test
run the application
 
and this will be the server
and this is the first client and this the
second one so Alice supplies those two
prime numbers and this public exponent
and ends up with this public key and
this private key
and Bob supplies those two prime numbers
and this public exponent
and he ends up with this public key and
this private key
and let's have Alice send this message
and sign it with a made-up key
so she end up sending this signed
message to Bob
and he receives it and determines that
that message was not signed with Alice's
private key so let's go ahead and pick
up her private key
and let's try to send the same message
again and now sign it with her private
key
so now Bob receives that message and
determines that it was signed with
Alice's private key and let's have
Bob try to send this message to Alice
and sign it with a made-up key
so Alice determines that the message was
not signed with Bob's private key so
let's have him send that message again
but this time we'll pick up his private
key
and
sign the message with it
so now Alice determines that the message
was signed with Bob's private key
 
