RSA Algorithm Implemented in Java
0343 3 05 2007I have my Network Information and Security practical exams on the 4th. I have been trying to implement most of these encryption algorithms on Java. The very nature of the algorithms and being incompetent with Java made it that much more difficult; overflowing, under flowing, data loss and what not!
I also tried to find a simple implementation of the RSA algorithm across the net but with no success. I finally figured a way out to implement it.
It may not be the most accurate implementation; but good enough if you want to learn how RSA works.
The code follows for those who are interested.
import java.io.*;
import java.net.*;
class clientRSA
{
public static void main(String args[]) throws Exception
{
Socket sock = new Socket("localhost", 8080);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedReader read = new BufferedReader(new InputStreamReader(sock.getInputStream()));
PrintWriter write = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()), true);
int p,q,ns,es,ds,nc,ec,dc,fi;
System.out.println("Enter the value of P : ");
p=Integer.parseInt(in.readLine());
System.out.println("Enter the value of Q : ");
q=Integer.parseInt(in.readLine());
nc = p * q;
fi = (p-1) * (q-1);
ec = 1;
while(true)
{
if(fi % ec != 0)
break;
ec++;
}
System.out.println("Value of N at client : " + nc);
System.out.println("Send N to server....");
write.println(nc);
System.out.println("Value of E at client : " + ec);
System.out.println("Send E to server....");
write.println(ec);
dc = 1;
while(true)
{
if((ec * dc) % fi == 1)
break;
dc++;
}
System.out.println("Value of D at client : " + dc);
ns = Integer.parseInt(read.readLine());
es = Integer.parseInt(read.readLine());
String msg;
System.out.print("Enter the message to be encrypted : ");
msg = in.readLine();
int msglen = msg.length();
int c, rem, j;
write.println(msglen);
for(int i=0;i
{
c = (int)msg.charAt(i);
rem = c;
for(j=0;j
{
rem = (rem * c) % ns;
}
write.println(rem);
}
int msglenserver = Integer.parseInt(read.readLine());
int[] servermsg = new int[msglenserver];
int i;
for(i=0;i
{
servermsg[i] = Integer.parseInt(read.readLine());
}
System.out.println("Msg from server : ");
for(i=0;i
{
c = servermsg[i];
rem = c;
for(j=0;j
{
rem = (rem * c) % nc;
}
System.out.print((char)rem);
}
}
}
import java.io.*;
import java.net.*;
class serverRSA
{ public static void main(String args[]) throws Exception
{
ServerSocket s = new ServerSocket(8080);
System.out.println("Waiting for client to connect....");
Socket sock = s.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedReader read = new BufferedReader(new InputStreamReader(sock.getInputStream()));
PrintWriter write = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()), true);
int p,q,ns,es,ds,nc,ec,dc,fi;
System.out.println("Enter the value of P : ");
p=Integer.parseInt(in.readLine());
System.out.println("Enter the value of Q : ");
q=Integer.parseInt(in.readLine());
ns = p * q;
fi = (p-1) * (q-1);
es = 1;
while(true)
{
if(fi % es != 0)
break;
es++;
}
ds = 1;
while(true)
{
if((es * ds) % fi == 1)
break;
ds++;
}
nc = Integer.parseInt(read.readLine());
ec = Integer.parseInt(read.readLine());
System.out.println("Value of N at server : " + ns);
System.out.println("Sending N to client...");
write.println(ns);
System.out.println("Value of E at server : " + es);
System.out.println("Sending E to client...");
write.println(es);
int msglenclient = Integer.parseInt(read.readLine());
int[] clientmsg = new int[msglenclient];
int i;
for(i=0;i
{
clientmsg[i] = Integer.parseInt(read.readLine());
}
int c, rem, j;
System.out.println("Msg from client : ");
for(i=0;i
{
c = clientmsg[i];
rem = c;
for(j=0;j
{
rem = (rem * c) % ns;
}
System.out.print((char)rem);
}
String msg;
System.out.print("Enter the message to be encrypted : ");
msg = in.readLine();
int msglen = msg.length();
write.println(msglen);
for(i=0;i
{
c = (int)msg.charAt(i);
rem = c;
for(j=0;j
{
rem = (rem * c) % nc;
}
write.println(rem);
}
}
}
Dang ! How i wish to understand what you are sayin. I have got some codes for you too. System.print.2.1.b. Print. Map. Cra.zy.u. String. 9. Write print. String system.Break. es = % 12 £ / write buffer { o } print. String.¥ read line. Total
Darling complete code nai aayena dang :s i can see only half of it. Anyways will continue after read line = rem. Time. System.out.print. > es = e. Int.msglen.u. ( u ) Encrypted. O . K. Hehe aba yo solve garing
I tried to decrypt the above, but with no success.. I did not get a single word from it.. k lekheko??????????