RSA algorithm Step by Step

Background:

RSA is one of the first practical 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 is made of the initial letters of the surnames of 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 not declassified until 1997.

Algorithm:

The RSA algorithm involves three steps:
1. Key generation
2. Encryption
3. Decryption.

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:

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)φ(q) = (p − 1)(q − 1) = n - (p + q -1), where φ is Euler's totient function. This value is kept private.
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.
5. Determine d as d ≡ e−1 (mod φ(n)); i.e., d is the modular 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. Using the pseudocode in the Modular integers section, inputs a and n         correspond to e and φ(n), respectively.
   d is kept as the private key exponent.
6. 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.

An alternative, used by PKCS#1, is to choose d matching de ≡ 1 (mod λ) with λ = lcm(p − 1, q − 1), where lcm is the least common multiple. Using λ instead of φ(n) allows more choices for d. λ can also be defined using the Carmichael function, λ(n).

Encryption

Alice transmits her public key (n, e) to Bob and keeps the private key d secret. Bob then wishes to send message M to Alice.

He first turns M into an integer m, such that 0 ≤ m < n and gcd(m, n) = 1 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 efficiently, even for 500-bit numbers, using Modular exponentiation. Bob then transmits c to Alice.

Note that at least nine values of m will yield a ciphertext c equal to m,[note 1]

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.


package rsa;

import java.math.BigInteger;
import java.util.Random;

public class RsaAlgo
{
    private BigInteger p;
    private BigInteger q;
    private BigInteger N;
    private BigInteger phi;
    private BigInteger e;
    private BigInteger d;
    private int        bitlength = 1024;
    private Random     r;

    public RsaAlgo()
    {
        init();
    }
    //Key Generation
    private void init() {
        /*
        1. Choose two distinct prime numbers p and q.
        */
        r = new Random();
        p = BigInteger.probablePrime(bitlength, r);
        q = BigInteger.probablePrime(bitlength, r);
        /*
        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.
        */
        N = p.multiply(q);
        /*
        3. Compute φ(n) = φ(p)φ(q) = (p − 1)(q − 1) = n - (p + q -1), where φ is Euler's totient function. This value is kept private.
        */
        phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
        /*
        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.[8]
        */
        e = BigInteger.probablePrime(bitlength / 2, r);
        while (phi.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(phi) < 0)
        {
            e.add(BigInteger.ONE);
        }
        /*
        5. Determine d as d ≡ e−1 (mod φ(n)); i.e., d is the modular 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.
          Using the pseudocode in the Modular integers section, inputs a and n correspond to e and φ(n), respectively.
          d is kept as the private key exponent.
        */
        d = e.modInverse(phi);
    }

    // Encryption
    public byte[] encrypt(byte[] message)
    {
        return (new BigInteger(message)).modPow(e, N).toByteArray();
    }

    // Decryption
    public byte[] decrypt(byte[] message)
    {
        return (new BigInteger(message)).modPow(d, N).toByteArray();
    }
}



Reference:Wikipedia
Reactions

Post a Comment

1 Comments