How RSA encrypts with a public exponent

Short answer: RSA builds a public modulus n as a product of two secret primes and a public exponent e that is coprime to λ(n), the Carmichael function of n. The private exponent d is the modular inverse of e modulo λ(n). Encryption of an integer m in 0 … n−1 is m^e mod n. Decryption is c^d mod n, which returns m. The public operation does not reveal p, q, or d. Raw RSA is a deterministic map on integers. A confidential message uses a standard padding scheme, usually around a symmetric key.


What the problem actually asks

A sender should be able to transform a message so that only the holder of a private key can invert the transform, using parameters the sender can publish. RSA publishes n and e. Inversion uses d. Euler’s theorem, in the form m^{λ(n)} ≡ 1 (mod n) when m is coprime to n, makes the exponents cancel: ed = 1 + k λ(n), so m^{ed} ≡ m (mod n). The Chinese remainder theorem extends the same identity to every residue, including those that share a factor with n.

The map m ↦ m^e mod n is completely determined by the public key and the integer m. The same m always produces the same ciphertext. A message longer than n does not fit in one residue. Those two facts are why a deployed encryption scheme pads and why it usually encrypts a short symmetric key rather than a long message.


What the algorithm guarantees

For a correctly generated key, decryption recovers every integer m in 0 … n−1. Key generation is two primality tests, a product, a coprimality test, and one modular inverse. Encryption and decryption are modular exponentiations, polynomial in the bit length of n. Extra secret storage is p, q, and d. Implementations often decrypt modulo p and modulo q and combine the halves with the Chinese remainder theorem. The result is the same m.

The theorem is about the integer map. It does not say that a raw residue hides equality of messages, and it does not say that an altered ciphertext fails in a useful way. Those are properties of a padding and encoding scheme built around the map. Signature generation is the private exponent applied to a hash, which is a different construction from encryption.


The working steps

  1. Draw two distinct large random primes p and q. Set n = p q and λ = lcm(p − 1, q − 1).
  2. Choose a public exponent e with gcd(e, λ) = 1. The private exponent d is the inverse of e modulo λ.
  3. Publish n and e. Keep p, q, and d secret.
  4. To encrypt an integer m with 0 ≤ m < n, send m^e mod n. To decrypt a ciphertext c, compute c^d mod n.
  5. For a real message, encode it with a standard padding scheme, or encrypt a symmetric key and encrypt the message with that key. The residue map is the same either way.

Correctness is the exponent identity modulo n. Confidentiality of arbitrary bytes is a padding and hybrid construction around that identity. Diffie-Hellman agrees on a shared exponent without a public composite. RSA is the public-exponent residue map.


Approaches that look simpler and fail

Approach Why it fails when a long message is split into raw residues
Chop a message into blocks smaller than n and apply the public exponent to each block Each block is a deterministic public function of itself. Equal blocks stay equal, and the encoding is not a secure encryption scheme. Padding or hybrid encryption is the layer that carries a message.
Publish d with n and e The inverse exponent undoes the public map. Publishing it publishes decryption.
Pick e that shares a factor with λ The inverse modulo λ does not exist, so there is no decryption exponent for that e.
Skip the primality test and multiply two random odd numbers The correctness proof used primes, so that λ has the form the inverse is taken modulo. A composite factor breaks that setup.

A shared secret from public powers is Diffie-Hellman. Modular exponentiation is the arithmetic inside both. RSA is the public map m^e mod n and the private inverse.


A working example

This Python fragment publishes n = 61×53 and e = 17, then encrypts the integer 65. Decryption with the inverse exponent modulo φ(n) returns 65. It prints the ciphertext 2790.

p, q, e, m = 61, 53, 17, 65
n = p * q
phi = (p - 1) * (q - 1)
d = pow(e, -1, phi)
c = pow(m, e, n)
assert pow(c, d, n) == m
print(c)

Where it is used

*RSA* signs TLS certificates, SSH user keys, and OpenPGP messages. The public exponent encrypts a small integer or verifies a signature; a long message is encrypted with a symmetric key. OAEP is the padding around the integer encryption.


When you can drop this algorithm

Drop raw residues as a message format. Keep the key generation and the exponent for a scheme that pads, or that wraps a symmetric key, and treat signing a hash as its own operation.


Why this stays published

We publish this so a search for RSA returns the public exponent, the private inverse modulo λ(n), and the reason raw residues are not a message format. RSA publishes n and e, encrypts an integer as m to the e modulo n, and decrypts with the inverse exponent modulo λ(n).

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *