Skip to main content

RSA

Main Source:

Rivest–Shamir–Adleman (RSA) is a widely used asymmetric encryption algorithm. It was based on similar mathematical properties with the Diffie-Hellman key exchange, that is large prime numbers and modular arithmetic.

RSA uses public and private key, the public key, which is shared publicly is used to encrypt, and the private key, which is kept secret, is used to decrypt. The key are related mathematically, however, it is computationally infeasible to derive the private key from the public key.

Explanation

The RSA is an actual asymmetric encryption algorithm (e.g., used for exchanging data), it is not used solely for exchanging key like the Diffie-Hellman key exchange.

It consists of four steps:

  1. Key Generation:

    • Randomly select two large prime numbers which has large different, pp and qq. These prime numbers should be kept secret.

    • Compute n=pqn = pq, where nn is the modulus used for both the public and private keys. nn is also publicly available as part of the public key.

    • Calculate λ(n)\lambda(n), where λ\lambda is Carmichael's totient function, and it is the smallest positive integer mm such that am1(modn)a^{m}\equiv 1{\pmod {n}}, where nn is a positive integer, for all integers aa coprime to nn.

      Since n=pqn = pq, λ(n)=lcm(λ(p),λ(q))\lambda(n) = \text{lcm}(\lambda(p), \lambda(q)), and since pp and qq are prime, λ(p)=ϕ(p)=p1\lambda(p) = \phi(p) = p − 1, and likewise λ(q)=q1\lambda(q) = q − 1. Hence, λ(n)=lcm(p1,q1)\lambda(n) = \text{lcm}(p − 1, q − 1), where ϕ\phi is the Euler's totient function.

      The LCM itself can be calculated using Euclidean algorithm through the LCM formula, also λ(n)\lambda(n) is kept secret.

    • Choose a public exponent, ee, such that 2<e<λ(n)2 < e < \lambda(n) and gcd(e,λ(n))=1\text{gcd}(e, \lambda(n)) = 1; that is, ee is coprime to λ(n)\lambda(n). ee is also publicly available as part of the public key.

    • Compute dd as de1((modλ)(n))d \equiv e^{-1} (\pmod \lambda(n)) to obtain dd. dd is modular multiplicative inverse of ee modulo λ(n)\lambda(n). dd can be obtained by solving de1((modλ)(n))de \equiv 1 (\pmod \lambda(n)) using the extended Euclidean algorithm, using the properties of Bézout's identity. This dd is kept secret as the private key exponent.

    In conclusion, modulus nn and exponent ee is the public key. While exponent dd is obtained from pp, qq, and λ(n)\lambda(n), these must be kept secret.

note

In the original RSA paper, the Euler's totient function, ϕ(n)=(p1)×(q1)\phi(n) = (p - 1) \times (q - 1) is used instead of λ(n)\lambda(n).

  1. Key Distribution:

    • The public key is shared with anyone who wants to send an encrypted message to the owner of the private key.
    • The private key must be kept secret by the owner and should not be shared.

    Sender must know recipient's public key to encrypt message, and the recipient must use its own private key to decrypt the message. Similar when the scenario is reversed, if the recipient want to send message to the sender instead, then the recipient needs sender's public key and sender will use their private key to decrypt the message.

  2. Encryption:

    • The sender must convert the plaintext message into a numerical representation, the plaintext will also be added with random padding, becoming mm.
    • The sender retrieves the recipient's public key ee and the modulus nn.
    • The ciphertext is computed as cme mod nc \equiv m^e \text{ mod } n.
  3. Decryption:

    • The recipient uses their private key dd to decrypt the encrypted message.
    • The numerical representation of plaintext mm is calculated by cd(me)dm mod nc^d \equiv (\text{m}^e)^d \equiv m \text{ mod } n.
    • The original message MM is recovered by reversing the random padding scheme in the beginning of encryption scheme.

    Illustration of asymmetric encryption
    Source: https://www.practicalnetworking.net/series/cryptography/using-asymmetric-keys/

Security

RSA is used to encrypt message that is transmitted over insecure network. Attacker would intercept that encrypted message and decrypt it. In order to decrypt it, the attacker would need the recipient's private key.

To obtain the recipient's private key, the modulus nn, which is available in public needs to be prime factorized. Modulus nn is obtained by the product of two prime number pp and qq, we would need to find these numbers. After that, the pp and qq will be used to calculate the Carmichael totient's function to obtain the private key dd, which is used to decrypt the message.

The difficulty of breaking RSA comes when factoring large composite numbers into their prime factors. Modulus nn is obtained by the product of two prime number (product of two prime number is always a composite number), however, factoring large numbers is computationally challenging. As long as the key sizes used in RSA are sufficiently large, it is considered secure against brute force attacks.

tip

See factorization as reference about factoring number.

The level of RSA security can be chosen by how many bits used for prime numbers pp and qq:

  • 1024-bit RSA: The modulus nn is 1024 bits, each prime factor pp and qq would be roughly 512 bits.
  • 2048-bit RSA: nn is 2048 bits, pp and qq is 1024 bits.
  • 3072-bit RSA: nn is 3072 bits, pp and qq is 1536 bits.
  • 4096-bit RSA: nn is 4096 bits, pp and qq is 2048 bits.

By having 2048 bits for pp, it means pp can vary from 00 to 2204812^{2048} - 1, which is approximately equal to 1.1×10771.1 \times 10^{77}. Consider the table below, the time grows exponentially. For reference, 2577907×35324489=910632474645232577907 \times 35324489 = 91063247464523 is approximately 47 bits.

RSA bruteforcing
Source: https://www.semanticscholar.org/paper/Using-Random-Search-and-Brute-Force-Algorithm-in-Budiman-Rachmawati/c54d03d38e7b1e34efb712fb6ec6f23e8673d559