On this page
RSA, described by Ron Rivest, Adi Shamir and Leonard Adleman in their 1978 paper A method for obtaining digital signatures and public-key cryptosystems in Communications of the ACM, is the classic way to sign a message digitally. Their abstract makes a strong promise: "Signatures cannot be forged, and a signer cannot later deny the validity of his signature." That promise holds only when you sign the right thing, and this post shows why.
How does an RSA digital signature work?
You need a key pair. Pick two primes p and q, multiply them to get the modulus n, choose a public exponent e, and find the private exponent d so that e × d leaves remainder 1 when divided by (p − 1)(q − 1). Publish (n, e). Keep d secret.
Sign: hash the message to a number h smaller than n, then compute s = h^d mod n.
Verify: compute s^e mod n. If it equals the hash of the message you received, the signature is valid.
The RSA standard RFC 8017 (PKCS #1 v2.2) writes the signing step as s = m^d mod n and the verifying step as m = s^e mod n, where m is a message representative: an encoded form of the message, not the raw message itself.
It works because raising to e and raising to d undo each other modulo n. Raising a number to a large power is quick with repeated squaring, which our post on modular exponentiation by square and multiply works through step by step.
A worked example with small primes
Take p = 5 and q = 11. Then n = 55 and (p − 1)(q − 1) = 4 × 10 = 40. Choose e = 3. The private exponent is d = 27, because 3 × 27 = 81 = 2 × 40 + 1.
Suppose the message hashes to h = 13 (a toy hash; real hashes are far longer). To sign, compute 13^27 mod 55 by squaring:
13² = 169, and 169 − 3 × 55 = 4, so 13² ≡ 4.
13⁴ ≡ 4² = 16.
13⁸ ≡ 16² = 256 ≡ 36.
13¹⁶ ≡ 36² = 1296 ≡ 31.
Since 27 = 16 + 8 + 2 + 1, multiply 31 × 36 × 4 × 13: 31 × 36 = 1116 ≡ 16, then 16 × 4 = 64 ≡ 9, then 9 × 13 = 117 ≡ 7.
The signature is s = 7.
To verify, anyone with the public key (55, 3) computes 7³ = 343 = 6 × 55 + 13, so 7³ ≡ 13. That matches the hash, so the signature checks out. With n = 55 anyone can factor n and rebuild d; real keys use primes far too large to factor.
Why sign the hash and not the message?
One reason is size: the number you sign must be smaller than n, and a hash turns a message of any length into one fixed-length number. The bigger reason is that "textbook" RSA, applied to the raw message, is easy to forge. Two forgeries, same key:
Forgery 1: choose the signature first. Pick any s, say s = 2, and compute 2³ = 8. The pair "message 8, signature 2" now passes verification, and no private key was used. The forger cannot choose the message, but a system checking raw numbers accepts it.
Forgery 2: multiply two signatures. RSA respects multiplication: (ab)^d = a^d × b^d mod n. Suppose the owner signed message 2 (signature 18, since 18³ = 5832 = 106 × 55 + 2) and message 3 (signature 42, since 42³ = 74088 = 1347 × 55 + 3). Multiply: 18 × 42 = 756 ≡ 41. Check: 41³ = 68921 = 1253 × 55 + 6. So 41 is a valid signature on message 6, which the owner never signed.
Hashing blocks both. Forgery 1 now needs a real message whose hash is exactly 8, which a good hash makes impractical to find. Forgery 2 now needs a message whose hash equals the product of two other hashes, which is just as hard to find. Standards also add padding before signing, for the same reason.
What does a digital signature prove, and what does it not?
NIST's FIPS 186-5, the Digital Signature Standard, says digital signatures "are used to detect unauthorized modifications to data and to authenticate the identity of the signatory." In plain terms, a valid signature proves two things:
the message has not changed since it was signed, because changing it changes the hash;
whoever signed it held the private key that matches the public key you used.
It does not prove:
who the person is. It proves who holds the key. Tying a key to a real person needs something else, such as a certificate.
that the key was not stolen. A leaked d signs exactly as well as its owner.
that the message is true or recent. An old signed message can be replayed unless it carries a date.
that the message is secret. Signing hides nothing. Secrecy is a different job, as our post on one-time pad perfect secrecy explains.
How do you practise RSA signatures for an exam or interview?
Work one toy key from start to finish with this checklist:
Compute n = pq and (p − 1)(q − 1), and check that e shares no factor with (p − 1)(q − 1).
Find d with the extended Euclidean algorithm, and check that e × d leaves remainder 1.
Sign a small hash value with square and multiply, writing d in binary first.
Verify by raising the signature to e; you must get the hash back.
Try both forgeries on your own numbers.
If modular inverses feel shaky, the Fermi Concept Library covers every concept in mathematics and computing, each with its own lesson. To practise, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer.



