1. Home
  2. Explore
  3. Computer Science
  4. RSA Public-Key Cryptography

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

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:

n=pqn = pqn=pq

But given only the product nnn, finding the original primes ppp and qqq 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\mathrm{P}P and NP\mathrm{NP}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 ppp and qqq. 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:

n=pqn = pqn=pq

The number nnn is the RSA modulus. It is published as part of the public key.

3. Calculate the totient

For distinct primes ppp and qqq, Euler’s totient of nnn is

φ(n)=(p−1)(q−1)\varphi(n) = (p-1)(q-1)φ(n)=(p−1)(q−1)

This value is used to construct the private key. Although nnn is public, calculating φ(n)\varphi(n)φ(n) from nnn is straightforward if its prime factors are known. Thus, hiding ppp and qqq is essential.

4. Choose the public exponent

The receiver chooses an integer eee that is relatively prime to φ(n)\varphi(n)φ(n):

gcd⁡(e,φ(n))=1\gcd(e,\varphi(n)) = 1gcd(e,φ(n))=1

The public exponent must have a multiplicative inverse modulo φ(n)\varphi(n)φ(n).

5. Calculate the private exponent

The receiver finds ddd such that

ed≡1(modφ(n))ed \equiv 1 \pmod{\varphi(n)}ed≡1(modφ(n))

Equivalently, ddd is the modular inverse of eee modulo φ(n)\varphi(n)φ(n). The extended Euclidean algorithm can calculate this inverse efficiently.

  • Public key: (n,e)(n,e)(n,e)

  • Private key: ddd

  • Secret setup values: the primes ppp and qqq, 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 mmm in the range

0≤m<n0 \leq m < n0≤m<n

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)(n,e)(n,e) and computes

c≡me(modn)c \equiv m^e \pmod nc≡me(modn)

The result ccc is the ciphertext. The sender can transmit it openly.

Decryption

The receiver uses the private exponent ddd to compute

m≡cd(modn)m \equiv c^d \pmod nm≡cd(modn)

Since c≡me(modn)c \equiv m^e \pmod nc≡me(modn), decryption gives

cd≡med(modn)c^d \equiv m^{ed} \pmod ncd≡med(modn)

The choice of ddd ensures that ed≡1(modφ(n))ed \equiv 1 \pmod{\varphi(n)}ed≡1(modφ(n)), which makes the exponentiation recover the original message.

Why decryption works

When mmm is relatively prime to nnn, Euler’s theorem states that

mφ(n)≡1(modn)m^{\varphi(n)} \equiv 1 \pmod nmφ(n)≡1(modn)

Because ededed has the form 1+kφ(n)1+k\varphi(n)1+kφ(n) for some integer kkk,

med=m1+kφ(n)≡m(modn)m^{ed} = m^{1+k\varphi(n)} \equiv m \pmod nmed=m1+kφ(n)≡m(modn)

Thus, raising the ciphertext to the power ddd recovers mmm.

The correctness of RSA can also be established for messages that are not relatively prime to nnn, using the fact that n=pqn=pqn=pq and reasoning separately modulo ppp and modulo qqq. 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 xxx:

π(x)∼xln⁡x\pi(x) \sim \frac{x}{\ln x}π(x)∼lnxx​

Here, π(x)\pi(x)π(x) counts the primes less than or equal to xxx, and ln⁡x\ln xlnx is the natural logarithm. This means that a randomly selected large integer near xxx has a probability of being prime on the order of 1/ln⁡x1/\ln x1/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))\gcd(e,\varphi(n))gcd(e,φ(n)) can be computed using Euclid’s algorithm.

  • If a candidate eee is not relatively prime to φ(n)\varphi(n)φ(n), another candidate can be tested.

  • Once a suitable eee is found, the extended Euclidean algorithm computes its inverse ddd modulo φ(n)\varphi(n)φ(n).

Computing powers modulo nnn

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 mem^eme; intermediate values are reduced modulo nnn.


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 rrr is prime and aaa is not divisible by rrr, then

ar−1≡1(modr)a^{r-1} \equiv 1 \pmod rar−1≡1(modr)

Therefore, if a candidate rrr fails this test for some suitable aaa, it is definitely composite.

Passing the test does not prove that rrr is prime. Some composite numbers satisfy the Fermat congruence for particular choices of aaa.

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 12\frac{1}{2}21​, then passing ttt independent rounds has probability at most

(12)t\left(\frac{1}{2}\right)^t(21​)t

For example, the bound after 505050 rounds is 2−502^{-50}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 nnn, then they recover ppp and qqq. From these they can calculate

φ(n)=(p−1)(q−1)\varphi(n)=(p-1)(q-1)φ(n)=(p−1)(q−1)

They can then use the extended Euclidean algorithm to find the inverse of eee modulo φ(n)\varphi(n)φ(n), recovering the private exponent ddd. Thus, factoring nnn 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 me mod nm^e \bmod nmemodn, 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.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent