Anda di halaman 1dari 2

/*Title:- RSA JAVA

Roll no.:-44*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.net.*;
public class RSA
{
public RSA() throws NumberFormatException, IOException
{
int i,j,p,q,Fi,temp,tempF,flag=1;
long e = 0,d = 0;
BigInteger n;
BigInteger PT = new BigInteger("9");
BigInteger CT = BigInteger.ONE;
p=11;
q=13;
n=BigInteger.valueOf(p*q);
Fi=((p-1)*(q-1));
System.out.println("\np="+p+"\nq="+q+"\nn="+n+"\nFi="+Fi);

for(i=2 ; i<Fi ; i++)


{
if(prime(i))
{
if(((Fi % i)!=0))
{
tempF=Fi;
j=1;
for(j=1 ; j<Fi ; j++)
{
temp=((tempF*j)+1) % i;
if(temp==0 && flag==1)
{
e=i;
d=((tempF*j)+1)/i;
System.out.println("d =
"+d);
System.out.println("e =
"+e);
flag=0;
}
}
}
}
}
BigInteger tpow;
System.out.println("Enter Plain Text to be Encrypted : ");
BufferedReader b = new BufferedReader(new InputStreamReader(Sy
stem.in));
PT = BigInteger.valueOf(Integer.parseInt(b.readLine()));
tpow = power(PT.longValue(),e);
CT = tpow.mod(n);
System.out.println("\nCipher Text = "+CT);
tpow = power(CT.longValue(),d);
PT = tpow.mod(n);
System.out.println("\nPlain Text(Counter Check) = "+PT);
}
public boolean prime(long i)
{
int r,s;
for(r=2;r<i;r++)
{
if((i % r)==0)
{
break;
}
}
if(r==i)
{
return true;
}
else
{
return false;
}
}
BigInteger power(long num,long exp)
{
int i;
BigInteger temp = BigInteger.ONE;
long num1;
for(i=0;i<exp;i++)
{
temp=temp.multiply(BigInteger.valueOf(num));
}
return temp;
}

public static void main(String args[])


{
try
{
RSA r = new RSA();
}catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Anda mungkin juga menyukai