postheadericon P-Box Example using Java

Write a programs to simulate encryption and decryption technique using P-Box, algorithm development and Communication between client and server will be done using Java server socket programming.

P-Box.java

package bsr;
import java.util.*;
import java.net.*;
import java.io.*;
/*A code from www.bipinrupadiya.com*/
public class PBOX
{
public String doEncryption(String s)
{
int i,temp;


byte p[]=new byte[8];
byte pTemp[]=new byte[8];

p=s.getBytes();
pTemp=s.getBytes();

//disorder the bit positions
p[0]=pTemp[2];
p[1]=pTemp[4];
p[2]=pTemp[5];
p[3]=pTemp[0];
p[4]=pTemp[6];
p[5]=pTemp[7];
p[6]=pTemp[1];
p[7]=pTemp[3];

return(new String(p));
}
public String doDecryption(String s)
{
int i,temp;

byte p[]=s.getBytes();
byte pTemp[]=s.getBytes();

//reorder the bit positions
p[0]=pTemp[3];
p[1]=pTemp[6];
p[2]=pTemp[0];
p[3]=pTemp[7];
p[4]=pTemp[1];
p[5]=pTemp[2];
p[6]=pTemp[4];
p[7]=pTemp[5];

return(new String(p));
}
}

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();

*/

PBOX obj =new PBOX();

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
{

PBOX obj =new PBOX();

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.