RSA Algorithm (Java)

import java.util.*;
class RSA
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter 2 Prime nos”);
int p=sc.nextInt();
int q=sc.nextInt();
int n=p*q;
int fi=(p-1)*(q-1);
System.out.println(“Enter the value for e”);
int e=sc.nextInt();
while(true)
{
if(findGCD(fi,e)!=1)
{
System.out.println(“Enter a valid value for e”);
e=sc.nextInt();
}
else
break;
}
int d= findD(e,fi);
System.out.println(“Enter plain text(number):”);
int m=sc.nextInt();
int c= (int)(Math.pow(m,e))%n;
int x= (int)(Math.pow(c,d))%n;
System.out.println(“Cipher text is: “+c);
if(x==m)
System.out.println(“Inputs matched.”);
else
System.out.println(“Inputs mismatched.”);

}

static int findD(int e, int fi)
{
int x;
for(int i=1;i<=fi;i++)
{
x=(i*e)%fi;
if(x==1)
return i;
}
return 0;
}

static int findGCD(int number1, int number2) {
//base case
if(number2 == 0){
return number1;
}
return findGCD(number2, number1%number2);
}
}

Advertisement

4 thoughts on “RSA Algorithm (Java)

Leave a Reply to Darshan Gajara Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.