RSA Public-Key Cryptography
RSA Public-Key Cryptography
Public-key cryptography and its central idea
RSA is a public-key cryptosystem: a system in which one person can publish information that lets others send them secret messages, without first sharing a secret key.
The receiver publishes a public key that anyone can use to encrypt a message for them.
The receiver keeps a related private key secret and uses it to decrypt messages.
A sender needs no prior contact with the receiver: they can look up the public key, encrypt a message, and send the encrypted result openly.
Someone who sees the public key and encrypted message should not be able to recover the message efficiently.
This can seem paradoxical because the public encryption information is available to everyone, yet only the receiver can decrypt. The resolution is computational: encryption is easy to perform, but recovering the message without the private key is believed to require an impractical amount of computation.
The idea also makes seemingly impossible activities, such as mental poker, possible in principle. Participants can use public-key cryptographic methods to arrange a game in which cards remain concealed from other players, even without a trusted dealer. The broader lesson is that computational difficulty can enable coordination and secrecy that may seem logically impossible at first.
One-way functions and computational difficulty
A function can be easy to compute while being difficult to reverse. Such a function is called a one-way function.
For example, multiplying two large primes is straightforward:
But given only the product n, finding the original primes p and q is believed to be computationally difficult when the primes are sufficiently large and chosen appropriately.
The product uniquely determines its prime factors, but uniqueness does not make those factors easy to discover. The challenge is not that no answer exists; it is that finding the answer may require prohibitively much computation.
Factoring is connected to major questions in computational complexity, including the relationship between the classes P and NP. The status of factoring and its precise relationship to those complexity questions are not settled in the way needed to prove RSA secure. Nevertheless, RSA’s security has historically relied on the widely held belief that factoring suitably large integers is infeasible with known methods.
How RSA keys are generated
Suppose a receiver wants to create an RSA key pair.
1. Choose two large primes
The receiver chooses two distinct, large primes, denoted p and q. They should be generated unpredictably; predictable prime choices could make the key easier to attack.
In textbook descriptions, the primes may be pictured as hundreds of decimal digits long. Practical key generation uses carefully designed procedures and security standards.
2. Compute the modulus
The receiver multiplies the primes:
The number n is the RSA modulus. It is published as part of the public key.
3. Calculate the totient
For distinct primes p and q, Euler’s totient of n is
This value is used to construct the private key. Although n is public, calculating φ(n) from n is straightforward if its prime factors are known. Thus, hiding p and q is essential.
4. Choose the public exponent
The receiver chooses an integer e that is relatively prime to φ(n):
The public exponent must have a multiplicative inverse modulo φ(n).
5. Calculate the private exponent
The receiver finds d such that
Equivalently, d is the modular inverse of e modulo φ(n). The extended Euclidean algorithm can calculate this inverse efficiently.
Public key: (n,e)
Private key: d
Secret setup values: the primes p and q, which must also remain private
The public values are distributed openly. The receiver protects the private key and the prime factors.
Encrypting and decrypting a message
A message must first be represented as an integer m in the range
If a message is too large to fit in this range, it must be divided into suitable blocks or encoded using a secure message-encoding scheme.
Encryption
The sender looks up the receiver’s public key (n,e) and computes
The result c is the ciphertext. The sender can transmit it openly.
Decryption
The receiver uses the private exponent d to compute
Since c≡me(modn), decryption gives
The choice of d ensures that ed≡1(modφ(n)), which makes the exponentiation recover the original message.
Why decryption works
When m is relatively prime to n, Euler’s theorem states that
Because ed has the form 1+kφ(n) for some integer k,
Thus, raising the ciphertext to the power d recovers m.
The correctness of RSA can also be established for messages that are not relatively prime to n, using the fact that n=pq and reasoning separately modulo p and modulo q. This is an application of the Chinese remainder theorem.
Why key generation and RSA operations are feasible
RSA relies on difficult factoring, but the legitimate key-generation and encryption tasks must still be efficient.
Finding large primes
Prime numbers occur frequently enough that randomly generated candidates can be tested until suitable primes are found. The prime number theorem estimates the number of primes up to x:
Here, π(x) counts the primes less than or equal to x, and lnx is the natural logarithm. This means that a randomly selected large integer near x has a probability of being prime on the order of 1/lnx.
A weaker estimate, such as Chebyshev’s bound, also shows that primes are sufficiently common for practical searches. A prime-generation procedure can repeatedly produce candidate numbers and test them until it finds one that is prime.
Choosing the exponent and finding its inverse
Finding an appropriate public exponent is efficient:
The greatest common divisor gcd(e,φ(n)) can be computed using Euclid’s algorithm.
If a candidate e is not relatively prime to φ(n), another candidate can be tested.
Once a suitable e is found, the extended Euclidean algorithm computes its inverse d modulo φ(n).
Computing powers modulo n
Encryption and decryption involve very large exponents, but modular exponentiation can be computed efficiently using repeated squaring. The calculation does not require constructing an enormous ordinary integer such as me; intermediate values are reduced modulo n.
Testing whether candidate primes are prime
A prime-generation method needs an efficient way to reject composite candidates.
The Fermat test
Fermat’s little theorem says that if r is prime and a is not divisible by r, then
Therefore, if a candidate r fails this test for some suitable a, it is definitely composite.
Passing the test does not prove that r is prime. Some composite numbers satisfy the Fermat congruence for particular choices of a.
For many odd composite numbers, at least half of the eligible bases reveal compositeness. If independent random bases are tested repeatedly, the chance of a composite passing every round can become very small. Under the relevant conditions, if the probability of passing one round is at most 21, then passing t independent rounds has probability at most
For example, the bound after 50 rounds is 2−50.
Carmichael numbers and limitations
Carmichael numbers are composite numbers that pass the Fermat congruence for every base relatively prime to the number. They show why the Fermat test alone is not a definitive primality test. More generally, composites can sometimes pass Fermat tests for selected bases.
Consequently, the Fermat test is useful for understanding probabilistic primality testing, but modern secure prime generation generally uses stronger tests, such as Miller–Rabin, rather than relying on Fermat testing alone.
Why factoring threatens RSA
If an attacker can factor the public modulus n, then they recover p and q. From these they can calculate
They can then use the extended Euclidean algorithm to find the inverse of e modulo φ(n), recovering the private exponent d. Thus, factoring n gives an effective way to obtain the decryption key.
The reverse implication is also important: knowledge of the private exponent can be used, with suitable methods, to factor the modulus. This supports the view that recovering the private key is closely connected to factoring.
However, this does not prove that every possible way of recovering plaintext must be equivalent to factoring. There could, in principle, be another attack that decrypts messages without explicitly finding either the factors or the private exponent. A general proof that all such attacks are as hard as factoring is not known for RSA.
Security assumptions and practical cautions
RSA is trusted because factoring appropriately constructed large moduli has resisted known attacks, and because substantial mathematical and computational effort has been devoted to finding efficient factoring methods. Its security is based on the best known methods and current evidence, not on a proof that decryption is impossible.
Security also depends on correct implementation:
The primes must be sufficiently large, distinct, and generated unpredictably.
The private values must remain secret.
The public exponent and key parameters must be selected according to current standards.
Plain textbook RSA, which directly computes memodn, is deterministic and does not by itself provide secure real-world message encryption.
Practical RSA encryption uses secure padding and encoding schemes; without them, messages may be vulnerable even if factoring remains infeasible.
The key distinction is between mathematical correctness and cryptographic security: RSA’s exponentiation rules explain why the intended receiver can recover a message, while secure key generation and message encoding are needed to prevent attackers from exploiting the system.

Ask anything
We've read this whole study set, so questions about it get the best answers.