## RSA Algorithm: User Defined Library in Java

###
RSA: (**R**ivest, **S**hamir, **A**dleman)

RSA is an encryption/ decryption and authentication system, An algorithm developed in year, 1977 by Ron

**R**ivest, Adi**S**hamir, and Leonard**A**dleman.**RSA is a cryptosystem, which is also known as public-key cryptosystems ( Public Key Encryption ). RSA is normally used for secure data transmission.**
A user of RSA creates product of two large prime numbers, along with an auxiliary value, as public key. The prime numbers given to algorithm kept as secret. The public key is used to encrypt a message, and private key is used to decrypt a message.

## Using the code

Herer is the user defined RSA algorithm library. Library having five function as IsPrime, to check parameter , given number is prime or not. square, to calculate the square.BigMod, to calculate modulation, it means calculate the encryption and decryption data, n_value, to calculate the value of n in RSA algorithm, it is the product of two prime numbers. cal_phi, to calculate the phi in RSA algorithms, it is product of one less than these two prime number.

```
package ClassLib;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author SOHAM GANDHI
*/
public class RSA
{
/**
*
* @param number
* @return
*/
public static boolean IsPrime(int number)
{
if (number < 2) return false;
if (number % 2 == 0) return (number == 2);
int root = (int)java.lang.Math.sqrt((double)number);
for (int i = 3; i <= root; i += 2)
{
if (number % i == 0)
return false;
}
return true;
}
/**
*
* @param a
* @return
*/
public static long square(long a)
{
return (a * a);
}
/**
*
* @param b
* @param p
* @param m
* @return
*/
public static long BigMod(int b, int p, int m) //b^p%m=?
{
if (p == 0)
return 1;
else if (p % 2 == 0)
return square(BigMod(b, p / 2, m)) % m;
else
return ((b % m) * BigMod(b, p - 1, m)) % m;
}
/**
*
* @param prime1
* @param prime2
* @return
*/
public static int n_value(int prime1, int prime2)
{
return( prime1 * prime2);
}
/**
*
* @param prime1
* @param prime2
* @return
*/
public static int cal_phi(int prime1, int prime2)
{
return ( (prime1 - 1) * (prime2- 1) );
}
}
```

## How RSA works?

**Step 1**: Start

**Step 2**: Choose two prime numbers

p = 3 and q = 11

**Step 3**: Compute the value for ‘n’

n = RSA.n_value(RSA_P, RSA_Q);

n = p * q = 3 * 11 = 33

**Step 4**: Compute the value for ? (n)

? (n) = (p - 1) * (q -1) = 2 * 10 = 20

`int phi = RSA.cal_phi(RSA_P, RSA_Q);`

**Step 5**: Choose e such that 1 < e < ? (n) and e and n are coprime. Let e = 7

**Step 6**: Compute a value for d such that (d * e) % ? (n) = 1. d = 3

Public key is (e, n) => (7, 33)

Private Key is (d, n) => (3, 33)

**Step 7**: Stop.

Let M, is plain text (message), M= 2.

Encryption of M is: C = M

^{e }% n.c = "" + RSA.BigMod ( ar[i], RSA_E, n);

Cipher text is, C = 2

^{7}% 33.
C = 29.

Decryption of C is: M = C

^{d }% n.`dc = dc + (char) RSA.BigMod(Integer.parseInt(c) , d, n );`

Plain text (message), M= 29

^{3}% 33.
M= 2

Points of Interest

- How RSA algorithms works?
- What function neeed to implement RSA?
- How module operation used in RSA algorithms?
- How to process Prime number?

## References

- http://en.wikipedia.org/wiki/RSA_(cryptosystem)
- http://en.wikipedia.org/wiki/RSA
- http://en.wikipedia.org/wiki/RSA_Security