RSA (algorithm)

From Wikipedia, the free encyclopedia
Jump to: navigation, search
 RSA is an algorithm used by modern computers to encrypt and decrypt messages. It is an asymmetric cryptographic algorithm. Asymmetric means that there are two different keys. This is also called public key cryptography, because one of them can be given to everyone. The other key must be kept private. It is based on the fact that finding the factors of a integer is hard (the factoring problem). RSA stands for Ron Rivest, Adi Shamir and Leonard Adleman, who first publicly described it in 1978. A user of RSA creates and then publishes the product of two large prime numbers, along with an auxiliary value, as their public key. he prime factors 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.[1]

Operation[change | change source]

RSA involves a public key and private key. The public key can be known to everyone, it is used to encrypt messages. Messages encrypted using the public key can only be decrypted with the private key. The keys for the RSA algorithm are generated the following way:

  1. Choose two different large random prime numbers p \, and q \,
  2. Calculate n = p q \,
    • n\, is the modulus for the public key and the private keys
  3. Calculate the totient: \phi(n) = (p-1)(q-1) \,.
  4. Choose an integer e\, such that 1 < e\, < \phi(n)\,, and e\, is coprime to \phi(n)\, ie: e\, and \phi (n)\, share no factors other than 1; gcd(e\,,\phi(n)\,) = 1.
    • e\, is released as the public key exponent
  5. Compute d\, to satisfy the congruence relation d e \equiv 1\pmod{\phi(n)}\, ie: de = 1 + k\phi(n)\, for some integer k\,.
    • d\, is kept as the private key exponent

Notes on the above steps:

  • Step 1: Numbers can be probabilistically tested for primality.
  • Step 3: changed in PKCS#1 v2.0 to \lambda(n) = {\rm lcm}(p-1, q-1) \, instead of \phi(n) = (p-1)(q-1) \,.
  • Step 4: A popular choice for the public exponents is e\, = 216 + 1 = 65537. Some applications choose smaller values such as e\, = 3, 5, or 35 instead. This is done to make encryption and signature verification faster on small devices like smart cards but small public exponents may lead to greater security risks.
  • Steps 4 and 5 can be performed with the extended Euclidean algorithm; see modular arithmetic.


The public key is made of the modulus n\, and the public (or encryption) exponent e\,.
The private key is made of the modulus n\, and the private (or decryption) exponent d\, which must be kept secret.

  • For efficiency a different form of the private key can be stored:
    • p\, and q\,: the primes from the key generation,
    • d\mod (p - 1)\, and d\mod(q - 1)\,: often called dmp1 and dmq1.
    • q^{-1} \mod(p)\,: often called iqmp
  • All parts of the private key must be kept secret in this form. p\, and q\, are sensitive since they are the factors of n\,, and allow computation of d\, given e\,. If p\, and q\, are not stored in this form of the private key then they are securely deleted along with other intermediate values from key generation.
  • Although this form allows faster decryption and signing by using the Chinese Remainder Theorem (CRT) it is considerably less secure since it enables side channel attacks. This is a particular problem if implemented on smart cards, which benefit most from the improved efficiency. (Start with y = x^e \pmod{n} and let the card decrypt that. So it computes y^d \pmod{p} or y^d \pmod{q} whose results give some value z. Now, induce an error in one of the computations. Then \gcd(z-x,n) will reveal p or q.)

Encrypting messages[change | change source]

Alice gives her public key (n\, & e\,) to Bob and keeps her private key secret. Bob wants to send message M to Alice.

First he turns M into a number smaller than n by using an agreed-upon reversible protocol known as a padding scheme. He then computes the ciphertext c\, corresponding to:

 c = m^e \mod{n}

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

Decrypting messages[change | change source]

Alice can recover m\, from c\, by using her private key d\, in the following procedure:

m = c^d \mod{n}

Given m\,, she can recover the original message M.

The decryption procedure works because first

c^d \equiv (m^e)^d \equiv m^{ed}\pmod{n}.

Now, since

e d \equiv 1\pmod{p - 1}\, and
e d \equiv 1\pmod{q - 1}\,

Fermat's little theorem yields

m^{ed} \equiv m \pmod{p} and
m^{ed} \equiv m \pmod{q}.

Since p\, and q\, are distinct prime numbers, applying the Chinese remainder theorem to these two congruences yields

m^{ed} \equiv m \pmod{pq}.

Thus,

c^d \equiv m \pmod{n}.

A working example[change | change source]

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

  1. Choose two random prime numbers
  2.  :p = 61 and q=53
  3. Compute n = p q \,
  4.  :n=61*53=3233
  5. Compute the totient \phi(n) = (p-1)(q-1) \,
  6.  :\phi(n) = (61 - 1)(53 - 1) = 3120
  7. Choose e>1 coprime to 3120
  8.  :e=17
  9. Choose d\, to satisfy d e \equiv 1\pmod{\phi(n)}\,
  10.  :d=2753
  11.  :17 * 2753 = 46801 = 1 + 15 * 3120.


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

c = m^e \mod{n} = m^{17} \mod 3233\,.

The private key is (n=3233, d=2753). The decryption function is:

m = c^d \mod{n} = c^{2753} \mod 3233\,.


For example, to encrypt m=123, we calculate

c = 123^{17} \mod 3233 = 855

To decrypt c = 855, we calculate

m = 855^{2753} \mod 3233 = 123.

Both of these calculations can be computed efficiently using the square-and-multiply algorithm for modular exponentiation.

Padding schemes[change | change source]

When used in practice, RSA must be combined with some form of padding scheme, so that no values of M result in insecure ciphertexts. RSA used without padding may have some problems:

  • The values m = 0 or m = 1 always produce ciphertexts equal to 0 or 1 respectively, due to the properties of exponentiation.
  • When encrypting with small encryption exponents (e.g., e = 3) and small values of the m, the (non-modular) result of m^e may be strictly less than the modulus n. In this case, ciphertexts may be easily decrypted by taking the eth root of the ciphertext with no regard to the modulus.
  • RSA encryption is a deterministic encryption algorithm. It has no random component. Therefore, an attacker can successfully launch a chosen plaintext attack against the cryptosystem. They can make a dictionary by encrypting likely plaintexts under the public key, and storing the resulting ciphertexts. The attacker can then observe the communication channel. As soon as they see ciphertexts that match the ones in their dictionary, the attackers can then use this dictionary in order to learn the content of the message.

In practice, the first two problems can arise when short ASCII messages are sent. In such messages, m might be the concatenation of one or more ASCII-encoded character(s). A message consisting of a single ASCII NUL character (whose numeric value is 0) would be encoded as m = 0, which produces a ciphertext of 0 no matter which values of e and N are used. Likewise, a single ASCII SOH (whose numeric value is 1) would always produce a ciphertext of 1. For systems which conventionally use small values of e, such as 3, all single character ASCII messages encoded using this scheme would be insecure, since the largest m would have a value of 255, and 2553 is less than any reasonable modulus. Such plaintexts could be recovered by simply taking the cube root of the ciphertext.

To avoid these problems, practical RSA implementations typically embed some form of structured, randomized padding into the value m before encrypting it. This padding ensures that m does not fall into the range of insecure plaintexts, and that a given message, once padded, will encrypt to one of a large number of different possible ciphertexts. The latter property can increase the cost of a dictionary attack beyond the capabilities of a reasonable attacker.

Standards such as PKCS have been carefully designed to securely pad messages prior to RSA encryption. Because these schemes pad the plaintext m with some number of additional bits, the size of the un-padded message M must be somewhat smaller. RSA padding schemes must be carefully designed so as to prevent sophisticated attacks. This may be made easier by a predictable message structure. Early versions of the PKCS standard used ad-hoc constructions, which were later found vulnerable to a practical adaptive chosen ciphertext attack. Modern constructions use secure techniques such as Optimal Asymmetric Encryption Padding (OAEP) to protect messages while preventing these attacks. The PKCS standard also has processing schemes designed to provide additional security for RSA signatures, e.g., the Probabilistic Signature Scheme for RSA (RSA-PSS).

Signing messages[change | change source]

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. So, 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 produces a hash value of the message, raises it to the power of d mod n (just like when decrypting a message), and attaches it as a "signature" to the message. When Bob receives the signed message, he raises the signature to the power of e mod n (just like 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 secret key, and that the message has not been tampered with since.

Note that secure padding schemes such as RSA-PSS are as essential for the security of message signing as they are for message encryption, and that the same key should never be used for both encryption and signing purposes.

References[change | change source]

  1. Rivest, R.; A. Shamir; L. Adleman (1978). "A Method for Obtaining Digital Signatures and Public-Key Cryptosystems". Communications of the ACM 21 (2): 120–126. doi:10.1145/359340.359342 . http://theory.lcs.mit.edu/~rivest/Rsapaper.pdf.

Other websites[change | change source]