RSA algorithm

RSA (Rivest–Shamir–Adleman) stops people from understanding messages they are not allowed to read. A message only some people can understand is called an encrypted message. Any message can become an encrypted message. An encrypted message can be given to anyone because they will not understand what it says. To understand the encrypted message a person must first get the original message back. This is called decryption.

RSA uses two special numbers. One number is called the public key and allows anyone to write an encrypted message. The second number is called a private key and allows understanding the encrypted message (decryption). Both numbers belong together. They are a pair. RSA gives everyone a different pair of numbers to use.

Reading and writing encrypted messages is called cryptography. There are different kinds of cryptography. Cryptography that uses two numbers (keys), like RSA, is called asymmetric cryptography and public key cryptography.

Usually only reading encrypted messages is important. Anyone may write them. This is why the public key can be given to everyone and is called public. The private key needs to be kept secret.

RSA uses math that is easy to do but hard to undo. Someone who does not have the private key must do a lot of work to find it. Reading an encrypted message with the private key is easy.

The math takes a large number and finds numbers that can be multiplied to equal the large number. The smaller numbers are called factors. If a number has no factors it is called a prime number otherwise it is called a composite number. Finding the smallest factors for a large number is called prime factorization.

Concepts used

RSA uses these concepts from cryptography:

Generating keys

The keys for the RSA algorithm are generated the following way:

  1. Choose two different large random prime numbers p and q. This should be kept secret.
  2. Calculate n=pq.
    • n is the modulus for the public key and the private keys.
  3. Calculate the totient: ϕ(n)=(p−1)(q−1).
  4. Choose an integer e such that 1<e<ϕ(n), and e is co-prime to ϕ(n).
    i.e.: e and ϕ(n) share no factors other than 1: gcd⁡(e,ϕ(n))=1; See greatest common divisor.
    • e is released as the public key exponent.
  5. Compute d to satisfy the congruence relation de≡1(modϕ(n)).
    i.e.: de=1+x⋅ϕ(n) for some integer x. (Simply to say: Calculate d=(1+x⋅ϕ(n))/e to be an integer)
    • d is kept as the private key exponent.

Notes on the above steps:


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

Encrypting message

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 m smaller than n by using an agreed-upon reversible protocol known as a padding scheme. He then computes the ciphertext c corresponding to:

c=memodn

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

Decrypting message

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

Given m, she can recover the original distinct prime numbers, applying the Chinese remainder theorem to these two congruences yields

med≡mmodpq.

Thus,

cd≡mmodn.

Therefore:

m=cd mod n

A working example

Here is an example of RSA encryption and decryption. The prime numbers used here are too small to let us securely encrypt anything. You can use OpenSSL to generate and examine a real keypair.

1. Choose two random prime numbers p and q:
p=61 and q=53;
2. Compute n=pq:
n=61×53=3233;
3. Compute the totient ϕ(n)=(p−1)(q−1):
ϕ(n)=(61−1)(53−1)=3120;
4. Choose e>1 coprime to 3120:
e=17;
5. Choose d to satisfy ed≡1modϕ(n):
d=2753, with 17×2753=46801=1+15×3120.


The public key is (n=3233, e=17). For a padded message mthe encryption function c=memodn becomes:

c=m17mod3233

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

m=c2753mod3233


For example, to encrypt m=123, we calculate

c=12317mod3233=855

To decrypt c=855, we calculate

m=8552753mod3233=123

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

Deriving RSA equation from Euler's theorem

RSA can easily be derived using Euler's theorem and Euler's totient function.

Starting with Euler's theorem,

mϕ(n)≡1(modn)

we must show that decrypting an encrypted message, with the correct key, will give the original message. Given a padded message m, the ciphertext c, is calculated by

c≡me(modn)

Substituting into the decryption algorithm, we have

cd≡(me)d≡med(modn)

We want to show this value is also congruent to m. The values of e and d were chosen to satisfy,

ed≡1(modϕ(n))

Which is to say, there exists some integer k, such that

ed=k×ϕ(n)+1

Now we substitute into the encrypted then decrypted message,

med≡mkϕ(n)+1≡m×mkϕ(n)≡m×(mϕ(n))k(modn)

We apply Euler's theorem, and achieve the result.

m×(1)k≡m(modn)

Padding schemes

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:

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

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

Other websites