this is a prebuilt version of the
application so if we go ahead and run
the server and two clients
so this will be the server and this is
the first client and this one the second
client so here we will have Alice
and she will pick those two prime
numbers and this public exponent and
Bob will pick those two prime numbers
and this public exponent
and alice is sending this message to Bob
so she would start by mapping the
characters to ASCII and obtaining this
ASCII message and then she would encrypt
it with Bob's public key so she would
obtain this ciphertext which she would
send to Bob and Bob in turn would
receive the ciphertext and decrypt it with
his private key and obtains this ASCII
message which he would map to character
and obtain the original message coming
in from Alice and Bob in turn will
send this message to Alice so he would
start by mapping the character to
ASCII and obtaining this ASCII message
and then he would encrypt it with
Alice's public key and obtain this
ciphertext which he would send to Alice
and Alice in turn would receive it and
decrypt it with her private key and
obtain this ASCII message which she
would map to character and obtain the
original message that is coming from Bob
and Passive Eve here can see all the
communications so she can see Alice's
public key and Bob's public key and
the cipher text messages sent from Alice
and from Bob but in order to decrypt
those
she needs Alice's private key
and Bob's private key and she can only
obtain those by factoring the n into p
and q so Alice's n into the original p
and q and Bob's n into the
original p and q now before
proceeding with the code let's do a
quick introduction so this is the
extended Euclidean algorithm and it is
used to compute the inverse modulo of an
integer so for example to compute the
inverse of r1 mod r0 where r1 is
smaller than r0 that inverse only
exists if the greatest common divisor of
our r0 r1 is equal to 1 so we apply this
extended Euclidean algorithm and obtain
s r0 + t r1 = 1 which is
equal to the GCD of our r0 r1 and we end
up with the inverse of r1 in mod r0 is
equal to t and this is an example of
computing the inverse of 7 in mod 19 so
we end up with 11
now the euler phi function denotes the
number of integers in Zm that are
relatively prime to m where Zm is the
set of integers from 0 to m - 1 and
two numbers are relatively prime if
their GCD is equal to 1 for example if m
is 15 then Z of 15 is 0 to 14 and if
we want to calculate Phi of 15 then we
take the GCDs of all those numbers and
we count the GCDs with result
equaling to 1 so we have 8 of them so 5
of 15 is equal to 8 now if m is a very
large number it is not computationally
feasible to take the GCDs of all the
numbers from 0 to m - 1 so we use
those two formulas for m and for Phi of
m so m is equal p1 to the power of e1
times p2 to the power of e2 etc until
Pn to the power of En and Phi of m is equal to this formula and an example is if m is equal
to 200 then it is equal to 5 to the
power of 2 times 2 to the power of 3 so
p1 is 5 and p2 is 2 and e1 is 2 and e2
is 3 and we apply this formula here
and end up with 5 to the power of 2
minus 5 to the power of 1 times 2 to the
power of 3 minus 2 to the power of 2 and
we end up with 20 times 4 is equal to 80
now if p is prime then Phi of p is equal
p to the power of 1 minus p to the power
of 0 so 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 p and q are
primes and n is equal to p times q then
applying this formula we end up with Phi
of n is equal to p minus 1 times q
minus 1 so if m is equal to 15 it
is equal to 3 times 5 so we end
up with 3 minus 1 times 5 minus
1 which is equal to 8 which is the
same result that we obtained by
Counting GCDs
so our goal is to build a prime
factorization based crypto system so we
start by multiplying two very large
prime numbers p and q and obtain the
result n now it is not computationally
feasible to figure out p and q from n so
in our crypto system n has to be known
and p and q unknowns and we want to
set up the system in such a way that
there exists a private key d that is the
inverse of the public key e or the
public exponent e but where it is very
hard to figure out d from e in mod 
something now if that something is known
then we can use the extended Euclidean
algorithm in figuring out the
inverse so the something has to be the
unknown in our crypto system meaning
that something has to be based on p and
q from here 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 and we know that
for very large numbers it is not
computationally feasible to obtain all
GCDs and count relatively prime numbers
so the idea is to use Phi of n as
that 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 we start by choosing large prime
numbers p and q such that p is different
than q and then we calculate n is equal
p times q and third we calculate Phi of n is
equal to p minus 1 times q
minus one and forth we choose a public
exponent e in 1 to Phi of n minus 1 such
that the GCD of e and Phi of n minus 1
is equal to 1 and finally we compute the
private key d such that d times e is
congruent to 1 in mod Phi of n so our
public key will be made up of n and e
and our private key will be d now to
encrypt a message m that is in Zn from
0 to n minus 1 we use the public key n and
e to encrypt m and obtain the ciphertext
c is equal to m ^ e in mod n and
to decrypt a ciphertext c in Zn from
the set 0 to n minus 1 we use the
private key d and obtain m is equal
to c to the power of d in mod n
i will start by creating a new project
and to do JSON I used this jar on the
previous project so let's copy it and
paste it here and we go to properties
and Java build path
and the code will be in 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
and on the client side we will have
three classes a client class with a
main method and a client thread
class and the RSA implementation
code will be in this class
and both clients thread and server
thread classes will extend from thread
and the server thread class will have
three instance variables a server
and a buffered reader and a print
writer and we will initialize those in
the constructor
and on the Server
it will accept the connections on this
port and we have a set of server
threads for the clients that this server
is interacting with
and this is just a get method for the
server threads and this method will
forward the messages from one client to
the other so it calls forward
message on the server thread which we
will add
and here we are starting a new server
thread and adding it to the set of
servers threads and then calling the start
method of that server thread and when
we do that and when the operating system
decides to this start will end up
triggering a call to the run method on
the server thread
so here we are checking if one client is
sending his public key to the other
client then we indicate what that public
key is and forward the message to the
other client but in order to decrypt any
messages we need to figure out the
private key for this public key and to
do that we need to do prime
factorization of n into p and q
so this should do it on the server side
and next we will do the client side
so the client will keep track of this
client username and the n for this
client so p times q and the n for the
other client and the Phi for this client
and the encryption or public exponent
for this and for the other client and
the private key for this client and we will
have get and set methods for those
now going here we will have those two
methods for calculating d the
private key and returning it and for
calculating phi given p and q two
prime numbers
and for generating the keys
so we are making use of
those two methods
for checking if a number is prime or if
two numbers are relatively prime so here
we are handling p and q entry and
calculating and setting n and Phi and
prompting the user for the public
exponent
e
and here we are calculating and setting
the private key and here we are sending
a JSON message indicating what the
public key of this client is to the
other client and those two methods
encrypt and decrypt messages
and those two methods do character to ascii and ascii to character and finally
those three methods
this one does sending messages and this
one handles receiving the public key of
the other client and this one handles
receiving message from the other client
so this should do it for this class
and the client thread will have
those two instance variables and we'll
initialize them in the constructor
and the client here will be interacting
with the server on this port
and it will instantiate a new client
thread and call the start method on it
which will trigger a call to this run
method when the operating system decides
to do so
so here we are handling receiving a
public key and here we are handling
receiving a message
and this handles sending messages
and this method handles the key
generation this should do it for this
class 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
and Alice will pick those two prime
numbers and this public exponent so she
calculates n and Phi of n and her public
key and private key and Bob picks
those two prime numbers and this public
exponent so he calculates n and Phi of
n and his public key and private
one
so Alice sent this message to Bob so she
starts by mapping the characters to
ASCII and then she encrypts it with
Bob's public key and obtains this
ciphertext and send it and Bob
receives it and decryptes it with his
private key and maps the ASCII to character and obtains the original message coming
in from Alice and he in turn sends this
message to Alice so he maps the characters
to ASCII and obtaining this ASCII
message and encrypts it was Alice's
public key and obtains this ciphertext
and sends it and Alice receive the
ciphertext and decrypts it with her
private key and obtain the ASCII
message and maps the ASCII to character
and obtains the original message coming in
from Bob
now Passive Eve can see all the
communications so she now knows Alice
public key and Bob's public key and she
can see the cipher text communications
between Alice and Bob but in order to
decrypts them she needs the private keys
for Alice and for Bob and to obtain
those private keys she needs to do prime
factorization
of n into p and q of both public
keys so we need this p and q and this p
and q
