Friday, 8 February 2013

RSA Algorithm

RSA is an algorithm for public-key cryptography that is based on the presumed difficulty of factoring large integers, the factoring problem. RSA stands for Ron Rivest, Adi 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 was classified until 1997.

A user of RSA creates and then publishes the product of two large prime numbers, along with an auxiliary value, as their public key. The 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] Whether breaking RSA encryption is as hard as factoring is an open question known as the RSA problem.

To do this Use whose Kind of key
Send an encrypted message Use the receiver's Public key
Send an encrypted signature Use the sender's Private key
Decrypt an encrypted message Use the receiver's Private key
Decrypt an encrypted signature (and authenticate the sender) Use the sender's Public key

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 to 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 – 1)(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.[4]
  5. Determine d as de−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.
  • d is kept as the private key exponent.
By construction, de ≡ 1 (mod φ(n)). 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. p, q, and φ(n) must also be kept secret because they can be used to calculate d.


Alice transmits her public key (n, e) to Bob and keeps the private key 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.


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.

A working 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.
The public key is (n = 3233, e = 17). For a padded plaintext message m, the encryption function is m17 (mod 3233).
The private key is (n = 3233, d = 2753). For an encrypted ciphertext c, the decryption function is c2753 (mod 3233).
For instance, in order to encrypt m = 65, we calculate
c \equiv 65^{17} \equiv 2790 \pmod{3233} .
To decrypt c = 2790, we calculate
m \equiv 2790^{2753} \equiv 65 \pmod{3233}.
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.

1 comment:

  1. Excellent work ! Using this post I have understood the complete working of rsa algorithm. I am pleased that you used a working example to clear all the things so well. Appreciating !!
    digital signature