RSA algorithm
// #@ http://sagarlone.weebly.com/network-security-algorithm-implementation.html
import java.math.BigInteger;
import java.util.Scanner;
public class RSA {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter p: ");
int P=sc.nextInt();
System.out.print("Enter q: ");
int Q=sc.nextInt();
int n=P*Q;
int p=P-1;
int q=Q-1;
int z=p*q;
int d=0;
for(int i=2;i<z;i++)
if(z%i!=0)
{
d=i;
break;
}
int e=1;
for(int i=1;i<z;i++)
if((z*i+1)%d==0)
{
e=(z*i+1)/d;
break;
}
System.out.println("Encryption key: "+e);
System.out.println("Decryption key: "+d);
System.out.print("Enter int no for encryption: ");
BigInteger m=new BigInteger(sc.next());
BigInteger N=new BigInteger(n+"");
BigInteger D=new BigInteger(d+"");
BigInteger E=new BigInteger(e+"");
BigInteger em=(m.pow(e)).mod(N);
BigInteger dm=em.pow(d).mod(N);
System.out.println("Message enter: "+m);
System.out.println("Encrypted message:"+em);
System.out.println("After Decryption: "+dm);
}
}
import java.math.BigInteger;
import java.util.Scanner;
public class RSA {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter p: ");
int P=sc.nextInt();
System.out.print("Enter q: ");
int Q=sc.nextInt();
int n=P*Q;
int p=P-1;
int q=Q-1;
int z=p*q;
int d=0;
for(int i=2;i<z;i++)
if(z%i!=0)
{
d=i;
break;
}
int e=1;
for(int i=1;i<z;i++)
if((z*i+1)%d==0)
{
e=(z*i+1)/d;
break;
}
System.out.println("Encryption key: "+e);
System.out.println("Decryption key: "+d);
System.out.print("Enter int no for encryption: ");
BigInteger m=new BigInteger(sc.next());
BigInteger N=new BigInteger(n+"");
BigInteger D=new BigInteger(d+"");
BigInteger E=new BigInteger(e+"");
BigInteger em=(m.pow(e)).mod(N);
BigInteger dm=em.pow(d).mod(N);
System.out.println("Message enter: "+m);
System.out.println("Encrypted message:"+em);
System.out.println("After Decryption: "+dm);
}
}