http://en.wikipedia.org/wiki/RSA_(algorithm)
The RSA algorithm involves three steps: key generation, encryption and decryption.
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 using the private key. The keys for the RSA algorithm are generated the following way:
The public key consists of the modulus n and the public (or encryption) exponente. Theprivate key consists of the modulus n and the private (or decryption) exponentd which must be kept secret.
Notes:
Encryption rule: ciphertext, c = RsaPublic(m) = me mod n, 1 < m < n-1
Decryption rule: plaintext, m = RsaPrivate(c) = cd mod n
Inverse transformation: m = RsaPrivate(RsaPublic(m))
Signing: signature, s = RsaPrivate(m) = md mod n, 1 < m < n-1
Verification: check, v = RsaPublic(s) = se mod n
Inverse transformation: m = RsaPublic(RsaPrivate(m))
First, note that the inverse transformations for encryption and signing are equivalent, since
So we only need to show that the decryption rule works; that is, for (n, e) as defined above,
All proofs of how RSA works boil down to the fact that for any integer x, x1+kφ(n) ≡ x (mod n).
Note that the algorithm strictly works for 0 ≤ m < n, but we exclude the values m = 0, 1 and m = n-1 from the cryptosystem because there is no secrecy for such messages.
The first proof is short. It uses the Euler-Fermat Theorem. It works for messages m that are relatively prime to the modulus n, that is where gcd(m, n) = 1.
Proof. Suppose gcd(m, n) = 1. The relation ed ≡ 1 (mod φ(n)) gives that ed = 1 + kφ(n) for some integer k. If c ≡ me (mod n) then, working modulo n,
Hence m = cd mod n is a unique integer in the range 0 ≤ m < n. ♦
The first proof does not include the special cases where the message m is divisible by one of the prime factors, p or q. There are p numbers where q|m: that is, m = 0, q, 2q, 3q, ..., (p-1)q. Similarly there are q numbers where p|m, and thus p+q-1 possible numbers where gcd(m, n) > 1 (zero occurs twice). Note that p and q cannot both divide m since we restrict m < n = pq.
The second proof specifically considers the case where gcd(m, n) > 1, but it could easily be written to include the first case above (if gcd(q, n)=1 then just say "Similarly for q" after the result mod p). It usesFermat's Little Theorem and the fact that if s and t are coprime and a ≡ b (mod s) and a ≡ b (mod t), then a ≡ b (mod st).
Proof. Suppose one of the primes, say q, divides m. Then m ≡ 0 (mod q) and so, trivially, we have
If q divides m then p cannot also divide m since m < n = pq and so we must have gcd(m, p) = 1. ByFermat's Little Theorem we have
Now since m1+kφ(n) ≡ m (mod q) and m1+kφ(n) ≡ m (mod p), and p and q are coprime, then it follows that (see † above)
We have ed ≡ 1 (mod φ(n)) ⇒ ed = 1 + kφ(n). So, if c ≡ me (mod n) then cd ≡ med ≡ m1+kφ(n) ≡ m (mod n). Hence m = cd mod n is a unique integer in the range 0 ≤ m < n. ♦