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 readJump to the quiz ↓

    RSA separates easy exponentiation from a hard inverse

    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.

    Key generation finds an inverse modulo the totient

    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)=(p−1)(q−1)\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): d≡e−1(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 and decryption use the modulus n

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

    Decryption, only by the holder of the private key: M=Cd mod nM = 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 Me mod nM^e \bmod n in about log⁡2e\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.

    Repeated squaring makes both examples checkable by hand

    Try it — worked example

    RSA with p = 17, q = 11, e = 7. Find n, φ(n) and d. Then encrypt M = 88 and decrypt it back.

    Show answer and steps

    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)=(p−1)(q−1)=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 d≡7−1(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=7−1×61 = 7 - 1 \times 6
      substitute 6=160−22×76 = 160 - 22 \times 7:
      1=7−1×(160−22×7)=23×7−1×1601 = 7 - 1 \times (160 - 22 \times 7) = 23 \times 7 - 1 \times 160

    7. So 23×7≡1(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 0≤23<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=887 mod 187C = 88^7 \bmod 187 by repeated squaring.
      881 mod 187=8888^1 \bmod 187 = 88
      882 mod 187=7744 mod 187=7788^2 \bmod 187 = 7744 \bmod 187 = 77
      884 mod 187=772 mod 187=5929 mod 187=13288^4 \bmod 187 = 77^2 \bmod 187 = 5929 \bmod 187 = 132
      7=4+2+17 = 4 + 2 + 1, so 887=884×882×881 mod 18788^7 = 88^4 \times 88^2 \times 88^1 \bmod 187: 132×77 mod 187=10164 mod 187=66132 \times 77 \bmod 187 = 10164 \bmod 187 = 66, then 66×88 mod 187=5808 mod 187=1166 \times 88 \bmod 187 = 5808 \bmod 187 = 11.
      C=11C = 11.

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

    Try it — worked example

    RSA with p = 7, q = 11, e = 13. Find n, φ(n) and d. Then encrypt M = 3 and decrypt it back.

    Show answer and steps

    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)=(p−1)(q−1)=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 d≡13−1(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=3−1×21 = 3 - 1 \times 2
      2=5−1×3  ⟹  1=2×3−1×52 = 5 - 1 \times 3 \implies 1 = 2 \times 3 - 1 \times 5
      3=8−1×5  ⟹  1=2×8−3×53 = 8 - 1 \times 5 \implies 1 = 2 \times 8 - 3 \times 5
      5=13−1×8  ⟹  1=5×8−3×135 = 13 - 1 \times 8 \implies 1 = 5 \times 8 - 3 \times 13
      8=60−4×13  ⟹  1=5×60−23×138 = 60 - 4 \times 13 \implies 1 = 5 \times 60 - 23 \times 13

    7. So −23×13≡1(mod60)-23 \times 13 \equiv 1 \pmod{60}, and the least non-negative residue of −23-23 modulo 6060 is 60−23=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=313 mod 77C = 3^{13} \bmod 77.
      31=33^1 = 3, 32=93^2 = 9, 34=81 mod 77=43^4 = 81 \bmod 77 = 4, 38=42=163^8 = 4^2 = 16.
      13=8+4+113 = 8 + 4 + 1, so 313=38×34×31 mod 77=16×4×3=1923^{13} = 3^8 \times 3^4 \times 3^1 \bmod 77 = 16 \times 4 \times 3 = 192, and 192 mod 77=38192 \bmod 77 = 38.
      C=38C = 38.

    10. Decrypt C=38C = 38: compute M=3837 mod 77M = 38^{37} \bmod 77.
      381=3838^1 = 38, 382=1444 mod 77=5838^2 = 1444 \bmod 77 = 58, 384=582 mod 77=3364 mod 77=5338^4 = 58^2 \bmod 77 = 3364 \bmod 77 = 53, 388=532 mod 77=2809 mod 77=3738^8 = 53^2 \bmod 77 = 2809 \bmod 77 = 37, 3816=372 mod 77=1369 mod 77=6038^{16} = 37^2 \bmod 77 = 1369 \bmod 77 = 60, 3832=602 mod 77=3600 mod 77=5838^{32} = 60^2 \bmod 77 = 3600 \bmod 77 = 58.
      37=32+4+137 = 32 + 4 + 1, so 3837=3832×384×381 mod 7738^{37} = 38^{32} \times 38^4 \times 38^1 \bmod 77: 58×53 mod 77=7158 \times 53 \bmod 77 = 71, then 71×38 mod 77=2698 mod 77=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 (a→1a \to 1, b→2,…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.

    Check yourself