Information and Network Security (INS)

Implementing RSA Algorithm

INFORMATION! Input given to this program should be numeric. No characters
/*
	Program to implement RSA Algorithm
	Author: PracsPedia		www.pracspedia.com
*/
import java.io.*;	
class RSA
{
	static int phi,M,n,e,d,C,FLAG;
	public static void main(String args[])throws Exception
	{
		int p,q,s;
		DataInputStream in=new DataInputStream(System.in);
		System.out.println("Enter Two Relatively Prime Numbers\t: ");
		p=Integer.parseInt(in.readLine());
		q=Integer.parseInt(in.readLine());
		n = p*q;
		phi=(p-1)*(q-1);
		System.out.println("\n\tF(n)\t="+phi);
		do
		{
			System.out.println("\n\nEnter e\t: ");
			e=Integer.parseInt(in.readLine());
		}
		while(FLAG==1);
		d = 1;
		do
		{
			s = (d*e)%phi;
			d++;
		}
		while(s!=1);
		d = d-1;
		System.out.println("\n\tPublic Key\t:"+e+"  "+n);
		System.out.println("\n\tPrivate Key\t:"+d+"  "+n);
		System.out.println("\n\nEnter The Plain Text\t: ");
		M=Integer.parseInt(in.readLine());
		encrypt();
		System.out.println("\n\nEnter the Cipher text\t: ");
		C=Integer.parseInt(in.readLine());
		decrypt();
	}	
	public static void encrypt()throws Exception
	{
		int i;
		C = 1;
		for(i=0;i < e;i++)
		C=C*M%n;
		C = C%n;
		System.out.println("\n\tEncrypted keyword :"+C);
	}
	public static void decrypt()throws Exception
	{
		int i;
		M = 1;
		for(i=0;i< d;i++)
		M=M*C%n;
		M = M%n;
		System.out.println("\n\tDecrypted keyword : "+M);
	}	
}
Download Source Code Program List RSA output