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);
}
}

4 thoughts on “RSA Algorithm (Java)”

1. shruti r Habib says:

sir please send me random routing code….

2. raviraj says:

that programe is not working having 40 errors

1. Hey Raviraj,

That’s very unfortunate. It’s working fine here. Are those the syntax errors?

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