postheadericon S-Box Example using Java

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

SBOX.java

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


int plain=Integer.parseInt(s);

BasicOperation b=new BasicOperation();
int dec=b.binaryToDecimal(plain);
String encryptedString="";
if(dec < 8)
{
System.out.println("\n Decimal no :"+dec);
int base[]=new int[8];
for(i=0;i<8;i++)
{
base[i]=(i==dec)?1:0;
}

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

for(i=0;i<8;i++)
{
p[i]=base[i];
pTemp[i]=base[i];
}

//apply sender's pbox
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];

for(i=0;i<8;i++)
{
 if(p[i]==1)
break;
}
b.decimalToBinary(i);

b.displayBinaryArray();
int length=b.getLen();
int bin1[]=new int[length];
encryptedString=new String("");

for(i=0;i<length;i++)
{
bin1[i]=b.binaryArrayAtPosition(i);
encryptedString+=bin1[i];
}
}
else
{
System.out.println("\n Please enter 3 digit in binary Form");
//throw user define error here

}
return(encryptedString);
}
public String doDecryption(String s)
{
int i,temp;
//char str2char[]=new char[(s.length()/2)];
char str2char[]=new char[(s.length())];
int cnt=-1;
for(i=0;i<s.length();i++)
{
// Empty space is denoted 0 and no is in ascii.
if((int)s.charAt(i)!=0)
{
cnt++;
str2char[cnt]=s.charAt(i);
}
}

int chipher=Integer.parseInt(new String(str2char));

System.out.println("\n No :"+chipher);

BasicOperation b=new BasicOperation();
int dec=b.binaryToDecimal(chipher);
System.out.println("\n Decimal no :"+dec);


int base[]=new int[8];
for(i=0;i<8;i++)
{
base[i]=(i==dec)?1:0;
}

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

for(i=0;i<8;i++)
{
p[i]=base[i];
pTemp[i]=base[i];
}

//apply receiver's pbox.
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];

for(i=0;i<8;i++)
{
if(p[i]==1)
break;
}

b.decimalToBinary(i);
b.displayBinaryArray();

int length=b.getLen();
int c[]=new int[length];

String decryptedString=new String("");

for(i=0;i<length;i++)
{
decryptedString+=b.binaryArrayAtPosition(i);
}

return(new String(decryptedString));
}
}

BasicOperation.java

package bsr;
public class BasicOperation
{
int bin[] = new int[100];
int xor[] = new int[100];
int temp1[] = new int[100];
int temp2[] = new int[100];

int len;
int xorlen;
//convert binary number to decimal number
public int binaryToDecimal(int myNum)
{
int dec=0,no,i,n=0;
no=myNum;
//Find total digit of no of inupted number
while(no>0)
{
n++;
no=no/10;
}
//Convert inputed number into decimal
no=myNum;
for(i=0;i<n;i++)
{
int temp=no%10;
dec=dec+temp*((int)Math.pow(2,i));
no=no/10;

}
return dec;
}

//convert decimal number to binary number
public int decimalToBinary(int myNum)
{
int j,i=-1,no,n,temp=0;
no=myNum;
int t[]=new int[100];
while(no>0)
{
i++;
temp=no%2;
t[i]=temp;
no=no/2;
}
len=(i+1);
j=-1;
for(i=len;i>=0;i--)
{
j++;
bin[j]=t[i];
}
return len;
}

//find the specific bit value of binary number at given position
public int binaryArrayAtPosition(int pos)
{
return bin[pos];
}

public int xorinArrayAt(int pos)
{
return xor[pos];
}

//perform the binary X-OR operation
public int xorop(int a[],int b[],int arrlen)
{
int i;
for(i=0;i<arrlen;i++)
{
xor[i]=(a[i]==b[i])?0:1;
}
xorlen=i;
return xorlen;
}
//perform the binary X-OR operation
public int xorop(String s,char c[])
{
int temp,i=-1,cnt=-1;
for(i=0;i<s.length();i++)
{
   xor[i]=(s.charAt(i)==c[i])?0:1;
}
xorlen=i;
return xorlen;
}
public int getLen()
{
return len+1;
}

//display binary bit pattern or the array
public void displayBinaryArray()
{
for(int i=0;i<=len;i++)
{
System.out.println("\n Binary Array :"+bin[i]);
}
}
}


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
{


/*
// read data from user
Scanner sc=new Scanner(System.in);
System.out.println("Enter String (Three bit binnary) :");
myString=sc.next();

*/

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

}

Receiver.java

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

SBOX obj =new SBOX();
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


1 comments:

PROF SUKHPAL SINGH SAINI said...

NICE..
I AM PROF SPS SAINI
PL SHARE YOUR CONTACT NO

Blog Archive

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.