12 Implementation of RSA Algorithm
12 Implementation of RSA Algorithm
DESCRIPTION: -
RSA algorithm is an asymmetric cryptography algorithm. Asymmetric means that
it
works on two different keys i.e. Public Key and Private Key. As the name suggests
that the Public Key is given to everyone and Private Key is kept private.
ALGORITHM: -
Step 1 : Choose two prime numbers p and q.
Step 2 : Calculate n = p*q
Step 3 : Calculate ϕ(n) = (p – 1) * (q – 1)
Step 4 : Choose e such that gcd(e , ϕ(n) ) = 1
Step 5 : Calculate d such that e*d mod ϕ(n) = 1
Step 6 : Public Key {e,n} Private Key {d,n}
Step 7 : Cipher text C = P e mod n where P = plaintext
Step 8 : For Decryption D = D d mod n where D will give back the plaintext.
SOURCE-CODE: -
import java.util.*;
import java.math.*;
class RSA
{
public static void main(String args[])
{
break;
}}
System.out.println("the value of d = "+d);
c=(Math.pow(msg,e))%n;
System.out.println("Encrypted message is : -");
System.out.println(c);
//converting int value of n to BigInteger
BigInteger N = BigInteger.valueOf(n);
//converting float value of c to BigInteger
BigInteger C = BigDecimal.valueOf(c).toBigInteger();
msgback = (C.pow(d)).mod(N);
System.out.println("Decrypted message is : -");
System.out.println(msgback);
}
static int gcd(int e, int z)
{
if(e==0)
return z;
else
return gcd(z%e,e);
}}
break;
}}
System.out.println("the value of d = "+d);
c=(Math.pow(msg,e))%n;
System.out.println("Encrypted message is : -");
System.out.println(c);
//converting int value of n to BigInteger
BigInteger N = BigInteger.valueOf(n);
//converting float value of c to BigInteger
BigInteger C = BigDecimal.valueOf(c).toBigInteger();
msgback = (C.pow(d)).mod(N);
System.out.println("Decrypted message is : -");
System.out.println(msgback);
}
static int gcd(int e, int z)
{
if(e==0)
return z;
else
return gcd(z%e,e);
}}
SAMPLE OUT-PUT: -
Enter the number to be encrypted and decrypted
13
Enter 1st prime number p
2
Enter 2nd prime number q
5
the value of z = 4
the value of e = 3
the value of d = 3
Encrypted message is: -
7.0
Decrypted message is: -
3