You are on page 1of 3

Assignment No: 7

Title: Implement RSA


Problem Statement: To implement RSA algorithm to encrypt the given text.
Name : Jayesh Jadhav
Subject : Network Security
Class: BE-C
Roll no : 43
CODE:
importjava.math.BigInteger;
importjava.util.Random;
importjava.util.Scanner;
public class RSA {
BigInteger p,q,N,totient,e,d,M,C;
Int bitLength = 1024;
Int mssg;
Random r = new Random();
Void generatePrime()
{

p = BigInteger.probablePrime(bitLength, r);
q = BigInteger.probablePrime(bitLength, r);

N = p.multiply(q);
totient = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
}
Void genEncrKey()

{
Int maxNumBitLength = totient.bitLength();
int x;
do {
e = new BigInteger(maxNumBitLength, r);
x = e.gcd(totient).intValue();
} while((x!=1)||(e.compareTo(totient)>0 &&e.compareTo(BigInteger.ONE)<0));
d = e.modInverse(totient);
}
void accept()
{
System.out.println("Enter Message to be Encrypted: ");
Scanner sc = new Scanner(System.in);
mssg = sc.nextInt();
M = BigInteger.valueOf(mssg);
}
public static void main(String[] args) {
RSA rs = new RSA();
rs.generatePrime();
rs.genEncrKey();
rs.accept();
//Encryption
rs.C = rs.M.modPow(rs.e, rs.N);
System.out.println("Encrypted Text is: "+rs.C);
//Decryption
rs.M = rs.C.modPow(rs.d, rs.N);
rs.mssg = rs.M.intValue();

System.out.println("Decrypted Text is: "+ rs.mssg);


}
}

OUTPUT:
Enter Message to be Encrypted:
456789
Encrypted Text is:
206487879739544760016093947203947961936468057105942594554891083762617956
623558615171195394819662029365315710835453839375549425113270389656482884
255018337069681952531604261058546688108266917160111514135357153439648440
522536699328083490984839457616721101262881912380339282164884122560922543
488232665218936710831178008032156983317810018292652801179478039999499227
542952506652512182234441106514100303344621316059818356619328651425587991
419830954415005314385895297759512926314702652462975969093244179236153680
759414774261247871164692825087307805022768077675733429960539181457274957
7734929084569553066190701868409801318550
Decrypted Text is: 456789

You might also like