The first step of RSA is to choose two prime numbers, p & q. They should be greater than 2.
p & q should be considered secret values as if someone discovered them, they could derive your private key.
The value n is included in both the public and private key, so it's a value that's publicly known to everyone. We will calculate it now, but we don't need it until we include it in the public & private keys. So, I want you to think of n as - not used until the end.
Fortunately its very easy to calculate n using the formula :
n = p * q
n = p * q
n = 257 * 337
n = 86609
This step uses fancy maths words - but don't be scared.
We're calculating something called Euler's totient (ϕ), but its super easy to do.
ϕ is pronounced as 'phi' so we will also call it that.
ϕ is used to create your private key, so should be considered confidential.
e will become one of the two values that make up the 'public' key.
Pick a random number e, that is 'relatively co-prime' with the Euler's totient in the range [3,ϕ(n)).
'Relatively co-prime' means we have to choose a value for e that doesn't share any factors with phi.[Remind me what a factor is again?].
In other words, e must be a prime number, and if you divide phi by it, phi should not evenly divide into e.
Factors of ϕ : 1,2,3
Factors of e : Please pick an e first - tip : choose the next prime not listed above
In RSA, public keys are comprised of two values e and n. So most private keys look like this - Private Key : (e, n)
Its easy to get all the letters mixed up, so its good to remember that e stands for encrypting, which is usually what you use the public key for.
To come up with the value d (which will be used in the private key) we must solve this equation.
e * d mod ϕ = 1
That means we need to guess a value of d, which when multiplied with e and then divided by ϕ will have a remainder of 1.
e * d mod ϕ = ?
e * d mod ϕ = 1
17 * d mod 86016 = 1
d is used in the private key to decrypt any messages encoded with the public key (e).
Remember that d stands for decrypting, which is usually what you use the private key to do.
Now we're going to use the public/private keys that we just generated to encrypt and decrypt messages. Often the things we end up encrypting with RSA is other keys - symmetric keys which can be used in less computationally intensive block ciphers. For that reason, RSA more often considered a key-exchange protocol than an encryption algorithm.
To encrypt a message 'm', we first convert it to a large number then we calculate the following :
c = me mod n
Where c stands for cipher-text, m stands for the plain-text (the character we are trying to encrypt), and e and n come from the public key.
To encrypt 'A' we use the following formula with the public key (e=17,n=86609).
c = me mod n
c = me mod n (character converted to ASCII value )
c = me mod n
c = m17 mod 86609
c = c
To decrypt a cipher 'c', we apply the same process, but using the private key (d,n).
m = cd mod n
m = cd mod n (c substituted for actual cipher text in number form )
m = cd mod n
m = 12448
m = 12448
To encrypt a message 'm', we first convert it to a large number then we calculate the following :