RSA is the first practical public-key cipher: its security rests on the difficulty of factoring n = pq. Key generation, encryption C = Mᵉ mod n, decryption M = Cᵈ mod n and hash-then-sign signatures are classic PSC numericals with small primes.
High-yield 65 minSyllabus: “RSA algorithm”
Mark as
The idea in one minute
RSA is a mathematical padlock. Multiplying two big primes is easy; splitting the product back into its primes is astonishingly hard. The public key (e, n) lets anyone lock a message by raising it to the power e and wrapping around at n. Only someone who knows the primes can work out the matching exponent d that undoes the lock. Run it the other way — lock with d, open with e — and you get a signature that only the key's owner could have made.
Key points — the night-before list
RSA (Rivest, Shamir, Adleman, MIT, 1977) rests on factoring: whoever factors n = p × q can compute the private key.
Key generation: primes p, q; n = pq; φ(n) = (p − 1)(q − 1); choose e with gcd(e, φ) = 1; d = e⁻¹ mod φ by extended Euclid. Public {e, n}, private {d, n}.
Encrypt C = Mᵉ mod n; decrypt M = Cᵈ mod n, with 0 ≤ M < n. It works because ed ≡ 1 (mod φ(n)) and Euler's theorem.
Classic numbers: p = 3, q = 11 → n = 33, φ = 20, e = 7, d = 3. p = 17, q = 11 → n = 187, φ = 160, e = 7, d = 23; M = 88 → C = 11.
Square-and-multiply computes Mᵉ mod n with about 2·log₂e multiplications, reducing mod n after every step.
Signature: S = H(M)ᵈ mod n with the signer's private key; verify H(M) = Sᵉ mod n with the signer's public key. For secrecy, encrypt with the receiver's public key.
Use moduli of 2048 bits or more (3072 for 128-bit security), e = 65537 and padding (OAEP, PSS). Attacks: factoring, timing, small e without padding, chosen ciphertext.
ECC gives equal security with far smaller keys: 256-bit ECC ≈ 3072-bit RSA ≈ 128-bit symmetric key.
01The public-key idea
In 1976 Whitfield Diffie and Martin Hellman published New Directions in Cryptography, proposing that encryption and decryption could use different keys, one of which could be made public. They supplied a key-exchange method but no full cipher. A year later at MIT, Ron Rivest, Adi Shamir and Leonard Adleman found one: RSA (1977, published 1978). It is still the best-known public-key algorithm; its US patent expired in 2000. (Clifford Cocks at Britain's GCHQ had found the same idea in 1973, but it stayed classified until 1997.)
Public-key cryptography needs a trapdoor one-way function: easy to compute in one direction, infeasible to reverse — unless you know a secret, the trapdoor. For RSA the easy direction is modular exponentiation, the hard direction is taking modular roots, and the trapdoor is the factorisation of n.
It must be easy to generate a key pair (public key PU, private key PR).
It must be easy to encrypt with PU and to decrypt with PR.
It must be infeasible to work out PR from PU.
It must be infeasible to recover M from C and PU.
Useful extra (true for RSA): either key may be applied first — encrypting with PR and decrypting with PU gives digital signatures.
Algorithm
Encryption / decryption
Digital signature
Key exchange
RSA
Yes
Yes
Yes (key transport)
Elliptic-curve cryptography
Yes
Yes (ECDSA, EdDSA)
Yes (ECDH)
Diffie–Hellman
No
No
Yes
DSA (Digital Signature Standard)
No
Yes
No
02Number theory toolkit
Prime — an integer greater than 1 whose only divisors are 1 and itself.
gcd(a, b) — greatest common divisor; a and b are relatively prime (coprime) when gcd(a, b) = 1.
a mod n — the remainder when a is divided by n; a ≡ b (mod n) means n divides a − b.
Modular inverse — d is the inverse of e modulo m if e·d ≡ 1 (mod m); it exists exactly when gcd(e, m) = 1.
Euler's totient φ(n) — how many integers from 1 to n are coprime to n.
φ(p) = p − 1 for a prime p
φ(pq) = (p − 1)(q − 1) for distinct primes p and q
Euler's theorem: a^φ(n) ≡ 1 (mod n) when gcd(a, n) = 1
Fermat's theorem: a^(p − 1) ≡ 1 (mod p) when p is prime and p ∤ a
Extended Euclid: find d = 7⁻¹ mod 160
Divide
160 = 22 × 7 + 6.
Divide again
7 = 1 × 6 + 1. The remainder 1 shows gcd(7, 160) = 1, so an inverse exists.
Pick two large, random, distinct primes p and q of similar size (about 1,024 bits each for a 2,048-bit key).
Compute the modulus
n = p × q. Its length in bits is the "key size" (RSA-2048 means n has 2,048 bits).
Compute the totient
φ(n) = (p − 1)(q − 1).
Choose the public exponent
Pick e with 1 < e < φ(n) and gcd(e, φ(n)) = 1. Real keys almost always use e = 65,537.
Compute the private exponent
d = e⁻¹ mod φ(n), so that e·d ≡ 1 (mod φ(n)), using extended Euclid.
Publish and protect
Public key PU = {e, n}. Private key PR = {d, n}; p, q and φ(n) must also stay secret, since any of them reveals d.
Encryption (anyone, with the public key): C = Mᵉ mod n
Decryption (owner, with the private key): M = Cᵈ mod n
Requirement: 0 ≤ M < n
Why it works: ed = 1 + kφ(n) for some integer k, so
Cᵈ = M^(ed) = M · (M^φ(n))ᵏ ≡ M · 1ᵏ = M (mod n) by Euler's theorem
The proof above uses gcd(M, n) = 1, but the result holds for every M below n (an argument using the Chinese remainder theorem covers the rare M that share a factor with n). Note that the same operation — exponentiation mod n — both encrypts and decrypts; only the exponent changes.
Classic PSC numbers
p = 3, q = 11 → n = 33 and φ(n) = 20. With e = 7, d = 3 (7 × 3 = 21 ≡ 1 mod 20). A favourite twist gives d and asks for e: with p = 3, q = 11 and d = 7, e = 3, so the public key is (3, 33).
04Modular exponentiation by repeated squaring
Computing 88⁷ in full gives a 14-digit number, and with 2,048-bit numbers the full power would not fit in the universe. Two ideas make RSA practical. First, reduce modulo n after every multiplication, so numbers never grow beyond n². Second, use the binary form of the exponent: square for every bit, and multiply by the base only when the bit is 1.
Square-and-multiply (left to right)
Write the exponent in binary
For example 23 = 10111₂.
Start
Set result = 1.
For each bit, left to right
result = result² mod n; then, if the bit is 1, result = result × M mod n.
Finish
After the last bit, result = Mᵉ mod n.
Bit
Square (mod 187)
Multiply by 11 if bit = 1 (mod 187)
Result
1
1² = 1
1 × 11 = 11
11
0
11² = 121
—
121
1
121² = 14,641 → 55
55 × 11 = 605 → 44
44
1
44² = 1,936 → 66
66 × 11 = 726 → 165
165
1
165² = 27,225 → 110
110 × 11 = 1,210 → 88
88
Decrypting C = 11 with d = 23 and n = 187 (23 = 10111₂)
CSquare-and-multiply in C (right-to-left form; fine while n < 2³²)
/* modpow.c - square-and-multiply: (base ^ exp) mod m without huge numbers */#include <stdio.h>
unsigned long long modpow(unsigned long long base, unsigned long long exp, unsigned long long m) {
unsigned long long result = 1;
base %= m;
while (exp > 0) {
if (exp & 1) /* lowest bit of exp is 1: multiply */
result = (result * base) % m;
base = (base * base) % m; /* square for the next bit */
exp >>= 1;
}
return result; /* safe while m < 2^32 (no 64-bit overflow) */
}
int main(void) {
printf("88^7 mod 187 = %llu\n", modpow(88, 7, 187)); /* RSA encryption: 11 */
printf("11^23 mod 187 = %llu\n", modpow(11, 23, 187)); /* RSA decryption: 88 */
return 0;
}
The cost is about log₂e squarings plus one multiplication per 1-bit. That is why e = 65,537 = 2¹⁶ + 1 is so popular: it is prime, and its binary form 1 0000 0000 0000 0001 needs only 16 squarings and one extra multiplication, so encryption and signature verification are very fast. Decryption with a full-size d is slower; implementations use the Chinese remainder theorem (working mod p and mod q separately) to speed it up about four times.
05RSA digital signatures
Hash-then-sign
Hash
The signer computes h = H(M) with a hash such as SHA-256 (and pads it).
Sign
S = hᵈ mod n, using the signer's private key. The message M is sent together with S.
Verify
The receiver computes h′ = Sᵉ mod n with the signer's public key and compares it with their own H(M). Equal means the message is intact and was signed by the private-key holder.
Why hash first? Speed — signing a 32-byte digest instead of a large file. Size — the value must be smaller than n. Security — raw RSA is multiplicative (S(M₁) × S(M₂) mod n is a valid signature on M₁ × M₂), so signing raw messages invites forgeries; hashing and padding destroy that structure.
Goal
Sender applies
Receiver applies
What you get
Confidentiality
Receiver's public key PU_b
Receiver's private key PR_b
Only the receiver can read it — but anyone could have sent it
Authentication / signature
Sender's private key PR_a
Sender's public key PU_a
Origin authentication, integrity and non-repudiation — but no secrecy
Both
Sign with PR_a, then encrypt with PU_b
Decrypt with PR_b, then verify with PU_a
Secrecy and authentication together
Who verifies with what?
If Alice signs a message and sends it to Bob, Bob verifies the signature with Alice's public key. Nobody but Alice ever uses Alice's private key.
Standards add padding to every RSA operation: PKCS #1 (RFC 8017) defines RSAES-OAEP for encryption and RSASSA-PSS for signatures, alongside the older v1.5 formats. The Digital Signature Standard (FIPS 186) approves RSA, DSA and ECDSA signatures.
06Security, key sizes and attacks
Anyone who can factor n into p and q can compute φ(n) and then d, so RSA is at most as strong as factoring. Knowing φ(n) or d is in fact equivalent to knowing the factors. If someone found a polynomial-time factoring algorithm, RSA would fall — even if discrete logarithms stayed hard, because RSA does not rely on them (Diffie–Hellman, ElGamal and DSA do).
Modulus factored
Bits
Year
RSA-129
426
1994
RSA-155
512
1999
RSA-768
768
2009
RSA-250
829
2020
Public factoring records — 1,024-bit keys are now deprecated, 2,048 bits is the minimum
Security strength
Symmetric key
RSA modulus
ECC key
80 bits (legacy)
2TDEA
1,024
160
112 bits
3DES (3-key)
2,048
224
128 bits
AES-128
3,072
256
192 bits
AES-192
7,680
384
256 bits
AES-256
15,360
512
Comparable strengths (NIST SP 800-57)
Brute force on d — hopeless for real key sizes.
Mathematical attacks — factoring n with the general number field sieve; the reason key sizes keep growing.
Timing and power attacks (Kocher, 1996) — the time a decryption takes leaks bits of d. Defences: constant-time code and blinding (randomising the input before exponentiation).
Small e without padding — if Mᵉ < n, then C = Mᵉ exactly and M is just the integer e-th root of C; sending the same M to three users with e = 3 also leaks it (Håstad's broadcast attack). Padding defeats both.
Small d — Wiener's attack recovers d when d < n^(1/4)/3, so d must never be chosen small.
Chosen-ciphertext attacks — Bleichenbacher (1998) broke PKCS #1 v1.5 encryption in SSL by watching error messages; OAEP padding resists this.
Quantum computers — Shor's algorithm (1994) would factor n in polynomial time on a large quantum computer, which drives the move to post-quantum algorithms.
Textbook RSA is not safe RSA
Plain C = Mᵉ mod n is deterministic (the same M always gives the same C, so an attacker can test guesses) and malleable. Exam numericals use textbook RSA; real systems always pad.
07Elliptic-curve cryptography and RSA in practice
Elliptic-curve cryptography (ECC) was proposed independently by Neal Koblitz and Victor Miller in 1985. It works with points on a curve y² = x³ + ax + b over a finite field. Adding a point P to itself k times (kP) is easy, but recovering k from P and kP — the elliptic-curve discrete logarithm problem — has no known sub-exponential algorithm. That is why ECC keys can be so much shorter than RSA keys.
RSA
Hard problem: integer factorisation
Keys of 2,048–3,072 bits for today's security
Very fast verification and encryption (small e); slow signing and decryption
Simple and universally supported; in most older certificates
ECC
Hard problem: elliptic-curve discrete logarithm
256-bit keys give 128-bit security
Fast key generation and signing; small keys, signatures and certificates
ECDH, ECDSA and Ed25519 are the defaults in TLS 1.3, SSH and modern messaging apps
In practice RSA rarely encrypts data directly. In hybrid encryption it wraps a random AES session key (RSA-OAEP), and the AES key protects the data. TLS 1.2 could send its pre-master secret this way, but TLS 1.3 removed RSA key transport because it gives no forward secrecy — RSA survives there only for signatures in certificates and handshakes. SSH keys (ssh-rsa, ssh-ed25519) and PGP keys follow the same pattern.
Try it
Open the RSA calculator tool on this page: pick p, q and e, and it derives n, φ(n) and d, then encrypts, decrypts and signs with every step shown. Try p = 3, q = 11, e = 7, and then e = 4 to see why gcd(e, φ) must be 1.
Worked examples
Try the problem first, then reveal the working one step at a time.
EX 1RSA with p = 3, q = 11, e = 7
Generate the RSA key pair for p = 3, q = 11 and e = 7. Then encrypt M = 4 and decrypt the result.
EX 2RSA with p = 17, q = 11, e = 7 and M = 88
Find the private key for p = 17, q = 11, e = 7, encrypt M = 88, and decrypt the ciphertext.
EX 3Finding the public key from the private key
In an RSA system p = 3, q = 11 and the private exponent is d = 7. Find the public key, then encrypt M = 5 and verify by decrypting.
EX 4Signing and verifying a hash
A signer uses p = 7, q = 11 and e = 7. Find d, sign the hash value h = 6, and show how a verifier checks the signature.
Practice session
Work each problem on paper first, then check. Marking one solved earns 10 XP.0/5 solved
Q1
For p = 5, q = 11 and e = 3, find n, φ(n) and d. Encrypt M = 8 and then decrypt your ciphertext.
Q2
For p = 13, q = 17 and e = 5, find d and encrypt M = 10.
Q3
A public key is (e, n) = (17, 3233). Act as the attacker: factor n, compute φ(n) and recover d.
Q4
Use Fermat's little theorem to compute (a) 5¹¹⁷ mod 19 and (b) 3²⁰⁰ mod 13 without large numbers.
Q5
With p = 5 and q = 7, explain why e = 4 cannot be used. Then show that e = 5 is legal but gives d = e, and explain why such a key is useless.
Exam traps
φ(n) = (p − 1)(q − 1), not n − 1 (that is only true when n itself is prime); and d is computed modulo φ(n), not modulo n.
Key directions: for confidentiality encrypt with the receiver's public key; to sign, use the sender's private key; to verify, use the sender's public key.
M must be smaller than n. Larger data is split into blocks — or, in practice, RSA only encrypts a short session key.
RSA rests on factoring; Diffie–Hellman, ElGamal and DSA rest on discrete logarithms. A fast discrete-log algorithm alone would not break RSA.
Small public exponent, large private exponent. e = 3, 17 or 65,537 is normal; a small d is insecure (Wiener's attack). When extended Euclid gives a negative coefficient, add φ(n).
Books write the private key as {d, n} or as {d, p, q}; both are correct — p, q and φ(n) must stay secret in every version.
Quiz — KPSC style
12 questions. 70 % passes the topic quiz (+30 XP, +20 more for 100 %). Exam scoring: +1 correct, −⅓ wrong.
RSA & digital signatures · Question 1 of 12✓ 0 · ✗ 0
The acronym RSA comes from:
Past-paper questions on this topic
Modelled on real KPSC questions (reworded) with the same concept and answer; each links to its original paper.
Past papers · Question 1 of 8✓ 0 · ✗ 0
Modelled on QP 105/2024 · Higher Secondary School Teacher (Junior) Computer Science (8 Aug 2024), Q.93 (reworded)original paper (PDF)
The name RSA is made of its inventors' initials. Who were they?