Discrete MathematicsModular Arithmetic and Cryptography6 of 6
Modular Arithmetic and Cryptography · 6
RSA Public Key Cryptography
We want to let anyone send us a locked secret message across a public room or network so that only we can open it, without ever meeting beforehand to share a secret key.
1
Locking a Message on a Clock
To lock a secret number M using a public recipe that anyone can run forward to get a scrambled number C, we need a mathematical operation that an eavesdropper cannot simply run backward. Let's see how raising a message to a power on a modular clock hides the original number.
Locking a Message on a Clock
On a clock of size N, we lock a whole-number message M by raising it to a public lock power e and keeping only the remainder:
Because working modN erases full trips around the clock, the message M must fit on the clock between 0 and N−1 before locking; a longer message is chopped into blocks smaller than N first. Here is how the 33-hour clock locks the message M=7 with lock power e=3.
Text solution
Check that the message fits on the clockSince 7 is between 0 and 32, putting it onto the 33-hour clock does not shrink it before we lock it.
0≤7<33
Raise the message to the lock power and reduce modulo 33Cube 7 and keep the remainder after removing full trips of 33.
C≡73(mod33)=343=10×33+13≡13(mod33)
So the locked message sent across the room is C=13, and because the 10 full trips around the clock are gone, an eavesdropper cannot take a normal cube root of 13 to get 7 back.
As the message M steps around the 33-hour clock one hour at a time, the locked remainder C≡M3(mod33) jumps around the dial and hides how many full turns the M3 steps took to get there.
Raising a message to the 3rd power on a 33-hour clock wraps the count around the circle, so stepping the message by 1 makes the locked remainder jump all over the clock.
2
Decoding with Euler's Theorem
Once we receive the locked number C≡Me(modN), we cannot turn the clock backward with an eth root, so we need a secret decoding power d that turns the clock forward until Cd(modN) lands right back on M.
By Euler's theorem, whenever M shares no factors with N, raising M to the power ϕ(N) resets the clock hand to 1:
Mϕ(N)≡1(modN)
Raising M to any multiple k⋅ϕ(N) still gives 1k=1, so taking one extra step brings back M itself:
Mk⋅ϕ(N)+1=(Mϕ(N))k⋅M1≡1k⋅M=M(modN)
Let's see how this reset cycle tells us which private power d recovers the message.
Unlocking with Euler's Theorem
When we raise the locked message C≡Me(modN) to the private key power d, the exponents multiply together:
Cd≡(Me)d=Me⋅d(modN)
To make the total exponent e⋅d land one step past a multiple of ϕ(N), we choose d to be the modular inverse of e modulo ϕ(N):
d is the inverse of emodϕ(N)e⋅d≡1(modϕ(N)),which requires gcd(e,ϕ(N))=1
Even in the rare case where M shares a prime factor with a two-prime modulus N=p⋅q, raising M to k⋅ϕ(N)+1 still returns M(modN).
As the private power d increases by 1, the total exponent e⋅d moves forward by e=3 at a time around the 20-step ϕ(33) reset clock, and 13d(mod33) lands back on the original message M=7 at d=7, right where 3×7=21 lands one step past 20.
Stepping around the exponent reset wheel jumps the clock hand around until landing one step past the reset mark brings the original message back.
3
The Two-Prime Trapdoor
Because the clock size N and the lock power e are announced to everyone, we need a way to build N so that we know ϕ(N) and can find d, while an eavesdropper who only sees N cannot find ϕ(N).
When N=p⋅q is the product of two distinct primes, Euler's totient function is:
ϕ(p⋅q)=(p−1)(q−1)
Let's see how multiplying two secret primes creates this one-way setup and builds our 33-hour clock keys from start to finish.
The Two-Prime Trapdoor
Here are the steps that build the public and private keys from p=3 and q=11.
Text solution
Multiply two secret primes to make the modulus NMultiplying p=3 and q=11 sets the clock size N=33 for whole-number messages from 0 to 32.
N=p⋅q=3×11=33
Find the exponent reset number ϕ(N)Because 3 and 11 are prime, multiply p−1 by q−1.
ϕ(33)=(3−1)(11−1)=2×10=20
Choose a public lock power e coprime to ϕ(N)Pick an exponent that shares no factors with 20 so that a decoding inverse exists.
gcd(3,20)=1⟹e=3
Find the private decoding power d modulo ϕ(N)Solve for the inverse of 3 modulo 20, then discard the secret values p,q, and ϕ(N).
3⋅d3×7d≡1(mod20)=21=1×20+1=7
We post the public key (N=33,e=3) for everyone, keep the private key d=7 to ourselves, and destroy the scratch paper with p=3,q=11, and ϕ(33)=20. Notice that the lock power e does not have to be prime itself; it only has to satisfy gcd(e,ϕ(N))=1.
4
Finding the Private Key
When ϕ(N) and e are too large to spot d in your head, we need a direct method to solve e⋅d≡1(modϕ(N)) without listing multiples of ϕ(N) plus 1. We use the extended Euclidean algorithm to find the private key d for the primes p=7 and q=11 with public lock power e=17.
Finding the Private Key
ϕ(77)17⋅dd=60,e=17≡1(mod60)=?
Text solution
Find N and ϕ(N) to set up the inverse equationFrom p=7 and q=11, we need the inverse of e=17 modulo ϕ(77)=60.
Nϕ(77)17⋅d=7×11=77=(7−1)(11−1)=6×10=60≡1(mod60)
Run the Euclidean algorithm until the remainder is 1Divide 60 by 17, then divide each divisor by the new remainder at the next step.
60179=3×17+9=1×9+8=1×8+1
Substitute backward to write 1 in terms of 60 and 17Start from the bottom equation and replace each remainder using the line above it, keeping multiples of 60 and 17 grouped.
Reduce modulo 60 and make the private key positiveDrop 2×60 and add ϕ(77)=60 to the negative coefficient −7.
(−7)×17d≡1(mod60)=60−7=53
So the private key is d=53, because 17×53=901=15×60+1 lands one step past a multiple of ϕ(77)=60.
5
Locking and Decoding a Message
Now that we have the public key (N=33,e=3) and the private key d=7, we need a fast way to compute the powers MemodN and CdmodN without the numbers along the way getting huge. Let's lock the secret message M=7 into C=13 and then decode 137(mod33) using repeated squaring.
Locking and Unlocking a Message
Text solution
Lock the message M=7 with the public key (N=33,e=3)Since 7<33, cube 7 and reduce modulo 33.
C≡73=343=10×33+13≡13(mod33)
Break the private power d=7 into powers of 2Write 7=4+2+1 to split 137 into powers we can reach by squaring.
M≡137=134+2+1=134⋅132⋅131(mod33)
Square repeatedly modulo 33Reduce modulo 33 after each squaring step so the numbers stay small.
Multiply the pieces modulo 33 to get M backCombine the remainders for 134,132, and 131, replacing 64=2×33−2 with −2(mod33) to make the multiplication easier.
So raising the locked message 13 to the private power d=7 modulo 33 returns the original secret message M=7.
Putting the entire system together gives the complete recipe for RSA:
RSA Public Key Cryptography1. Secret setup from primes p,q:N=p⋅q(0≤M<N),ϕ(N)=(p−1)(q−1)2. Public key (N,e)and private key d:gcd(e,ϕ(N))=1,e⋅d≡1(modϕ(N))3. Lock and decode (by repeated squaring modN):C≡Me(modN),M≡Cd(modN)
Key concepts
RSA runs on two modular clocks at the same time, and keeping track of which clock you are on decides every step. From the secret primes p=3 and q=11, the modulus N=p⋅q=33 is the message clock: every message block must start inside 0≤M<33 (if you lock M=40 without chopping it first, the clock immediately wraps it to 40≡7(mod33) and decoding will return 7 rather than 40), and both locking C≡Me(mod33) and decoding M≡Cd(mod33) reduce modulo N=33. The totient ϕ(N)=(p−1)(q−1)=20 is the exponent reset clock: you check gcd(e,20)=1 to make sure the lock power e is valid, and you solve e⋅d≡1(mod20) to find the private key d.
To find d, run the extended Euclidean algorithm on ϕ(N) and e (as in 17⋅d≡1(mod60), where −7×17≡1(mod60) gives d=60−7=53), unless ϕ(N) is so small that you can spot a multiple of ϕ(N) plus 1 that is divisible by e (like 3⋅d≡1(mod20), where 20+1=21=3×7 gives d=7 right away). To compute MemodN or CdmodN, break the exponent into powers of 2 and use repeated squaring modulo N (like 137=134⋅132⋅131≡16⋅4⋅13≡7(mod33)), unless the exponent is tiny enough to multiply directly (like 73=343≡13(mod33)) or C is still written in its unexpanded form MemodN, where Euler's theorem gives (Me)d≡M(modN) right away without squaring.
6
Practice
1
You build an RSA clock from the secret primes p=5 and q=11. Find the modulus N, the range of whole-number messages this clock can lock in a single block, and ϕ(N). Then determine which of the candidate powers e∈{3,5,7,9,12} can be used as a public lock power.
After finding N=p⋅q and ϕ(N)=(p−1)(q−1), what condition must gcd(e,ϕ(N)) satisfy for e to have a decoding inverse, and does e itself need to be prime?
Multiply the primes to get the modulus N=5×11=55, which can lock whole-number messages from 0 to 54 (do not include 55, since 55≡0(mod55) wraps back to 0). Because 5 and 11 are prime, the exponent reset number is ϕ(55)=(5−1)(11−1)=4×10=40 (watch out for using N−1=54, which only applies when N itself is prime). A candidate power e is valid when gcd(e,40)=1: here gcd(5,40)=5 and gcd(12,40)=4 share factors with 40, whereas e=3,7,9 have gcd(e,40)=1 (do not rule out e=9 just because 9 is composite, which would wrongly leave only 3 and 7, and do not test gcd(e,55)=1 against N, which would wrongly accept 12). Thus N=55, the message range is 0 to 54,ϕ(55)=40, and the valid lock powers are e=3,7,9.
2
Using the secret primes p=5 and q=13, you set up an RSA modulus N=65 and choose the public lock power e=11. Use the extended Euclidean algorithm to find the positive private key d.
Solving modulo N=65 instead of ϕ(65)=48 is a common mistake that leads to d=6.Add ϕ(65)=48 to make the exponent positive (adding N=65 gives the wrong answer 52, and dropping the minus sign gives 13): d=48−13=35.
First find the exponent reset number ϕ(65)=(5−1)(13−1), then run the Euclidean algorithm on ϕ(65) and 11 to solve 11⋅d≡1(modϕ(65)).
First compute ϕ(65)=(5−1)(13−1)=4×12=48, so we need to solve 11⋅d≡1(mod48) (solving modulo N=65 instead of ϕ(65)=48 is a common mistake that leads to d=6). Running the Euclidean algorithm gives:
Modulo 48, this says (−13)×11≡1(mod48). Add ϕ(65)=48 to make the exponent positive (adding N=65 gives the wrong answer 52, and dropping the minus sign gives 13): d=48−13=35.
3
A public key is posted as N=55 and e=7. Lock the secret message M=8 by computing C≡87(mod55) using repeated squaring.
Be careful to square the remainder 92=81 rather than doubling 9×2=18, which would lead to 31.Multiplying the pieces for 4+2+1 modulo 55, don't forget the 81 factor, which would leave 26×9≡14.
Break the exponent 7 into powers of 2, then square 8 repeatedly modulo 55 to find 82mod55 and 84mod55.
Write 7=4+2+1, so 87=84⋅82⋅81. Squaring repeatedly modulo 55 gives:
(Be careful to square the remainder 92=81 rather than doubling 9×2=18, which would lead to 31.) Multiplying the pieces for 4+2+1 modulo 55 (don't forget the 81 factor, which would leave 26×9≡14) gives:
C≡26×9×8=26×72≡26×17=442=8×55+2≡2(mod55)
So C=2.
4
For the RSA clock with p=3 and q=17, the public key is N=51,e=3 and the private key is d=11. You receive the locked message C=10. Decode it by computing M≡1011(mod51) using repeated squaring.
Adding 7×51=357 converts −320 to a positive remainder (dropping the negative sign and taking +320mod51 is a common mistake that gives 14 instead of 51−14=37).
Break the private exponent 11 into powers of 2, and use a negative remainder when squaring 102 modulo 51 to keep the numbers small.
Write 11=8+2+1, so 1011=108⋅102⋅101 (watch out for including 104, which is not part of 11=8+2+1). Squaring modulo 51 gives:
Multiplying the pieces for 8+2+1 modulo 51 gives 1011≡16×(−2)×10=−320(mod51). Adding 7×51=357 converts −320 to a positive remainder (dropping the negative sign and taking +320mod51 is a common mistake that gives 14 instead of 51−14=37): −320+357=37, so M=37.
5
An eavesdropper intercepts a public key N=77,e=7 and a locked message C=2. Because N=77 is small enough to factor by hand, break the lock: factor N to find ϕ(77), find the private key d, and decode C=2 to recover the original message M.
Factor 77=p⋅q to find ϕ(77), solve 7⋅d≡1(modϕ(77)) for d, and then compute 2dmod77 by repeated squaring.
Factoring gives 77=7×11, so ϕ(77)=(7−1)(11−1)=60. To solve 7⋅d≡1(mod60), the Euclidean algorithm gives 60=8×7+4,7=1×4+3, and 4=1×3+1. Back-substituting gives:
1=4−1×(7−1×4)=2×(60−8×7)−1×7=2×60−17×7
So (−17)×7≡1(mod60), and adding 60 gives d=60−17=43 (forgetting to shift −17 modulo 60 and using d=17 gives the wrong message 18). Now decode C=2 by computing 243mod77 (be sure to switch back to modulo N=77, not 60), using 43=32+8+2+1:
Multiplying the pieces for 32+8+2+1 gives 243≡4×25×4×2=100×8≡23×8=184=2×77+30≡30(mod77), so M=30.
6
Using the public key N=33,e=3 and private key d=7, a sender forgets to chop a large message into blocks smaller than 33 and locks M=104 directly as C≡1043(mod33). Without multiplying out 1043, find the number the receiver gets upon decoding C with C7(mod33).
Expecting C7(mod33) to give back 104 does not work, because the 3 full turns of 33 are erased the moment we work mod33.
First reduce 104 modulo 33 to see what number actually sits on the 33-hour clock before the lock power is applied.
Because 104=3×33+5≡5(mod33), wrapping 104 onto the 33-hour clock immediately reduces it to 5, so C≡1043≡53(mod33) (expecting C7(mod33) to give back 104 does not work, because the 3 full turns of 33 are erased the moment we work mod33). Decoding with d=7 and applying Euler's theorem with ϕ(33)=20 gives C7≡(53)7=521=520⋅51≡1⋅5=5(mod33), so the receiver gets 5.