postheadericon Generalized Caesar Cipher example using java

Write a programs to simulate encryption and decryption using Caesar Cipher. algorithm development and Communication between client and server is done using Java socket programming.


CaesarCipher.java

package bsr;
import java.util.*;
import java.net.*;
import java.io.*;
/*A code from www.bipinrupadiya.com*/
public class CaesarCipher
{
char a[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int key=3;


public String doEncryption(String s)
{

int currentCharacter,targetedCharacter,i;
char c1;
char c[]=new char[(s.length())];

for(i=0;i<s.length();i++)
{
currentCharacter=s.charAt(i);
targetedCharacter=currentCharacter+key;

if(targetedCharacter <= 90)
{
c[i]=(char)(s.charAt(i) + key);
}
else if(targetedCharacter <= 93)
{
c[i]=(char)a[((targetedCharacter)-90)-1];
}
else if(targetedCharacter >= 97 && targetedCharacter <= 122)
{
int temp=targetedCharacter-97;
c[i]=(char)a[((temp)+26)];
}
else if(targetedCharacter>122 && targetedCharacter<=125)
{
int temp=((targetedCharacter)-97)-26;
c[i]=(char)a[((temp)+26)];
}

}


String en=new String(c);

return(en);
}
public String doDecryption(String s)
{

int currentCharacter,targetedCharacter,i;
char c1;
char c[]=new char[100];

for(i=0;i<s.length();i++)
{
currentCharacter=s.charAt(i);
targetedCharacter=currentCharacter-key;

if(targetedCharacter>=65 && targetedCharacter<=90)
{
c[i]=(char)(targetedCharacter);
}
else if(targetedCharacter<=64)
{
c[i]=(char)a[26-(65-(targetedCharacter))];
}
else if(targetedCharacter>=97 && targetedCharacter<=122)
{
int temp=targetedCharacter-97;
c[i]=(char)a[((temp)+26)];
}
else if((targetedCharacter<97) && (targetedCharacter>=94))
{
int temp=(52-(97-(targetedCharacter)));
c[i]=(char)a[temp];
}
}
return(new String(c));
}
}

MySocket.java

package bsr;
/*A code from www.bipinrupadiya.com*/
import java.util.*;
import java.net.*;
import java.io.*;
public class MySocket
{
String serverName;
int port;
String frame;
public MySocket()
{
this.serverName="localhost";
this.port=6061;
this.frame="BipinRupadiya";
}
public MySocket(int port)
{
this.port=port;
}
public MySocket(String serverName,int port,String frame)
{
this.serverName=serverName;
this.port=port;
this.frame=frame;
}

public void sendFrame(String frame) throws Exception
{
ServerSocket ss=new ServerSocket(port);
Socket s=ss.accept();

byte bFrame[]=frame.getBytes();
OutputStream os=s.getOutputStream();
os.write(bFrame);
ss.close();
s.close();

    }
public String receiveFrame() throws Exception
{
Socket s=new Socket(serverName,port);
InputStream is=s.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String str=br.readLine();
s.close();
return(str);
    }

}

sender.java

import bsr.*;
/*A code from www.bipinrupadiya.com*/
public class sender
{
public static void main(String args[])throws Exception
{

String myString="BipinRupadiya";
/*
// read data from user
Scanner sc=new Scanner(System.in);
System.out.println("Enter String(only alphabets allowed) :");
myString=sc.next();

*/
CaesarCipher obj =new CaesarCipher();
String encryptedString=obj.doEncryption(myString);
System.out.println("\nEncryted String : "+encryptedString);
MySocket m = new MySocket();
m.sendFrame(encryptedString);
}

}

receiver.java

import bsr.*;
public class receiver
{
public static void main(String args[])throws Exception
{

CaesarCipher obj =new CaesarCipher();

MySocket m = new MySocket();
String encryptedString=m.receiveFrame();
String decryptedString=obj.doDecryption(encryptedString);
System.out.println("\nDecryted String : "+decryptedString);
}
}


To execute this code


  • compile bsr package
  • compile sender.java
  • compile receiver.java
  • open two command prompt
  • in 1st command prompt execute sender.java
  • in 2nd command prompt execute receiver.java

0 comments:

Follow by Email

Blog Archive

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.