# Encryption and Decryption of a key using RSA algorithm

RSA algorithm is all about encryption and decryption and type of asymmetric algorithm in which the Public key is given to everyone and the Private key is kept private.

RSA Algorithm in Cryptography

RSA Algorithm is asymmetric cryptography, which means that nobody expects the browser to decrypt the data even if a third party has a browser's public key. The keys are of two types: Public key and Private key. The public key consists of two numbers where one number obtained is the multiplication of two random prime numbers. Similarly, the value of the private key is generated from the same two prime numbers.

The encryption strength totally lies in the key size and if we double/triple the size of the key, the strength of encryption increases exponentially. In short, the larger the prime number, the better would be the strength of the encryption of the RSA Algorithm.

Steps to realize the RSA algorithm for encrypting as well as decrypting the key:

1. Generating Public key

i) Select two large prime numbers:  p and q.

The first part of the public key is n = p*q

ii) Calculate the value of Euler's Totient Function denoted by phi             and given by the formula,

phi = (p-1)*(q-1)

iii) We also need the value of e to generate the value of the private key. The condition for 'e' is:

a) It must be an integer.

b) The value of e must lie within 1 < e < phi

c) gcd(e,phi) = 1

2. Generating a Private key

The private key(d) is given by:  d = (k*phi + 1)/e

where, the value of k is selected randomly between 1 and 5 by using a Random constructor and nextInt(n-1) function.

3. Encryption Message

c = messageemod(n)

4. Decryption Message

message = cdmod(n)

This is how the output of code looks like: Note:

The message selected is of a random number. It depends on the user what message he/she wants to encrypt/decrypt.