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\msg{M} using a public recipe that anyone can run forward to get a scrambled number C\locked{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
you\text{you}
me\text{me}

On a clock of size NN, we lock a whole-number message M\msg{M} by raising it to a public lock power e\lockpow{e} and keeping only the remainder:

C⏟lockedmessage≡Me⏟message tolock power(modN⏟clock size),0≤M<N\displaystyle \begin{gathered}\underbrace{\locked{C}}_{\begin{gathered}\scriptstyle\text{locked} \\[-4pt] \scriptstyle\text{message}\end{gathered}} \equiv \underbrace{\msg{M}^{\lockpow{e}}}_{\begin{gathered}\scriptstyle\text{message to} \\[-4pt] \scriptstyle\text{lock power}\end{gathered}} \pmod{\underbrace{N}_{\text{clock size}}}, \\ 0 \le \msg{M} < N\end{gathered}

Because working  mod N\bmod N erases full trips around the clock, the message M\msg{M} must fit on the clock between 00 and N−1N - 1 before locking; a longer message is chopped into blocks smaller than NN first. Here is how the 33-hour clock locks the message M=7\msg{M} = \msg{7} with lock power e=3\lockpow{e} = \lockpow{3}.

Text solution
Check that the message fits on the clockSince 7\msg{7} is between 00 and 3232, putting it onto the 33-hour clock does not shrink it before we lock it.
0\displaystyle 0 ≤7<33\displaystyle \le \msg{7} < 33
Raise the message to the lock power and reduce modulo 3333Cube 7\msg{7} and keep the remainder after removing full trips of 3333.
C≡73(mod33)=343=10×33+13≡13(mod33)\displaystyle \begin{aligned} \locked{C} &\equiv \msg{7}^{\lockpow{3}} \pmod{33} \\ &= 343 = \laps{10 \times 33} + \locked{13} \\ &\equiv \locked{13} \pmod{33} \end{aligned}

So the locked message sent across the room is C=13\locked{C} = \locked{13}, and because the 10\laps{10} full trips around the clock are gone, an eavesdropper cannot take a normal cube root of 13\locked{13} to get 7\msg{7} back.

As the message M\msg{M} steps around the 33-hour clock one hour at a time, the locked remainder C≡M3(mod33)\locked{C} \equiv \msg{M}^{\lockpow{3}} \pmod{33} jumps around the dial and hides how many full turns the M3\msg{M}^{\lockpow{3}} 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)\locked{C} \equiv \msg{M}^{\lockpow{e}} \pmod N, we cannot turn the clock backward with an e\lockpow{e}th root, so we need a secret decoding power d\keypow{d} that turns the clock forward until Cd(modN)\locked{C}^{\keypow{d}} \pmod N lands right back on M\msg{M}.

By Euler's theorem, whenever M\msg{M} shares no factors with NN, raising M\msg{M} to the power ϕ(N)\reset{\phi(N)} resets the clock hand to 11:

Mϕ(N)≡1(modN)\displaystyle \msg{M}^{\reset{\phi(N)}} \equiv 1 \pmod N

Raising M\msg{M} to any multiple k⋅ϕ(N)\reset{k \cdot \phi(N)} still gives 1k=11^k = 1, so taking one extra step brings back M\msg{M} itself:

Mk⋅ϕ(N)+1=(Mϕ(N))k⋅M1≡1k⋅M=M(modN)\displaystyle \begin{aligned}\msg{M}^{\reset{k \cdot \phi(N)} + 1} &= \left(\msg{M}^{\reset{\phi(N)}}\right)^k \cdot \msg{M}^1 \\ &\equiv 1^k \cdot \msg{M} = \msg{M} \pmod N\end{aligned}

Let's see how this reset cycle tells us which private power d\keypow{d} recovers the message.

Unlocking with Euler's Theorem
How do I unlock it?\text{How do I unlock it?}
M3 mod 33\htmlClass{lc-blue}{M}^{\htmlClass{lc-violet}{3}} \bmod 33
00
13\htmlClass{lc-orange}{13}

When we raise the locked message C≡Me(modN)\locked{C} \equiv \msg{M}^{\lockpow{e}} \pmod N to the private key power d\keypow{d}, the exponents multiply together:

Cd≡(Me)d\displaystyle \locked{C}^{\keypow{d}} \equiv (\msg{M}^{\lockpow{e}})^{\keypow{d}} =Me⋅d(modN)\displaystyle = \msg{M}^{\lockpow{e} \cdot \keypow{d}} \pmod N

To make the total exponent e⋅d\lockpow{e} \cdot \keypow{d} land one step past a multiple of ϕ(N)\reset{\phi(N)}, we choose d\keypow{d} to be the modular inverse of e\lockpow{e} modulo ϕ(N)\reset{\phi(N)}:

e⋅d≡1(modϕ(N))⏟d is the inverse of e mod ϕ(N),which requires gcd⁡(e,ϕ(N))=1\displaystyle \begin{gathered}\underbrace{\lockpow{e} \cdot \keypow{d} \equiv 1 \pmod{\reset{\phi(N)}}}_{\keypow{d} \text{ is the inverse of } \lockpow{e} \bmod \reset{\phi(N)}}, \\ \text{which requires } \gcd(\lockpow{e}, \reset{\phi(N)}) = 1\end{gathered}

Even in the rare case where M\msg{M} shares a prime factor with a two-prime modulus N=p⋅qN = p \cdot q, raising M\msg{M} to k⋅ϕ(N)+1\reset{k \cdot \phi(N)} + 1 still returns M(modN)\msg{M} \pmod N.

As the private power d\keypow{d} increases by 11, the total exponent e⋅d\lockpow{e} \cdot \keypow{d} moves forward by e=3\lockpow{e} = \lockpow{3} at a time around the 20\reset{20}-step ϕ(33)\reset{\phi(33)} reset clock, and 13d(mod33)\locked{13}^{\keypow{d}} \pmod{33} lands back on the original message M=7\msg{M} = \msg{7} at d=7\keypow{d} = \keypow{7}, right where 3×7=21\lockpow{3} \times \keypow{7} = 21 lands one step past 20\reset{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 NN and the lock power e\lockpow{e} are announced to everyone, we need a way to build NN so that we know ϕ(N)\reset{\phi(N)} and can find d\keypow{d}, while an eavesdropper who only sees NN cannot find ϕ(N)\reset{\phi(N)}.

secret\htmlClass{lc-mute}{\text{secret}}
pp
=3,q=11{} = 3, \quad q = 11
multiply\text{multiply}
public\htmlClass{lc-mute}{\text{public}}
N=33N = 33
e=3\htmlClass{lc-violet}{e} = \htmlClass{lc-violet}{3}
factor\text{factor}
hopeless for huge N\htmlClass{lc-mute}{\text{hopeless for huge } N}
ϕ(33)\htmlClass{lc-green}{\phi(33)}
=(3−1)(11−1)=20{} = (3 - 1)(11 - 1) = \htmlClass{lc-green}{20}
d\htmlClass{lc-pink}{d}
=7{} = \htmlClass{lc-pink}{7}

When N=p⋅qN = p \cdot q is the product of two distinct primes, Euler's totient function is:

ϕ(p⋅q)\displaystyle \reset{\phi(p \cdot q)} =(p−1)(q−1)\displaystyle = (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
NN
e\htmlClass{lc-violet}{e}
ϕ(N)?\htmlClass{lc-green}{\phi(N)}\enspace \htmlClass{lc-mute}{?}
d?\htmlClass{lc-pink}{d}\enspace \htmlClass{lc-mute}{?}

Here are the steps that build the public and private keys from p=3p = 3 and q=11q = 11.

Text solution
Multiply two secret primes to make the modulus NNMultiplying p=3p = 3 and q=11q = 11 sets the clock size N=33N = 33 for whole-number messages from 00 to 3232.
N\displaystyle N =p⋅q\displaystyle = p \cdot q =3×11\displaystyle = 3 \times 11 =33\displaystyle = 33
Find the exponent reset number ϕ(N)\reset{\phi(N)}Because 33 and 1111 are prime, multiply p−1p - 1 by q−1q - 1.
ϕ(33)\displaystyle \reset{\phi(33)} =(3−1)(11−1)\displaystyle = (3 - 1)(11 - 1) =2×10\displaystyle = 2 \times 10 =20\displaystyle = \reset{20}
Choose a public lock power e\lockpow{e} coprime to ϕ(N)\reset{\phi(N)}Pick an exponent that shares no factors with 20\reset{20} so that a decoding inverse exists.
gcd⁡(3,20)\displaystyle \gcd(\lockpow{3}, \reset{20}) =1  ⟹  e\displaystyle = 1 \implies \lockpow{e} =3\displaystyle = \lockpow{3}
Find the private decoding power d\keypow{d} modulo ϕ(N)\reset{\phi(N)}Solve for the inverse of 3\lockpow{3} modulo 20\reset{20}, then discard the secret values p,q,p, q, and ϕ(N)\reset{\phi(N)}.
3⋅d≡1(mod20)3×7=21=1×20+1d=7\displaystyle \begin{aligned} \lockpow{3} \cdot \keypow{d} &\equiv 1 \pmod{\reset{20}} \\ \lockpow{3} \times \keypow{7} &= 21 = \reset{1 \times 20} + 1 \\ \keypow{d} &= \keypow{7} \end{aligned}

We post the public key (N=33,e=3)(N = 33, \lockpow{e} = \lockpow{3}) for everyone, keep the private key d=7\keypow{d} = \keypow{7} to ourselves, and destroy the scratch paper with p=3,q=11,p = 3, q = 11, and ϕ(33)=20\reset{\phi(33)} = \reset{20}. Notice that the lock power e\lockpow{e} does not have to be prime itself; it only has to satisfy gcd⁡(e,ϕ(N))=1\gcd(\lockpow{e}, \reset{\phi(N)}) = 1.

4

Finding the Private Key

When ϕ(N)\reset{\phi(N)} and e\lockpow{e} are too large to spot d\keypow{d} in your head, we need a direct method to solve e⋅d≡1(modϕ(N))\lockpow{e} \cdot \keypow{d} \equiv 1 \pmod{\reset{\phi(N)}} without listing multiples of ϕ(N)\reset{\phi(N)} plus 11. We use the extended Euclidean algorithm to find the private key d\keypow{d} for the primes p=7p = 7 and q=11q = 11 with public lock power e=17\lockpow{e} = \lockpow{17}.

Finding the Private Key
ϕ(77)=60,e=1717⋅d≡1(mod60)d=  ?\displaystyle \begin{aligned} \reset{\phi(77)} &= \reset{60}, \qquad \lockpow{e} = \lockpow{17} \\ \lockpow{17} \cdot \keypow{d} &\equiv 1 \pmod{\reset{60}} \\ \keypow{d} &= \;? \end{aligned}
Text solution
Find NN and ϕ(N)\reset{\phi(N)} to set up the inverse equationFrom p=7p = 7 and q=11q = 11, we need the inverse of e=17\lockpow{e} = \lockpow{17} modulo ϕ(77)=60\reset{\phi(77)} = \reset{60}.
N=7×11=77ϕ(77)=(7−1)(11−1)=6×10=6017⋅d≡1(mod60)\displaystyle \begin{aligned} N &= 7 \times 11 = 77 \\ \reset{\phi(77)} &= (7 - 1)(11 - 1) \\ &= 6 \times 10 = \reset{60} \\ \lockpow{17} \cdot \keypow{d} &\equiv 1 \pmod{\reset{60}} \end{aligned}
Run the Euclidean algorithm until the remainder is 11Divide 60\reset{60} by 17\lockpow{17}, then divide each divisor by the new remainder at the next step.
60=3×17+917=1×9+89=1×8+1\displaystyle \begin{aligned} \reset{60} &= 3 \times \lockpow{17} + \rem{9} \\ \lockpow{17} &= 1 \times \rem{9} + \rem{8} \\ \rem{9} &= 1 \times \rem{8} + 1 \end{aligned}
Substitute backward to write 11 in terms of 60\reset{60} and 17\lockpow{17}Start from the bottom equation and replace each remainder using the line above it, keeping multiples of 60\reset{60} and 17\lockpow{17} grouped.
1=9−1×8=9−1×(17−1×9)=2×9−1×17=2×(60−3×17)−1×17=2×60−7×17\displaystyle \begin{aligned} 1 &= \rem{9} - 1 \times \rem{8} \\ &= \rem{9} - 1 \times (\lockpow{17} - 1 \times \rem{9}) \\ &= 2 \times \rem{9} - 1 \times \lockpow{17} \\ &= 2 \times (\reset{60} - 3 \times \lockpow{17}) - 1 \times \lockpow{17} \\ &= 2 \times \reset{60} - 7 \times \lockpow{17} \end{aligned}
Reduce modulo 60\reset{60} and make the private key positiveDrop 2×602 \times \reset{60} and add ϕ(77)=60\reset{\phi(77)} = \reset{60} to the negative coefficient −7\keypow{-7}.
(−7)×17≡1(mod60)d=60−7=53\displaystyle \begin{aligned} (\keypow{-7}) \times \lockpow{17} &\equiv 1 \pmod{\reset{60}} \\ \keypow{d} &= \reset{60} - \keypow{7} = \keypow{53} \end{aligned}

So the private key is d=53\keypow{d} = \keypow{53}, because 17×53=901=15×60+1\lockpow{17} \times \keypow{53} = 901 = \reset{15 \times 60} + 1 lands one step past a multiple of ϕ(77)=60\reset{\phi(77)} = \reset{60}.

5

Locking and Decoding a Message

Now that we have the public key (N=33,e=3)(N = 33, \lockpow{e} = \lockpow{3}) and the private key d=7\keypow{d} = \keypow{7}, we need a fast way to compute the powers Me mod N\msg{M}^{\lockpow{e}} \bmod N and Cd mod N\locked{C}^{\keypow{d}} \bmod N without the numbers along the way getting huge. Let's lock the secret message M=7\msg{M} = \msg{7} into C=13\locked{C} = \locked{13} and then decode 137(mod33)\locked{13}^{\keypow{7}} \pmod{33} using repeated squaring.

Locking and Unlocking a Message
7\htmlClass{lc-blue}{7}
N=33N = 33
e=3\htmlClass{lc-violet}{e} = \htmlClass{lc-violet}{3}
C\htmlClass{lc-orange}{C}
d=7\htmlClass{lc-pink}{d} = \htmlClass{lc-pink}{7}
??
Text solution
Lock the message M=7\msg{M} = \msg{7} with the public key (N=33,e=3)(N = 33, \lockpow{e} = \lockpow{3})Since 7<33\msg{7} < 33, cube 7\msg{7} and reduce modulo 3333.
C≡73=343=10×33+13≡13(mod33)\displaystyle \begin{aligned}\locked{C} &\equiv \msg{7}^{\lockpow{3}} = 343 \\ &= \laps{10 \times 33} + \locked{13} \\ &\equiv \locked{13} \pmod{33}\end{aligned}
Break the private power d=7\keypow{d} = \keypow{7} into powers of 22Write 7=4+2+1\keypow{7} = \keypow{4} + \keypow{2} + \keypow{1} to split 137\locked{13}^{\keypow{7}} into powers we can reach by squaring.
M≡137=134+2+1=134⋅132⋅131(mod33)\displaystyle \begin{aligned}\msg{M} &\equiv \locked{13}^{\keypow{7}} = \locked{13}^{\keypow{4} + \keypow{2} + \keypow{1}} \\ &= \locked{13}^{\keypow{4}} \cdot \locked{13}^{\keypow{2}} \cdot \locked{13}^{\keypow{1}} \pmod{33}\end{aligned}
Square repeatedly modulo 3333Reduce modulo 3333 after each squaring step so the numbers stay small.
131≡13(mod33)132=169=5×33+4≡4(mod33)134=(132)2≡42=16(mod33)\displaystyle \begin{aligned} \locked{13}^{\keypow{1}} &\equiv 13 \pmod{33} \\ \locked{13}^{\keypow{2}} &= 169 = 5 \times 33 + 4 \\ &\equiv 4 \pmod{33} \\ \locked{13}^{\keypow{4}} &= (\locked{13}^{\keypow{2}})^2 \\ &\equiv 4^2 = 16 \pmod{33} \end{aligned}
Multiply the pieces modulo 3333 to get M\msg{M} backCombine the remainders for 134,132,\locked{13}^{\keypow{4}}, \locked{13}^{\keypow{2}}, and 131\locked{13}^{\keypow{1}}, replacing 64=2×33−264 = 2 \times 33 - 2 with −2(mod33)-2 \pmod{33} to make the multiplication easier.
M≡16⋅4⋅13(mod33)=64⋅13≡(−2)⋅13(mod33)=−26≡−26+33=7(mod33)\displaystyle \begin{aligned} \msg{M} &\equiv 16 \cdot 4 \cdot 13 \pmod{33} \\ &= 64 \cdot 13 \\ &\equiv (-2) \cdot 13 \pmod{33} \\ &= -26 \\ &\equiv -26 + 33 = \msg{7} \pmod{33} \end{aligned}

So raising the locked message 13\locked{13} to the private power d=7\keypow{d} = \keypow{7} modulo 3333 returns the original secret message M=7\msg{M} = \msg{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  mod N):C≡Me(modN),M≡Cd(modN)\displaystyle \begin{array}{l} \textbf{RSA Public Key Cryptography} \\[4pt] \text{1. Secret setup from primes } p, q: \\ \quad N = p \cdot q \;\; (0 \le \msg{M} < N), \\ \quad \reset{\phi(N)} = (p - 1)(q - 1) \\[4pt] \text{2. Public key } (N, \lockpow{e}) \\ \text{and private key } \keypow{d}: \\ \quad \gcd(\lockpow{e}, \reset{\phi(N)}) = 1, \\ \quad \lockpow{e} \cdot \keypow{d} \equiv 1 \pmod{\reset{\phi(N)}} \\[4pt] \text{3. Lock and decode } \\ (\text{by repeated squaring } \bmod N): \\ \quad \locked{C} \equiv \msg{M}^{\lockpow{e}} \pmod N, \\ \quad \msg{M} \equiv \locked{C}^{\keypow{d}} \pmod N \end{array}

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=3p = 3 and q=11q = 11, the modulus N=p⋅q=33N = p \cdot q = 33 is the message clock: every message block must start inside 0≤M<330 \le \msg{M} < 33 (if you lock M=40\msg{M} = \msg{40} without chopping it first, the clock immediately wraps it to 40≡7(mod33)\msg{40} \equiv \msg{7} \pmod{33} and decoding will return 7\msg{7} rather than 40\msg{40}), and both locking C≡Me(mod33)\locked{C} \equiv \msg{M}^{\lockpow{e}} \pmod{33} and decoding M≡Cd(mod33)\msg{M} \equiv \locked{C}^{\keypow{d}} \pmod{33} reduce modulo N=33N = 33. The totient ϕ(N)=(p−1)(q−1)=20\reset{\phi(N)} = (p - 1)(q - 1) = \reset{20} is the exponent reset clock: you check gcd⁡(e,20)=1\gcd(\lockpow{e}, \reset{20}) = 1 to make sure the lock power e\lockpow{e} is valid, and you solve e⋅d≡1(mod20)\lockpow{e} \cdot \keypow{d} \equiv 1 \pmod{\reset{20}} to find the private key d\keypow{d}.

To find d\keypow{d}, run the extended Euclidean algorithm on ϕ(N)\reset{\phi(N)} and e\lockpow{e} (as in 17⋅d≡1(mod60)\lockpow{17} \cdot \keypow{d} \equiv 1 \pmod{\reset{60}}, where −7×17≡1(mod60)\keypow{-7} \times \lockpow{17} \equiv 1 \pmod{\reset{60}} gives d=60−7=53\keypow{d} = \reset{60} - \keypow{7} = \keypow{53}), unless ϕ(N)\reset{\phi(N)} is so small that you can spot a multiple of ϕ(N)\reset{\phi(N)} plus 11 that is divisible by e\lockpow{e} (like 3⋅d≡1(mod20)\lockpow{3} \cdot \keypow{d} \equiv 1 \pmod{\reset{20}}, where 20+1=21=3×7\reset{20} + 1 = 21 = \lockpow{3} \times \keypow{7} gives d=7\keypow{d} = \keypow{7} right away). To compute Me mod N\msg{M}^{\lockpow{e}} \bmod N or Cd mod N\locked{C}^{\keypow{d}} \bmod N, break the exponent into powers of 22 and use repeated squaring modulo NN (like 137=134⋅132⋅131\locked{13}^{\keypow{7}} = \locked{13}^{\keypow{4}} \cdot \locked{13}^{\keypow{2}} \cdot \locked{13}^{\keypow{1}} ≡16⋅4⋅13≡7(mod33)\equiv 16 \cdot 4 \cdot 13 \equiv \msg{7} \pmod{33}), unless the exponent is tiny enough to multiply directly (like 73=343≡13(mod33)\msg{7}^{\lockpow{3}} = 343 \equiv \locked{13} \pmod{33}) or C\locked{C} is still written in its unexpanded form Me mod N\msg{M}^{\lockpow{e}} \bmod N, where Euler's theorem gives (Me)d≡M(modN)(\msg{M}^{\lockpow{e}})^{\keypow{d}} \equiv \msg{M} \pmod N right away without squaring.

6

Practice

1

You build an RSA clock from the secret primes p=5p = 5 and q=11q = 11. Find the modulus NN, the range of whole-number messages this clock can lock in a single block, and ϕ(N)\reset{\phi(N)}. Then determine which of the candidate powers e∈{3,5,7,9,12}\lockpow{e} \in \{\lockpow{3}, \lockpow{5}, \lockpow{7}, \lockpow{9}, \lockpow{12}\} can be used as a public lock power.

After finding N=p⋅qN = p \cdot q and ϕ(N)=(p−1)(q−1)\reset{\phi(N)} = (p - 1)(q - 1), what condition must gcd⁡(e,ϕ(N))\gcd(\lockpow{e}, \reset{\phi(N)}) satisfy for e\lockpow{e} to have a decoding inverse, and does e\lockpow{e} itself need to be prime?

Multiply the primes to get the modulus N=5×11=55N = 5 \times 11 = 55, which can lock whole-number messages from 00 to 5454 (do not include 5555, since 55≡0(mod55)55 \equiv 0 \pmod{55} wraps back to 00). Because 55 and 1111 are prime, the exponent reset number is ϕ(55)=(5−1)(11−1)=4×10=40\reset{\phi(55)} = (5 - 1)(11 - 1) = 4 \times 10 = \reset{40} (watch out for using N−1=54N - 1 = \fails{54}, which only applies when NN itself is prime). A candidate power e\lockpow{e} is valid when gcd⁡(e,40)=1\gcd(\lockpow{e}, \reset{40}) = 1: here gcd⁡(5,40)=5\gcd(\lockpow{5}, \reset{40}) = \fails{5} and gcd⁡(12,40)=4\gcd(\lockpow{12}, \reset{40}) = \fails{4} share factors with 40\reset{40}, whereas e=3,7,9\lockpow{e} = \lockpow{3}, \lockpow{7}, \lockpow{9} have gcd⁡(e,40)=1\gcd(\lockpow{e}, \reset{40}) = 1 (do not rule out e=9\lockpow{e} = \lockpow{9} just because 9\lockpow{9} is composite, which would wrongly leave only 3\lockpow{3} and 7\lockpow{7}, and do not test gcd⁡(e,55)=1\gcd(\lockpow{e}, 55) = 1 against NN, which would wrongly accept 12\fails{12}). Thus N=55N = 55, the message range is 00 to 5454, ϕ(55)=40\reset{\phi(55)} = \reset{40}, and the valid lock powers are e=3,7,9\lockpow{e} = \lockpow{3}, \lockpow{7}, \lockpow{9}.
2

Using the secret primes p=5p = 5 and q=13q = 13, you set up an RSA modulus N=65N = 65 and choose the public lock power e=11\lockpow{e} = \lockpow{11}. Use the extended Euclidean algorithm to find the positive private key d\keypow{d}.

First find the exponent reset number ϕ(65)=(5−1)(13−1)\reset{\phi(65)} = (5 - 1)(13 - 1), then run the Euclidean algorithm on ϕ(65)\reset{\phi(65)} and 11\lockpow{11} to solve 11⋅d≡1(modϕ(65))\lockpow{11} \cdot \keypow{d} \equiv 1 \pmod{\reset{\phi(65)}}.

First compute ϕ(65)=(5−1)(13−1)=4×12=48\reset{\phi(65)} = (5 - 1)(13 - 1) = 4 \times 12 = \reset{48}, so we need to solve 11⋅d≡1(mod48)\lockpow{11} \cdot \keypow{d} \equiv 1 \pmod{\reset{48}} (solving modulo N=65N = 65 instead of ϕ(65)=48\reset{\phi(65)} = \reset{48} is a common mistake that leads to d=6\keypow{d} = \fails{6}). Running the Euclidean algorithm gives:
48=4×11+411=2×4+34=1×3+1\displaystyle \begin{aligned} \reset{48} &= 4 \times \lockpow{11} + \rem{4} \\ \lockpow{11} &= 2 \times \rem{4} + \rem{3} \\ \rem{4} &= 1 \times \rem{3} + 1 \end{aligned}
Substituting backward from 11 gives:
1=4−1×3=4−1×(11−2×4)=3×4−1×11=3×(48−4×11)−1×11=3×48−13×11\displaystyle \begin{aligned} 1 &= \rem{4} - 1 \times \rem{3} \\ &= \rem{4} - 1 \times (\lockpow{11} - 2 \times \rem{4}) \\ &= 3 \times \rem{4} - 1 \times \lockpow{11} \\ &= 3 \times (\reset{48} - 4 \times \lockpow{11}) - 1 \times \lockpow{11} \\ &= 3 \times \reset{48} - 13 \times \lockpow{11} \end{aligned}
Modulo 48\reset{48}, this says (−13)×11≡1(mod48)(\keypow{-13}) \times \lockpow{11} \equiv 1 \pmod{\reset{48}}. Add ϕ(65)=48\reset{\phi(65)} = \reset{48} to make the exponent positive (adding N=65N = 65 gives the wrong answer 52\fails{52}, and dropping the minus sign gives 13\fails{13}): d=48−13=35\keypow{d} = \reset{48} - \keypow{13} = \keypow{35}.
3

A public key is posted as N=55N = 55 and e=7\lockpow{e} = \lockpow{7}. Lock the secret message M=8\msg{M} = \msg{8} by computing C≡87(mod55)\locked{C} \equiv \msg{8}^{\lockpow{7}} \pmod{55} using repeated squaring.

Break the exponent 7\lockpow{7} into powers of 22, then square 8\msg{8} repeatedly modulo 5555 to find 82 mod 55\msg{8}^{\lockpow{2}} \bmod 55 and 84 mod 55\msg{8}^{\lockpow{4}} \bmod 55.

Write 7=4+2+1\lockpow{7} = \lockpow{4} + \lockpow{2} + \lockpow{1}, so 87=84⋅82⋅81\msg{8}^{\lockpow{7}} = \msg{8}^{\lockpow{4}} \cdot \msg{8}^{\lockpow{2}} \cdot \msg{8}^{\lockpow{1}}. Squaring repeatedly modulo 5555 gives:
81≡8(mod55)82=64=1×55+9≡9(mod55)84=(82)2≡92=81=1×55+26≡26(mod55)\displaystyle \begin{aligned} \msg{8}^{\lockpow{1}} &\equiv 8 \pmod{55} \\ \msg{8}^{\lockpow{2}} &= 64 = 1 \times 55 + 9 \equiv 9 \pmod{55} \\ \msg{8}^{\lockpow{4}} &= (\msg{8}^{\lockpow{2}})^2 \equiv 9^2 = 81 = 1 \times 55 + 26 \equiv 26 \pmod{55} \end{aligned}
(Be careful to square the remainder 92=819^2 = 81 rather than doubling 9×2=189 \times 2 = \fails{18}, which would lead to 31\fails{31}.) Multiplying the pieces for 4+2+1\lockpow{4} + \lockpow{2} + \lockpow{1} modulo 5555 (don't forget the 81\msg{8}^{\lockpow{1}} factor, which would leave 26×9≡1426 \times 9 \equiv \fails{14}) gives:
C≡26×9×8=26×72≡26×17=442=8×55+2≡2(mod55)\displaystyle \begin{aligned}\locked{C} &\equiv 26 \times 9 \times 8 = 26 \times 72 \\ &\equiv 26 \times 17 = 442 = 8 \times 55 + 2 \\ &\equiv \locked{2} \pmod{55}\end{aligned}
So C=2\locked{C} = \locked{2}.
4

For the RSA clock with p=3p = 3 and q=17q = 17, the public key is N=51,e=3N = 51, \lockpow{e} = \lockpow{3} and the private key is d=11\keypow{d} = \keypow{11}. You receive the locked message C=10\locked{C} = \locked{10}. Decode it by computing M≡1011(mod51)\msg{M} \equiv \locked{10}^{\keypow{11}} \pmod{51} using repeated squaring.

Break the private exponent 11\keypow{11} into powers of 22, and use a negative remainder when squaring 102\locked{10}^{\keypow{2}} modulo 5151 to keep the numbers small.

Write 11=8+2+1\keypow{11} = \keypow{8} + \keypow{2} + \keypow{1}, so 1011=108⋅102⋅101\locked{10}^{\keypow{11}} = \locked{10}^{\keypow{8}} \cdot \locked{10}^{\keypow{2}} \cdot \locked{10}^{\keypow{1}} (watch out for including 104\locked{10}^4, which is not part of 11=8+2+1\keypow{11} = \keypow{8} + \keypow{2} + \keypow{1}). Squaring modulo 5151 gives:
101≡10(mod51)102=100=2×51−2≡−2(mod51)104=(102)2≡(−2)2=4(mod51)108=(104)2≡42=16(mod51)\displaystyle \begin{aligned} \locked{10}^{\keypow{1}} &\equiv 10 \pmod{51} \\ \locked{10}^{\keypow{2}} &= 100 = 2 \times 51 - 2 \equiv -2 \pmod{51} \\ \locked{10}^4 &= (\locked{10}^{\keypow{2}})^2 \equiv (-2)^2 = 4 \pmod{51} \\ \locked{10}^{\keypow{8}} &= (\locked{10}^4)^2 \equiv 4^2 = 16 \pmod{51} \end{aligned}
Multiplying the pieces for 8+2+1\keypow{8} + \keypow{2} + \keypow{1} modulo 5151 gives 1011≡16×(−2)×10\locked{10}^{\keypow{11}} \equiv 16 \times (-2) \times 10 =−320(mod51)= -320 \pmod{51}. Adding 7×51=3577 \times 51 = 357 converts −320-320 to a positive remainder (dropping the negative sign and taking +320 mod 51+320 \bmod 51 is a common mistake that gives 14\fails{14} instead of 51−14=3751 - 14 = \msg{37}): −320+357=37-320 + 357 = \msg{37}, so M=37\msg{M} = \msg{37}.
5

An eavesdropper intercepts a public key N=77,e=7N = 77, \lockpow{e} = \lockpow{7} and a locked message C=2\locked{C} = \locked{2}. Because N=77N = 77 is small enough to factor by hand, break the lock: factor NN to find ϕ(77)\reset{\phi(77)}, find the private key d\keypow{d}, and decode C=2\locked{C} = \locked{2} to recover the original message M\msg{M}.

Factor 77=p⋅q77 = p \cdot q to find ϕ(77)\reset{\phi(77)}, solve 7⋅d≡1(modϕ(77))\lockpow{7} \cdot \keypow{d} \equiv 1 \pmod{\reset{\phi(77)}} for d\keypow{d}, and then compute 2d mod 77\locked{2}^{\keypow{d}} \bmod 77 by repeated squaring.

Factoring gives 77=7×1177 = 7 \times 11, so ϕ(77)=(7−1)(11−1)=60\reset{\phi(77)} = (7 - 1)(11 - 1) = \reset{60}. To solve 7⋅d≡1(mod60)\lockpow{7} \cdot \keypow{d} \equiv 1 \pmod{\reset{60}}, the Euclidean algorithm gives 60=8×7+4\reset{60} = 8 \times \lockpow{7} + \rem{4}, 7=1×4+3\lockpow{7} = 1 \times \rem{4} + \rem{3}, and 4=1×3+1\rem{4} = 1 \times \rem{3} + 1. Back-substituting gives:
1=4−1×(7−1×4)=2×(60−8×7)−1×7=2×60−17×7\displaystyle \begin{aligned}1 &= \rem{4} - 1 \times (\lockpow{7} - 1 \times \rem{4}) \\ &= 2 \times (\reset{60} - 8 \times \lockpow{7}) - 1 \times \lockpow{7} \\ &= 2 \times \reset{60} - 17 \times \lockpow{7}\end{aligned}
So (−17)×7≡1(mod60)(\keypow{-17}) \times \lockpow{7} \equiv 1 \pmod{\reset{60}}, and adding 60\reset{60} gives d=60−17=43\keypow{d} = \reset{60} - \keypow{17} = \keypow{43} (forgetting to shift −17-17 modulo 60\reset{60} and using d=17\keypow{d} = \fails{17} gives the wrong message 18\fails{18}). Now decode C=2\locked{C} = \locked{2} by computing 243 mod 77\locked{2}^{\keypow{43}} \bmod 77 (be sure to switch back to modulo N=77N = 77, not 60\reset{60}), using 43=32+8+2+1\keypow{43} = \keypow{32} + \keypow{8} + \keypow{2} + \keypow{1}:
21≡2,22≡4,24≡16(mod77)28=256=3×77+25≡25(mod77)216≡252=625=8×77+9≡9(mod77)232≡92=81=1×77+4≡4(mod77)\displaystyle \begin{aligned} \locked{2}^{\keypow{1}} &\equiv 2, \quad \locked{2}^{\keypow{2}} \equiv 4, \quad \locked{2}^4 \equiv 16 \pmod{77} \\ \locked{2}^{\keypow{8}} &= 256 = 3 \times 77 + 25 \equiv 25 \pmod{77} \\ \locked{2}^{16} &\equiv 25^2 = 625 = 8 \times 77 + 9 \equiv 9 \pmod{77} \\ \locked{2}^{\keypow{32}} &\equiv 9^2 = 81 = 1 \times 77 + 4 \equiv 4 \pmod{77} \end{aligned}
Multiplying the pieces for 32+8+2+1\keypow{32} + \keypow{8} + \keypow{2} + \keypow{1} gives 243≡4×25×4×2\locked{2}^{\keypow{43}} \equiv 4 \times 25 \times 4 \times 2 =100×8≡23×8= 100 \times 8 \equiv 23 \times 8 =184=2×77+30= 184 = 2 \times 77 + 30 ≡30(mod77)\equiv \msg{30} \pmod{77}, so M=30\msg{M} = \msg{30}.
6

Using the public key N=33,e=3N = 33, \lockpow{e} = \lockpow{3} and private key d=7\keypow{d} = \keypow{7}, a sender forgets to chop a large message into blocks smaller than 3333 and locks M=104\msg{M} = \msg{104} directly as C≡1043(mod33)\locked{C} \equiv \msg{104}^{\lockpow{3}} \pmod{33}. Without multiplying out 1043\msg{104}^{\lockpow{3}}, find the number the receiver gets upon decoding C\locked{C} with C7(mod33)\locked{C}^{\keypow{7}} \pmod{33}.

First reduce 104\msg{104} modulo 3333 to see what number actually sits on the 33-hour clock before the lock power is applied.

Because 104=3×33+5≡5(mod33)\msg{104} = \laps{3 \times 33} + \msg{5} \equiv \msg{5} \pmod{33}, wrapping 104\msg{104} onto the 33-hour clock immediately reduces it to 5\msg{5}, so C≡1043≡53(mod33)\locked{C} \equiv \msg{104}^{\lockpow{3}} \equiv \msg{5}^{\lockpow{3}} \pmod{33} (expecting C7(mod33)\locked{C}^{\keypow{7}} \pmod{33} to give back 104\msg{104} does not work, because the 3\laps{3} full turns of 3333 are erased the moment we work  mod 33\bmod 33). Decoding with d=7\keypow{d} = \keypow{7} and applying Euler's theorem with ϕ(33)=20\reset{\phi(33)} = \reset{20} gives C7≡(53)7=521\locked{C}^{\keypow{7}} \equiv (\msg{5}^{\lockpow{3}})^{\keypow{7}} = \msg{5}^{21} =520⋅51≡1⋅5=5(mod33)= \msg{5}^{\reset{20}} \cdot \msg{5}^1 \equiv 1 \cdot \msg{5} = \msg{5} \pmod{33}, so the receiver gets 5\msg{5}.