CSEC3616Cybersecurity Engineering

    RSA

    RSA key generation, encryption and decryption, worked in full for both of the lecture's own examples, and why the scheme is fast to use but hard to break.

    • Generate an RSA key pair by hand: choose p and q, compute n and phi(n), pick e, and derive d.
    • Encrypt and decrypt a message with RSA, reproducing every intermediate value.
    • Explain why computing C = M^e mod n is fast while recovering M without d is believed infeasible.
    • State the message-size constraint M < n and what happens if it is violated.

    22 min read

    Intuition

    The previous page defined a trapdoor one-way function in the abstract. RSA is the concrete instance built from the RSA problem: multiply two primes to get a modulus nn, and factoring nn back into those primes is believed infeasible once they are large enough. Everything below is one algorithm, worked twice, with every intermediate value shown so you can check your own working against it.

    Mechanism

    Key generation. Four steps, in order:

    1. Choose two distinct primes pp and qq. In practice these are very large; the examples below use small ones so the arithmetic stays checkable by hand.
    2. Compute the modulus n=pqn = pq.
    3. Compute φ(n)=(p1)(q1)\varphi(n) = (p-1)(q-1) — Euler’s totient of nn, using the shortcut that applies when nn is a product of two distinct primes (see primes, Fermat, Euler and fast exponentiation for the general definition).
    4. Choose a public exponent ee with 1<e<φ(n)1 < e < \varphi(n) and gcd(e,φ(n))=1\gcd(e, \varphi(n)) = 1, then compute the private exponent dd as the modular inverse of ee modulo φ(n)\varphi(n): de1(modφ(n))d \equiv e^{-1} \pmod{\varphi(n)}. The method is the extended Euclidean algorithm — see extended Euclid and modular inverses.

    The public key is PU={e,n}PU = \{e, n\}; the private key is PR={d,n}PR = \{d, n\}. pp, qq and φ(n)\varphi(n) can be discarded once dd is computed — they are never needed again, and keeping them around only gives an attacker more to steal.

    Encryption, by anyone holding the public key: C=MemodnC = M^e \bmod n, where the plaintext satisfies M<nM < n.

    Decryption, only by the holder of the private key: M=CdmodnM = C^d \bmod n.

    RSA is a function from Z/nZ\mathbb{Z}/n\mathbb{Z} to Z/nZ\mathbb{Z}/n\mathbb{Z}, and that choice is deliberate. Exponentiation in Z/nZ\mathbb{Z}/n\mathbb{Z} is fast — repeated squaring computes MemodnM^e \bmod n in about log2e\log_2 e multiplications, never ee of them — so encryption and decryption are both cheap once you know the exponent you are raising to. Recovering MM from CC without knowing dd means finding an ee-th root modulo nn instead: the RSA problem, believed infeasible for large enough nn. The entire scheme rests on that gap between “fast if you know the exponent” and “hard if you don’t.”

    RSA — key generation, encryption, decryptionChoose p = 17, q = 11n = p·q= 187φ(n) = (p−1)(q−1)= 160choose e= 7, coprime to φ(n)d ≡ e⁻¹ mod φ(n)= 23Public keyPU = {7, 187}Private keyPR = {23, 187}Plaintext M= 88C = Mᵉ mod n= 88⁷ mod 187 = 11PU = {e, n}Ciphertext C= 11M = Cᵈ mod n= 11²³ mod 187 = 88PR = {d, n}sent as ciphertext

    The public key {e, n} and private key{d, n} both use the same modulus n, butd depends on φ(n), which depends on the two prime factors of n — recovering d from the public key alone means factoring n, believed infeasible for large enough primes. Anyone with the public key can encrypt; only the holder of d can decrypt. Numbers throughout are the lecture's own worked example, not illustrative round figures.

    Worked example

    Answerp=17, q=11, e=7 gives n=187, phi(n)=160, d=23. M=88 encrypts to C=11 and decrypts back to 88.

    This is the lecture’s main worked example.

    1. Choose p=17p = 17, q=11q = 11.
    2. n=pq=17×11=187n = pq = 17 \times 11 = 187.
    3. φ(n)=(p1)(q1)=16×10=160\varphi(n) = (p-1)(q-1) = 16 \times 10 = 160.
    4. Choose e=7e = 7. Check gcd(7,160)=1\gcd(7, 160) = 1 — 7 is prime and does not divide 160, so this holds.
    5. Find d71(mod160)d \equiv 7^{-1} \pmod{160} by the extended Euclidean algorithm on 160160 and 77:
      160=22×7+6160 = 22 \times 7 + 6
      7=1×6+17 = 1 \times 6 + 1
      6=6×1+06 = 6 \times 1 + 0 — remainder 0, so gcd(160,7)=1\gcd(160, 7) = 1, the last non-zero remainder.

    6. Back-substitute to write 11 as a combination of 160160 and 77:
      1=71×61 = 7 - 1 \times 6
      substitute 6=16022×76 = 160 - 22 \times 7:
      1=71×(16022×7)=23×71×1601 = 7 - 1 \times (160 - 22 \times 7) = 23 \times 7 - 1 \times 160

    7. So 23×71(mod160)23 \times 7 \equiv 1 \pmod{160}, which the lecture confirms directly: 23×7=161=(1×160)+123 \times 7 = 161 = (1 \times 160) + 1. Since 023<1600 \le 23 < 160, d=23d = 23.

    8. PU={7,187}PU = \{7, 187\}, PR={23,187}PR = \{23, 187\}.
    9. Encrypt M=88M = 88: compute C=887mod187C = 88^7 \bmod 187 by repeated squaring.
      881mod187=8888^1 \bmod 187 = 88
      882mod187=7744mod187=7788^2 \bmod 187 = 7744 \bmod 187 = 77
      884mod187=772mod187=5929mod187=13288^4 \bmod 187 = 77^2 \bmod 187 = 5929 \bmod 187 = 132
      7=4+2+17 = 4 + 2 + 1, so 887=884×882×881mod18788^7 = 88^4 \times 88^2 \times 88^1 \bmod 187: 132×77mod187=10164mod187=66132 \times 77 \bmod 187 = 10164 \bmod 187 = 66, then 66×88mod187=5808mod187=1166 \times 88 \bmod 187 = 5808 \bmod 187 = 11.
      C=11C = 11.

    10. Decrypt C=11C = 11: compute M=1123mod187M = 11^{23} \bmod 187 by repeated squaring.
      111=1111^1 = 11, 112=12111^2 = 121, 114=1212mod187=14641mod187=5511^4 = 121^2 \bmod 187 = 14641 \bmod 187 = 55, 118=552mod187=3025mod187=3311^8 = 55^2 \bmod 187 = 3025 \bmod 187 = 33, 1116=332mod187=1089mod187=15411^{16} = 33^2 \bmod 187 = 1089 \bmod 187 = 154.
      23=16+4+2+123 = 16 + 4 + 2 + 1, so 1123=1116×114×112×111mod18711^{23} = 11^{16} \times 11^4 \times 11^2 \times 11^1 \bmod 187: 154×55mod187=55154 \times 55 \bmod 187 = 55, then 55×121mod187=11055 \times 121 \bmod 187 = 110, then 110×11mod187=1210mod187=88110 \times 11 \bmod 187 = 1210 \bmod 187 = 88.
      M=88M = 88 — recovered exactly.

    Worked example

    Answerp=7, q=11, e=13 gives n=77, phi(n)=60, d=37. M=3 encrypts to C=38 and decrypts back to 3.

    This is the lecture’s toy example, from the slides.

    1. Choose p=7p = 7, q=11q = 11.
    2. n=pq=7×11=77n = pq = 7 \times 11 = 77.
    3. φ(n)=(p1)(q1)=6×10=60\varphi(n) = (p-1)(q-1) = 6 \times 10 = 60.
    4. Choose e=13e = 13. It is less than 60 and coprime to it.
    5. Find d131(mod60)d \equiv 13^{-1} \pmod{60} by the extended Euclidean algorithm on 6060 and 1313:
      60=4×13+860 = 4 \times 13 + 8
      13=1×8+513 = 1 \times 8 + 5
      8=1×5+38 = 1 \times 5 + 3
      5=1×3+25 = 1 \times 3 + 2
      3=1×2+13 = 1 \times 2 + 1
      2=2×1+02 = 2 \times 1 + 0 — remainder 0, so gcd(60,13)=1\gcd(60, 13) = 1.

    6. Back-substitute from the bottom up:
      1=31×21 = 3 - 1 \times 2
      2=51×3    1=2×31×52 = 5 - 1 \times 3 \implies 1 = 2 \times 3 - 1 \times 5
      3=81×5    1=2×83×53 = 8 - 1 \times 5 \implies 1 = 2 \times 8 - 3 \times 5
      5=131×8    1=5×83×135 = 13 - 1 \times 8 \implies 1 = 5 \times 8 - 3 \times 13
      8=604×13    1=5×6023×138 = 60 - 4 \times 13 \implies 1 = 5 \times 60 - 23 \times 13

    7. So 23×131(mod60)-23 \times 13 \equiv 1 \pmod{60}, and the least non-negative residue of 23-23 modulo 6060 is 6023=3760 - 23 = 37. d=37d = 37, matching the lecture’s stated value directly.

    8. PU={13,77}PU = \{13, 77\}, PR={37,77}PR = \{37, 77\}.
    9. Encrypt M=3M = 3: compute C=313mod77C = 3^{13} \bmod 77.
      31=33^1 = 3, 32=93^2 = 9, 34=81mod77=43^4 = 81 \bmod 77 = 4, 38=42=163^8 = 4^2 = 16.
      13=8+4+113 = 8 + 4 + 1, so 313=38×34×31mod77=16×4×3=1923^{13} = 3^8 \times 3^4 \times 3^1 \bmod 77 = 16 \times 4 \times 3 = 192, and 192mod77=38192 \bmod 77 = 38.
      C=38C = 38.

    10. Decrypt C=38C = 38: compute M=3837mod77M = 38^{37} \bmod 77.
      381=3838^1 = 38, 382=1444mod77=5838^2 = 1444 \bmod 77 = 58, 384=582mod77=3364mod77=5338^4 = 58^2 \bmod 77 = 3364 \bmod 77 = 53, 388=532mod77=2809mod77=3738^8 = 53^2 \bmod 77 = 2809 \bmod 77 = 37, 3816=372mod77=1369mod77=6038^{16} = 37^2 \bmod 77 = 1369 \bmod 77 = 60, 3832=602mod77=3600mod77=5838^{32} = 60^2 \bmod 77 = 3600 \bmod 77 = 58.
      37=32+4+137 = 32 + 4 + 1, so 3837=3832×384×381mod7738^{37} = 38^{32} \times 38^4 \times 38^1 \bmod 77: 58×53mod77=7158 \times 53 \bmod 77 = 71, then 71×38mod77=2698mod77=371 \times 38 \bmod 77 = 2698 \bmod 77 = 3.
      M=3M = 3 — recovered exactly.

    RSA playground

    Pick two small primes and a public exponent, derive the key, then encrypt and decrypt a message with it.

    iqrst

    Exam detail

    Non-numerical messages have to be mapped to numbers before RSA can touch them — the algorithm only encrypts integers in Z/nZ\mathbb{Z}/n\mathbb{Z}. A common convention maps letters to numbers (a1a \to 1, b2,b \to 2, \dots), but any fixed, invertible mapping works; the lecture does not test a specific one.

    Pitfall

    dd is the modular inverse of ee modulo φ(n)\varphi(n), not modulo nn. It is easy to mix these up because both moduli appear on the same page — φ(n)\varphi(n) only exists during key generation and is never used again once dd is derived, while nn appears in both the public and private key and is used in every encryption and decryption.

    Recall

    For p=7, q=11, phi(n)=60. Why must e=13 be checked against phi(n), and not against n=77?

    Because dd is defined as the inverse of ee modulo φ(n)\varphi(n), not modulo nn — a modular inverse only exists when gcd(e,φ(n))=1\gcd(e, \varphi(n)) = 1. Checking ee against nn instead would test the wrong condition entirely; a modulus of φ(n)=60\varphi(n) = 60 is what matters here, not n=77n = 77.