Rsa Encryption

RSA is one of the first practicable public-key cryptosystems and is widely used for secure data transmission. In such a cryptosystem, the encryption key is public and differs from the decryption key which is kept secret. In RSA, this asymmetry is based on the practical difficulty of factoring the product of two large prime numbers, the factoring problem. RSA stands for Ron RivestAdi Shamir and Leonard Adleman, who first publicly described the algorithm in 1977. Clifford Cocks, an English mathematician, had developed an equivalent system in 1973, but it wasn’t declassified until 1997.[1]


A user of RSA creates and then publishes a public key based on the two large prime numbers, along with an auxiliary value. The prime numbers must be kept secret. Anyone can use the public key to encrypt a message, but with currently published methods, if the public key is large enough, only someone with knowledge of the prime factors can feasibly decode the message. Breaking RSA encryption is known as the RSA problem. It is an open question whether it is as hard as the factoring problem.

The RSA algorithm involves three steps: key generation, encryption and decryption.

Key generation

RSA involves a public key and a private key. The public key can be known by everyone and is used for encrypting messages. Messages encrypted with the public key can only be decrypted in a reasonable amount of time using the private key. The keys for the RSA algorithm are generated the following way:

  1. Choose two distinct prime numbers p and q.
    • For security purposes, the integers p and q should be chosen at random, and should be of similar bit-length. Prime integers can be efficiently found using a primality test.
  2. Compute n = pq.
    • n is used as the modulus for both the public and private keys. Its length, usually expressed in bits, is the key length.
  3. Compute φ(n) = φ(p)φ(q) = (p − 1)(q − 1) = n – (p + q -1), where φ is Euler’s totient function.
  4. Choose an integer e such that 1 < e < φ(n) and gcd(e, φ(n)) = 1; i.e., e and φ(n) are coprime.
    • e is released as the public key exponent.
    • e having a short bit-length and small Hamming weight results in more efficient encryption – most commonly 216 + 1 = 65,537. However, much smaller values of e (such as 3) have been shown to be less secure in some settings.[5]
  5. Determine d as d ≡ e−1 (mod φ(n)); i.e., d is the multiplicative inverse of e (modulo φ(n)).
  • This is more clearly stated as: solve for d given de ≡ 1 (mod φ(n))
  • This is often computed using the extended Euclidean algorithm. Using the pseudocode in the Modular integers section, inputs a and n correspond to e and φ(n), respectively.
  • d is kept as the private key exponent.

The public key consists of the modulus n and the public (or encryption) exponent e. The private key consists of the modulus n and the private (or decryption) exponent d, which must be kept secret. pq, and φ(n) must also be kept secret because they can be used to calculated.


Alice transmits her public key (ne) to Bob and keeps the private key d secret. Bob then wishes to send message M to Alice.

He first turns M into an integer m, such that 0 ≤ m < n by using an agreed-upon reversible protocol known as a padding scheme. He then computes the ciphertext c corresponding to

 c \equiv m^e \pmod{n}

This can be done quickly using the method of exponentiation by squaring. Bob then transmits c to Alice.

Note that at least nine values of m will yield a ciphertext c equal to m, but this is very unlikely to occur in practice.


Alice can recover m from c by using her private key exponent d via computing

 m \equiv c^d \pmod{n}

Given m, she can recover the original message M by reversing the padding scheme.

(In practice, there are more efficient methods of calculating cd using the precomputed values below.)



A worked example

Here is an example of RSA encryption and decryption. The parameters used here are artificially small, but one can also use OpenSSL to generate and examine a real keypair.

  1. Choose two distinct prime numbers, such as
    p = 61 and q = 53
  2. Compute n = pq giving
    n = 61 \times 53 = 3233
  3. Compute the totient of the product as φ(n) = (p − 1)(q − 1) giving
    \varphi(3233) = (61 - 1)(53 - 1) = 3120
  4. Choose any number 1 < e < 3120 that is coprime to 3120. Choosing a prime number for e leaves us only to check that e is not a divisor of 3120.
    Let e = 17
  5. Compute d, the modular multiplicative inverse of e (mod φ(n)) yielding,
    d = 2753
    Worked example for the modular multiplicative inverse:
    e \times d \; \operatorname{mod}\; \varphi(n) = 1
    17 \times 2753  \; \operatorname{mod}\; 3120 = 1

The public key is (n = 3233e = 17). For a padded plaintext message m, the encryption function is

c(m) = m^{17} \; \operatorname{mod}\; 3233

The private key is (n = 3233d = 2753). For an encrypted ciphertext c, the decryption function is

m(c) = c^{2753} \; \operatorname{mod}\; 3233

For instance, in order to encrypt m = 65, we calculate

c = 65^{17} \; \operatorname{mod}\; 3233 = 2790

To decrypt c = 2790, we calculate

m = 2790^{2753} \; \operatorname{mod}\; 3233 = 65

Both of these calculations can be computed efficiently using the square-and-multiply algorithm for modular exponentiation. In real-life situations the primes selected would be much larger; in our example it would be trivial to factor n, 3233 (obtained from the freely available public key) back to the primes p and q. Given e, also from the public key, we could then compute d and so acquire the private key.

Practical implementations use the Chinese remainder theorem to speed up the calculation using modulus of factors (mod pq using mod p and mod q).

The values dpdq and qinv, which are part of the private key are computed as follows:

\begin{align}<br /><br /><br />            d_p &= d\; \operatorname{mod}\; (p-1) = 2753 \; \operatorname{mod}\; (61-1) = 53 \\<br /><br /><br />            d_q &= d\; \operatorname{mod}\;(q-1) = 2753 \; \operatorname{mod}\; (53-1) = 49 \\<br /><br /><br />   q_\text{inv} &= q^{-1} \; \operatorname{mod}\; p = 53^{-1} \; \operatorname{mod}\; 61 = 38 \\<br /><br /><br />                &\Rightarrow (q_\text{inv} \times q) \; \operatorname{mod}\; p = 38 \times 53 \; \operatorname{mod}\; 61= 1<br /><br /><br /> \end{align}

Here is how dpdq and qinv are used for efficient decryption. (Encryption is efficient by choice of public exponent e)

\begin{align}<br /><br /><br />   m_1 &= c^{d_p} \; \operatorname{mod}\; p = 2790^{53} \; \operatorname{mod}\; 61 = 4 \\<br /><br /><br />   m_2 &= c^{d_q} \; \operatorname{mod}\; q = 2790^{49} \; \operatorname{mod}\; 53 = 12 \\<br /><br /><br />     h &= (q_\text{inv} \times (m_1 - m_2)) \; \operatorname{mod}\; p = (38 \times -8) \; \operatorname{mod}\; 61 = 1 \\<br /><br /><br />     m &= m_2 + h \times q = 12 + 1 \times 53 = 65<br /><br /><br /> \end{align}

Signing messages

Suppose Alice uses Bob‘s public key to send him an encrypted message. In the message, she can claim to be Alice but Bob has no way of verifying that the message was actually from Alice since anyone can use Bob’s public key to send him encrypted messages. In order to verify the origin of a message, RSA can also be used to sign a message.

Suppose Alice wishes to send a signed message to Bob. She can use her own private key to do so. She produces a hash value of the message, raises it to the power of d (modulo n) (as she does when decrypting a message), and attaches it as a “signature” to the message. When Bob receives the signed message, he uses the same hash algorithm in conjunction with Alice’s public key. He raises the signature to the power of e (modulo n) (as he does when encrypting a message), and compares the resulting hash value with the message’s actual hash value. If the two agree, he knows that the author of the message was in possession of Alice’s private key, and that the message has not been tampered with since.


Proof using Fermat’s little theorem

The proof of the correctness of RSA is based on Fermat’s little theorem. This theorem states that if p is prime and p does not divide an integer a then

 a^{p - 1} \equiv 1 \pmod{p}

We want to show that (me)d ≡ m (mod pq) for every integer m when p and q are distinct prime numbers and e and d are positive integers satisfying

e d \equiv 1 \pmod{(p - 1)(q - 1)}

We can write

ed - 1 = h(p - 1)(q - 1)

for some nonnegative integer h.

To check two numbers, like med and m, are congruent mod pq it suffices (and in fact is equivalent) to check they are congruent mod p and mod q separately. (This is part of the Chinese remainder theorem, although it is not the significant part of that theorem.) To showmed ≡ m (mod p), we consider two cases: m ≡ 0 (mod p) and m \not\equiv 0 (mod p).

In the first case med is a multiple of p, so med ≡ 0 ≡ m (mod p). In the second case

m^{e d} = m^{(ed - 1)}m = m^{h(p - 1)(q - 1)}m = \left(m^{p - 1}\right)^{h(q - 1)}m \equiv 1^{h(q - 1)}m \equiv m \pmod{p}

where we used Fermat’s little theorem to replace mp−1 mod p with 1.

The verification that med ≡ m (mod q) proceeds in a similar way, treating separately the cases m ≡ 0 (mod q) and m \not\equiv 0 (mod q), using Fermat’s little theorem for modulus q in the second case.

This completes the proof that, for any integer m,

\left(m^e\right)^d \equiv m \pmod{pq}

Proof using Euler’s theorem

Although the original paper of Rivest, Shamir, and Adleman used Fermat’s little theorem to explain why RSA works, it is common to find proofs that rely instead on Euler’s theorem.

We want to show that med ≡ m (mod n), where n = pq is a product of two different prime numbers and e and d are positive integers satisfying ed ≡ 1 (mod φ(n)). Since e and d are positive, we can write ed = 1 + hφ(n) for some non-negative integer hAssuming that m is relatively prime to n, we have

m^{ed} = m^{1 + h\varphi(n)} = m \left(m^{\varphi(n)}\right)^{h} \equiv m (1)^{h} \equiv m \pmod{n}

where the second-last congruence follows from the Euler’s theorem.

When m is not relatively prime to n, the argument just given is invalid. This is highly improbable (only a proportion of 1/p + 1/q − 1/(pq) numbers have this property), but even in this case the desired congruence is still true. Either m ≡ 0 (mod p) or m ≡ 0 (mod q), and these cases can be treated using the previous proof.