Quality information related RSA algorithm can be found at Wikipedia.
follow http://en.wikipedia.org/wiki/RSA
Code language : Java
I have kept two different classes for Sender and Receiver so that their private members should not get accessed.
--------------------------------------------------------------------------------------------------------------
/*
* Implement RSA algorithm
* @author chakreshprasad
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class data
{
double dat[];
int e,n,len;
void getdata(double msg[],int len,int e,int n)
{
this.e=e;
dat=new double[len];
for(int i=0;i<len;i++)
dat[i]=msg[i];
this.n=n;
this.len=len;
}
}
class sender
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
double in[];
int len;
int e; //encryption key
data dt=new data();
void start(int n,int fi) throws IOException
{
System.out.println("Enter the length of message : ");
len=Integer.parseInt(br.readLine());
in=new double[len];
System.out.println("Enter message with ele less than "+n);
for(int i=0;i<len;i++)
{
System.out.println("Enter msg"+(i+1)+" :");
in[i]=Integer.parseInt(br.readLine());
}
/*Choose 'e' s.t. gcd(fi,e)=1*/
/*'e' and 'fi' are coprime*/
e=GCD(fi);
System.out.println("\nPublic Key : { "+e+","+n+" }");
/*encryption*/
System.out.println("Plain message : \n");
for(int i=0;i<len;i++)
System.out.print(" "+in[i]);
for(int i=0;i<len;i++)
{
in[i]= (double)Math.pow((double)in[i],(double)e)%n;
}
System.out.println("\nEncrypted message : \n");
for(int i=0;i<len;i++)
System.out.print(" "+in[i]);
dt.getdata(in, len, e, n);
}
data senddata()
{
return dt;
}
int GCD(int fi)
{
int a=2;
/*1<e<fi*/
while(a<fi)
{
int x=a,b=fi,check=0;
while(check==0)
{
int c;
c=x%b;
//System.out.println("a="+x+"b="+b+"c="+c);
if(c==0)
check=1;//go for new value
x=b;
b=c;
// System.out.println("------------");
if(x==1||b==1)
return a;//are coprime
}
a++;
}
return 1;
}
}
class reciever
{
int d; //decryption key
double dec[];
int n,e;
void start(data dt,int fi)
{
int len=dt.len;
dec=new double[len];
for(int i=0;i<len;i++)
dec[i]=dt.dat[i];
n=dt.n;
e=dt.e;
/* ed mod fi = 1*/
d=1;
while(true)
{
int s;
s=(e*d)%fi;
if(s==1)
break;
d++;
}
System.out.println("\nPrivate Key : { "+d+","+n+" }");
/*Decryption*/
for(int i=0;i<len;i++)
{
dec[i]=(double)Math.pow(dec[i],d)%n;
}
System.out.println("\nDecrypted message : \n");
for(int i=0;i<len;i++)
System.out.print(" "+dec[i]);
}
}
public class rsa
{
public static void main(String[] args) throws IOException
{
//main contains all the public members in RSA algorithm
int p,q,n,fi;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the 1st prime no p : ");
p=Integer.parseInt(br.readLine());
System.out.println("Enter the 2nd prime no q : ");
q=Integer.parseInt(br.readLine());
n=p*q; //This is the value which won't be exceeded by each msg value
/*Euler's Totient Fuction*/
fi=(p-1)*(q-1);
/*Sender sends data*/
sender send=new sender();
send.start(n, fi);
/*Reciever recives data*/
reciever rec=new reciever();
rec.start(send.senddata(), fi);
System.out.println();
}
}
follow http://en.wikipedia.org/wiki/RSA
Code language : Java
I have kept two different classes for Sender and Receiver so that their private members should not get accessed.
--------------------------------------------------------------------------------------------------------------
/*
* Implement RSA algorithm
* @author chakreshprasad
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class data
{
double dat[];
int e,n,len;
void getdata(double msg[],int len,int e,int n)
{
this.e=e;
dat=new double[len];
for(int i=0;i<len;i++)
dat[i]=msg[i];
this.n=n;
this.len=len;
}
}
class sender
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
double in[];
int len;
int e; //encryption key
data dt=new data();
void start(int n,int fi) throws IOException
{
System.out.println("Enter the length of message : ");
len=Integer.parseInt(br.readLine());
in=new double[len];
System.out.println("Enter message with ele less than "+n);
for(int i=0;i<len;i++)
{
System.out.println("Enter msg"+(i+1)+" :");
in[i]=Integer.parseInt(br.readLine());
}
/*Choose 'e' s.t. gcd(fi,e)=1*/
/*'e' and 'fi' are coprime*/
e=GCD(fi);
System.out.println("\nPublic Key : { "+e+","+n+" }");
/*encryption*/
System.out.println("Plain message : \n");
for(int i=0;i<len;i++)
System.out.print(" "+in[i]);
for(int i=0;i<len;i++)
{
in[i]= (double)Math.pow((double)in[i],(double)e)%n;
}
System.out.println("\nEncrypted message : \n");
for(int i=0;i<len;i++)
System.out.print(" "+in[i]);
dt.getdata(in, len, e, n);
}
data senddata()
{
return dt;
}
int GCD(int fi)
{
int a=2;
/*1<e<fi*/
while(a<fi)
{
int x=a,b=fi,check=0;
while(check==0)
{
int c;
c=x%b;
//System.out.println("a="+x+"b="+b+"c="+c);
if(c==0)
check=1;//go for new value
x=b;
b=c;
// System.out.println("------------");
if(x==1||b==1)
return a;//are coprime
}
a++;
}
return 1;
}
}
class reciever
{
int d; //decryption key
double dec[];
int n,e;
void start(data dt,int fi)
{
int len=dt.len;
dec=new double[len];
for(int i=0;i<len;i++)
dec[i]=dt.dat[i];
n=dt.n;
e=dt.e;
/* ed mod fi = 1*/
d=1;
while(true)
{
int s;
s=(e*d)%fi;
if(s==1)
break;
d++;
}
System.out.println("\nPrivate Key : { "+d+","+n+" }");
/*Decryption*/
for(int i=0;i<len;i++)
{
dec[i]=(double)Math.pow(dec[i],d)%n;
}
System.out.println("\nDecrypted message : \n");
for(int i=0;i<len;i++)
System.out.print(" "+dec[i]);
}
}
public class rsa
{
public static void main(String[] args) throws IOException
{
//main contains all the public members in RSA algorithm
int p,q,n,fi;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the 1st prime no p : ");
p=Integer.parseInt(br.readLine());
System.out.println("Enter the 2nd prime no q : ");
q=Integer.parseInt(br.readLine());
n=p*q; //This is the value which won't be exceeded by each msg value
/*Euler's Totient Fuction*/
fi=(p-1)*(q-1);
/*Sender sends data*/
sender send=new sender();
send.start(n, fi);
/*Reciever recives data*/
reciever rec=new reciever();
rec.start(send.senddata(), fi);
System.out.println();
}
}
----------------------------------------------------
RSA implementation:
Enter the 1st prime no p :
3
Enter the 2nd prime no q :
11
Enter the length of message :
6
Enter message with ele less than 33
Enter msg1 :
1
Enter msg2 :
2
Enter msg3 :
3
Enter msg4 :
4
Enter msg5 :
5
Enter msg6 :
6
Public Key : { 3,33 }
Plain message :
1.0 2.0 3.0 4.0 5.0 6.0
Encrypted message :
1.0 8.0 27.0 31.0 26.0 18.0
Private Key : { 7,33 }
Decrypted message :
1.0 2.0 3.0 4.0 5.0 6.0
One my friend is looking for the implementation of this algorithm in java. I will recommend the link to your blog to help him. Thank you for sharing.
ReplyDeletedigital signatures