RSA (RivestβShamirβAdleman) was one of the first public key cryptosystems and is widely used to securely send messages over the internet. The implementation of RSA makes heavy use of modular arithmetic, Euler's theorem, and Euler's totient function that we have explained on the previous pages.
The idea is that we choose n = pq, choose e with gcd(e, π(n)) = 1, and find d with ed ≡ 1 (mod π(n)).
To encipher: P → E(P) ≡ Pe (mod n), where P denotes the plaintext and E denotes encrypt.
To decipher: Q → D(Q) ≡ Qd (mod n), where Q denotes the ciphertext and D denotes decrypt.
To encode we need to know (n, e) and to decode, we need to know (n, d).
Key Generation
First, the receiver chooses two large primes p and q. The product n = pq forms half of the public key. We then use the totient function on n, denoted π(n),
π(n) = π(pq)
= π(p)π(q)
= (p - 1)(q - 1)
= pq - p - q + 1
If we know n and π(n), we can determine p and q. Rearranging the above equation, we get
p + q = pq + 1 - π(n)
This can be written in the form of a polynomial:
x² - (p + q)x + pq = (x - q)(x - p)
Example:
Let n = 187 and π(n) = 160. Determine p and q.
From the question we know, pq = 187 so p + q = 187 + 1 -160 = 28. Rewriting this as a polynomial,
x² - 28x + 187 = 0
Using the quadratic function, we obtain the roots;
x = (28 ± √28² - 4(1)(187)) / 2
= (28 ± √784; - 748) / 2
= (28 ± √36) / 2
= (28 ± 6) / 2
= 17, 11
So p = 11, q = 17.
Finding the Decryption Key
For the purpose of this example weβve chosen very small primes but for encryption to be secure, remember you must choose large primes.
Let
p = 17 and q = 19
And multiply to find n, the modulus
n = pq = 17 x 19 = 323
π(323) = (17 - 1)(19 -1) = 16 x 18 = 288
We then choose e, which must be between 1 and π(n), and coprime to π(n), i.e. gcd(e, π(n)) = 1. Let;
e = 61
The decryption key, d, is such that
ed ≡ 1 mod π(n)
This can be calculated using the
extended Euclidean algorithm.
288 = (61 x 4) + 44
61 = (44 x 1) + 17
44 = (17 x 2) + 10
17 = 10 + 7
10 = 7 + 3
7 = (3 x 2) + 1
1 = 7 - 2(3)
= 7 - 2(10 - 7)
= 3(7) - 2(10)
= 3(17 - 10) - 2(10)
= 3(17) - 5(10)
= 3(17) - 5[44 - 2(17)]
= 13(17) - 5(44)
= 13(61 - 44) - 5(44)
= 13(61) - 18(44)
= 13(61) - 18[288 - 4(61)]
= 85(61) - 18(288)
When compared to the earlier formula de β‘ 1 mod π(n), you can see that the coefficient of e = 61 is 85, the decryption key.
Now we understand how to find the decryption key, letβs try to decrypt a secret message. We will assume that each letter of the alphabet has been given a number, i.e. A = 01, B = 02,..., Z = 26.
Example:
Your public key is (n,e) = (2173, 3) where n = 2173. You receive the message
1865 0183 1955 0308 0034 2130 0530
What does it say and what decryption exponent did you use?
(n, e) = (2173, 3) where n = 2173 = 41*53 (p*q)
P → E(P) ≡ P
e (mod n)
≡ P
3 (mod 2173)
To decode, we need (n, d) = (2173, d) where ed ≡ 1 mod π(n), 3d ≡ 1 (mod π(2173)).
π(n) = (p - 1)(q - 1)
π(2173) = (41 - 1)(53 - 1)
= (40)(52)
= 2080
3d ≡ 1 (mod 2080)
3d ≡ 1 + 2080k
1 = 3d - 2080k
gcd(2080, 3)
2080 = 3(693) + 1
1 = 2080 + (-693)3
Therefore, d = -693
= -693 + 2080 (to make d positive add the modulus to the negative number)
= 1387
D(E(P)) = 1865
1387 0183
1387 1955
1387 0308
1387 0034
1387 2130
1387 0530
1387 (mod 2173)
P = 2015 1815 1420 1509 1903 1512 0424
= T O R O N T O I S C O L D X
= Toronto is cold
Encryption
The formula you need for encryption in RSA is
C = Me (mod n)
Where C is the ciphertext, M is the plaintext message, e is a number coprime to (which must be between one and π(n), non-inclusive) and n is the modulus.
If you are working out an RSA encryption by hand, you will find that you canβt calculate on your calculator x
61, therefore, you have to expand your expression using several laws of indices. As 61 is a prime number, this cannot be simplified into factors. However,
π₯61 = π₯60 * π₯
π₯
60 can be simplified by finding its factors. 60 = 10 x 6 = 5 x 2 x 6 = 5 x 3 x 2 x 2 using
prime factorisation
π₯60 = {[(π₯5)3]2}2
By inserting the expression for π₯
60 into the expression for π₯
61 you have the following:
π₯61 = {[(π₯5)3]2}2 * π₯
NOTES
After each step you should be applying the modulus, as shown in the image below.
π₯
mn = (π₯
m)
n [Solve π₯
m first, then multiply the result by itself n times]
π₯
m+n = π₯
m*π₯
n [Solve π₯
m and π₯
n individually and multiply the results together]
In the image below [(M
5)
4]
3 was possible to calculate and quicker than {[(M
5)
3]
2}
2
Decryption
The decryption formula is
M = Cd (mod n)
The decryption can be worked out using the same method as before, but using d, the decryption key and not e. d is the multiplicative inverse of e mod π(n).
Where d = 85, 85 = [(5 + 2) x 4 x 3] + 1, therefore
π₯85 = [(π₯5 * π₯2)4]3 * π₯
Written by Chelsea and JaneW