 RSA is a cryptosystem, which is known as 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 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 wasn't declassified 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.Whether breaking RSA encryption is as hard as factoring is an open question known as the RSA problem.

RSA Algorithm Example

``` Choose p = 3 and q = 11 Compute n = p * q = 3 * 11 = 33 Compute φ(n) = (p - 1) * (q - 1) = 2 * 10 = 20 Choose e such that 1 < e < φ(n) and e and n are coprime. Let e = 7 Compute a value for d such that (d * e) % φ(n) = 1. One solution is d = 3 [(3 * 7) % 20 = 1] Public key is (e, n) => (7, 33) Private key is (d, n) => (3, 33) The encryption of m = 2 is c = 27 % 33 = 29 The decryption of c = 29 is m = 293 % 33 = 2 ```

The RSA algorithm involves three steps:

1. Key Generation
2. Encryption
3. Decryption

1. The 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:

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.

Compute n = p*q.

n is used as the modulus for both the public and private keys. Its length, usually expressed in bits, is the key length.

Compute φ(n) = φ(p)*φ(q) = (p − 1)*(q − 1), where φ is Euler's totient function.

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.

Determine d as d−1 ≡ e (mod φ(n)), i.e., d is the multiplicative inverse of e (modulo φ(n)).

This is more clearly stated as: solve for d given d⋅e ≡ 1 (mod φ(n))
This is often computed using the extended Euclidean algorithm. 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.

p, q, and φ(n) must also be kept secret because they can be used to calculate d.

2. ENCRYPTION

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 = m^e * mod{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.

3.DECRYPTION

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

m = c^d mod{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.

Choose two distinct prime numbers, such as

p = 61 and q = 53.

Compute n = pq giving

n = 61 * 53 = 3233.

Compute the totient of the product as φ(n) = (p − 1)*(q − 1) giving

φ(3233) = (61 - 1)*(53 - 1) = 3120.

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. 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

c(m) = m^{17} * mod 3233.

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

m(c) = c^{2753} * mod 3233.

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

c = 65^{17} * mod 3233 = 2790

To decrypt c = 2790, we calculate

m = 2790^{2753} * 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.