//Server
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Arrays;
import java.io.*;
import java.net.*;
class MyServer implements Runnable
{
//Declare class variables to be used
int i,j=0,tmp,t,k=0,l;
int[] P={1,2,3,4};
int[] K={1,1,1,1};
int[] C={0,0,0,0};
int[] D={0,0,0,0};
int[] S={0,1,2,3,4,5,6,7};
int[] T={1,1,1,1,1,1,1,1};
char x[]=new char[4];
char y[]=new char[4];
char a,b;
String sent="",received=null;
ObjectInputStream ois=null;
ObjectOutputStream oos=null;
BufferedReader br=null;
ServerSocket s=null;
public void run()
{
try
{
//1. Create a server socket
s=new ServerSocket(1025,5);
System.out.println("Client-Server Chat \n-----------------------------------------------------------\nPress Alt+F4 to quit this application.");
//2. Wait for connection
System.out.println("\nWaiting for connection from Client");
Socket connection=s.accept();
System.out.println("Connection received from "+connection.getInetAddress().getHostName()+":"+co nnection.getInetAddress());
System.out.println("Waiting for response.. ");
//3. Get Input and Output streams
oos=new ObjectOutputStream(connection.getOutputStream());
oos.flush();
ois=new ObjectInputStream(connection.getInputStream());
br=new BufferedReader(new InputStreamReader(System.in));
while(true)
{
receiveMessage();
System.out.println("Enter message: ");
sendMessage();
System.out.println("Waiting for response.. ");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
void receiveMessage()
{
try
{
received=(String)ois.readObject();
if(k==0)
{
l=received.length();
for(i=0;i<l;i++)
{
b=received.charAt(i);
P[i]=(int)b; // converting the string into integer data for ciphering
}
for(i=0;i<8;i++)
{
j=(j+S[i]+T[i])%8;
tmp=S[j];
S[j]=S[i];
S[i]=tmp;
}
i=0; j=0;
for(i=0;i<8;i++)
{
i=(i+1)%8;
j=(j+S[i])%8;
tmp=S[j];
S[j]=S[i];
S[i]=tmp;
t=(S[i]+S[j])%8;
k=S[t];
if(i<4) K[i]=k; // generating the cipher key
}
for(i=0;i<l;i++)
{
C[i]=P[i]^K[i]; // ciphering the data with the key
}
for(i=0;i<l;i++)
{
x[i]=(char)C[i];
}
}
else
{
for(i=0;i<l;i++)
{
C[i]=P[i]^K[i]; // ciphering the data with the key
}
for(i=0;i<l;i++)
{
x[i]=(char)C[i];
}
}
String code=new String(x); // the ciphered text that is sent
System.out.println("\nClient->");
System.out.println(code);
}
catch(Exception e)
{
System.out.println(e);
}
}
void sendMessage()
{
try
{
System.out.println("(Type DOT(.) and press enter to terminate the message)");
String temp=null;
sent="";
while(true)
{
temp=br.readLine();
if(temp.equalsIgnoreCase("."))
break;
else
{
l=temp.length();
for(i=0;i<l;i++)
{
b=temp.charAt(i);
P[i]=(int)b; // converting the string into integer data for ciphering
}
for(i=0;i<8;i++)
{
j=(j+S[i]+T[i])%8;
tmp=S[j];
S[j]=S[i];
S[i]=tmp;
}
i=0; j=0;
for(i=0;i<8;i++)
{
i=(i+1)%8;
j=(j+S[i])%8;
tmp=S[j];
S[j]=S[i];
S[i]=tmp;
t=(S[i]+S[j])%8;
k=S[t];
if(i<4) K[i]=k; // generating the cipher key
}
for(i=0;i<l;i++)
{
C[i]=P[i]^K[i]; // ciphering the data with the key
}
for(i=0;i<l;i++)
{
x[i]=(char)C[i];
}
String code=new String(x); // the ciphered text that is sent
sent=sent+code+"\n";
}
}
oos.writeObject(sent);
oos.flush();
}
catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String args[])
{
MyServer s=new MyServer();
s.run();
}
}
//Client
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Arrays;
import java.io.*;
import java.net.*;
class MyClient
{
//Declare class variables to be used
int i,j=0,tmp,t,k=0,l;
int[] P={1,2,3,4};
int[] K={1,1,1,1};
int[] C={0,0,0,0};
int[] D={0,0,0,0};
int[] S={0,1,2,3,4,5,6,7};
int[] T={1,1,1,1,1,1,1,1};
char x[]=new char[4];
char y[]=new char[4];
char a,b;
String sent="",received=null;
ObjectInputStream ois=null;
ObjectOutputStream oos=null;
BufferedReader br=null;
Socket s=null;
public void runCli()
{
try
{
s=new Socket("localhost",1025);
System.out.println("Client-Server Chat \n-----------------------------------------------------------\nPress Alt+F4 to quit this application.");
System.out.println("\nSuccessfully connected to Server-"+s.getInetAddress()+":"+s.getLocalPort()+"\n" );
ois=new ObjectInputStream(s.getInputStream());
oos=new ObjectOutputStream(s.getOutputStream());
br=new BufferedReader(new InputStreamReader(System.in));
while(true)
{
System.out.println("Enter message: ");
sendMessage();
System.out.println("Waiting for response.. ");
receiveMessage();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
void sendMessage()
{
try
{
System.out.println("(Type DOT(.) and press enter to terminate the message)");
String temp=null;
sent="";
while(true)
{
temp=br.readLine();
if(temp.equalsIgnoreCase("."))
break;
else
{
l=temp.length();
for(i=0;i<l;i++)
{
b=temp.charAt(i);
P[i]=(int)b; // converting the string into integer data for ciphering
}
for(i=0;i<8;i++)
{
j=(j+S[i]+T[i])%8;
tmp=S[j];
S[j]=S[i];
S[i]=tmp;
}
i=0; j=0;
for(i=0;i<8;i++)
{
i=(i+1)%8;
j=(j+S[i])%8;
tmp=S[j];
S[j]=S[i];
S[i]=tmp;
t=(S[i]+S[j])%8;
k=S[t];
if(i<4) K[i]=k; // generating the cipher key
}
for(i=0;i<l;i++)
{
C[i]=P[i]^K[i]; // ciphering the data with the key
}
for(i=0;i<l;i++)
{
x[i]=(char)C[i];
}
String code=new String(x); // the ciphered text that is sent
sent=sent+code+"\n";
}
}
oos.writeObject(sent);
oos.flush();
}
catch(Exception e)
{
System.out.println(e);
}
}
void receiveMessage()
{
try
{
received=(String)ois.readObject();
if(k==0)
{
l=received.length();
for(i=0;i<l;i++)
{
b=received.charAt(i);
P[i]=(int)b; // converting the string into integer data for ciphering
}
for(i=0;i<8;i++)
{
j=(j+S[i]+T[i])%8;
tmp=S[j];
S[j]=S[i];
S[i]=tmp;
}
i=0; j=0;
for(i=0;i<8;i++)
{
i=(i+1)%8;
j=(j+S[i])%8;
tmp=S[j];
S[j]=S[i];
S[i]=tmp;
t=(S[i]+S[j])%8;
k=S[t];
if(i<4) K[i]=k; // generating the cipher key
}
for(i=0;i<l;i++)
{
C[i]=P[i]^K[i]; // ciphering the data with the key
}
for(i=0;i<l;i++)
{
x[i]=(char)C[i];
}
}
else
{
for(i=0;i<l;i++)
{
C[i]=P[i]^K[i]; // deciphering the data with the key
}
for(i=0;i<l;i++)
{
x[i]=(char)C[i];
}
}
String code=new String(x);
System.out.println("\nServer->");
System.out.println(code);
}
catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String args[])
{
MyClient c1=new MyClient();
c1.runCli();
}
}