Transposition cipher example using Java
Write a programs to simulate encryption and decryption technique using Transposition (Columnar) Cipher, algorithm development and Communication between client and server will be done using Java server socket programming.
TranspositionCipher.java
package bsr;
import java.util.*;
import java.net.*;
import java.io.*;
/*A code from www.bipinrupadiya.com*/
public class TranspositionCipher
{
public String selectedKey;
public char sortedKey[];
public int sortedKeyPos[];
//default constructor define the default key
public TranspositionCipher()
{
selectedKey="megabuck";
sortedKeyPos =new int[selectedKey.length()];
sortedKey =selectedKey.toCharArray();
}
//Parameterized constructor define the custom key
public TranspositionCipher(String myKey)
{
selectedKey=myKey;
sortedKeyPos=new int[selectedKey.length()];
sortedKey=selectedKey.toCharArray();
}
//To reorder data do the sorting on selected key
public void doProcessOnKey()
{
//Find position of each character in selected key and arrange it on alphabetical order
int min,i,j;
char orginalKey[]=selectedKey.toCharArray();
char temp;
//First Sort the array of selected key
for(i=0;i<selectedKey.length();i++)
{
min=i;
for(j=i;j<selectedKey.length();j++)
{
if(sortedKey[min]>sortedKey[j])
{
min=j;
}
}
if(min!=i)
{
temp=sortedKey[i];
sortedKey[i]=sortedKey[min];
sortedKey[min]=temp;
}
}
// Fill the position of array according to alphabetical order
for(i=0;i<selectedKey.length();i++)
{
for(j=0;j<selectedKey.length();j++)
{
if(orginalKey[i]==sortedKey[j])
sortedKeyPos[i]=j;
}
}
}
//to encrypt the targeted string
public String doEncryption(String plainText)
{
int min,i,j;
char orginalKey[]=selectedKey.toCharArray();
char temp;
doProcessOnKey();
//Generate encrypted message by doing encryption using Transpotion Cipher
int row = plainText.length() / selectedKey.length();
int extrabit = plainText.length() % selectedKey.length();
int exrow = (extrabit==0)?0:1;
int rowtemp=-1,coltemp=-1;
int totallen=(row+exrow)*selectedKey.length();
char pmat[][]=new char[(row+exrow)][(selectedKey.length())];
char encry[]=new char[totallen];
int tempcnt=-1;
row=0;
for(i=0;i<totallen;i++)
{
coltemp++;
if(i<plainText.length())
{
if(coltemp==(selectedKey.length()))
{
row++;
coltemp=0;
}
pmat[row][coltemp]=plainText.charAt(i);
}
else
{ //do the padding ...
pmat[row][coltemp]='*';
}
}
int len=-1,k;
for(i=0;i<selectedKey.length();i++)
{
for(k=0;k<selectedKey.length();k++)
{
if(i==sortedKeyPos[k])
{
break;
}
}
for(j=0;j<=row;j++)
{
len++;
encry[len]=pmat[j][k];
}
}
String p1=new String(encry);
return(new String(p1));
}
//to decrypt the targeted string
public String doDecryption(String s)
{
int min,i,j,k;
char key[]=selectedKey.toCharArray();
char encry[]=s.toCharArray();
char temp;
doProcessOnKey();
//Now generating plain message
int row = s.length() / selectedKey.length();
char pmat[][] = new char[row][(selectedKey.length())];
int tempcnt = -1;
for(i=0;i<selectedKey.length();i++)
{
for(k=0;k<selectedKey.length();k++)
{
if(i==sortedKeyPos[k])
{
break;
}
}
for(j=0;j<row;j++)
{
tempcnt++;
pmat[j][k]=encry[tempcnt];
}
}
//store matrix character in to a single string
char p1[] = new char[row*selectedKey.length()];
k=0;
for(i=0;i<row;i++)
{
for(j=0;j<selectedKey.length();j++)
{
if(pmat[i][j]!='*')
{
p1[k++]=pmat[i][j];
}
}
}
p1[k++]='\0';
return(new String(p1));
}
}
import java.util.*;
import java.net.*;
import java.io.*;
/*A code from www.bipinrupadiya.com*/
public class TranspositionCipher
{
public String selectedKey;
public char sortedKey[];
public int sortedKeyPos[];
//default constructor define the default key
public TranspositionCipher()
{
selectedKey="megabuck";
sortedKeyPos =new int[selectedKey.length()];
sortedKey =selectedKey.toCharArray();
}
//Parameterized constructor define the custom key
public TranspositionCipher(String myKey)
{
selectedKey=myKey;
sortedKeyPos=new int[selectedKey.length()];
sortedKey=selectedKey.toCharArray();
}
//To reorder data do the sorting on selected key
public void doProcessOnKey()
{
//Find position of each character in selected key and arrange it on alphabetical order
int min,i,j;
char orginalKey[]=selectedKey.toCharArray();
char temp;
//First Sort the array of selected key
for(i=0;i<selectedKey.length();i++)
{
min=i;
for(j=i;j<selectedKey.length();j++)
{
if(sortedKey[min]>sortedKey[j])
{
min=j;
}
}
if(min!=i)
{
temp=sortedKey[i];
sortedKey[i]=sortedKey[min];
sortedKey[min]=temp;
}
}
// Fill the position of array according to alphabetical order
for(i=0;i<selectedKey.length();i++)
{
for(j=0;j<selectedKey.length();j++)
{
if(orginalKey[i]==sortedKey[j])
sortedKeyPos[i]=j;
}
}
}
//to encrypt the targeted string
public String doEncryption(String plainText)
{
int min,i,j;
char orginalKey[]=selectedKey.toCharArray();
char temp;
doProcessOnKey();
//Generate encrypted message by doing encryption using Transpotion Cipher
int row = plainText.length() / selectedKey.length();
int extrabit = plainText.length() % selectedKey.length();
int exrow = (extrabit==0)?0:1;
int rowtemp=-1,coltemp=-1;
int totallen=(row+exrow)*selectedKey.length();
char pmat[][]=new char[(row+exrow)][(selectedKey.length())];
char encry[]=new char[totallen];
int tempcnt=-1;
row=0;
for(i=0;i<totallen;i++)
{
coltemp++;
if(i<plainText.length())
{
if(coltemp==(selectedKey.length()))
{
row++;
coltemp=0;
}
pmat[row][coltemp]=plainText.charAt(i);
}
else
{ //do the padding ...
pmat[row][coltemp]='*';
}
}
int len=-1,k;
for(i=0;i<selectedKey.length();i++)
{
for(k=0;k<selectedKey.length();k++)
{
if(i==sortedKeyPos[k])
{
break;
}
}
for(j=0;j<=row;j++)
{
len++;
encry[len]=pmat[j][k];
}
}
String p1=new String(encry);
return(new String(p1));
}
//to decrypt the targeted string
public String doDecryption(String s)
{
int min,i,j,k;
char key[]=selectedKey.toCharArray();
char encry[]=s.toCharArray();
char temp;
doProcessOnKey();
//Now generating plain message
int row = s.length() / selectedKey.length();
char pmat[][] = new char[row][(selectedKey.length())];
int tempcnt = -1;
for(i=0;i<selectedKey.length();i++)
{
for(k=0;k<selectedKey.length();k++)
{
if(i==sortedKeyPos[k])
{
break;
}
}
for(j=0;j<row;j++)
{
tempcnt++;
pmat[j][k]=encry[tempcnt];
}
}
//store matrix character in to a single string
char p1[] = new char[row*selectedKey.length()];
k=0;
for(i=0;i<row;i++)
{
for(j=0;j<selectedKey.length();j++)
{
if(pmat[i][j]!='*')
{
p1[k++]=pmat[i][j];
}
}
}
p1[k++]='\0';
return(new String(p1));
}
}
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);
}
}
/*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();
*/
TranspositionCipher obj= new TranspositionCipher();
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
{
TranspositionCipher obj= new TranspositionCipher();
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
Subscribe to:
Post Comments (Atom)
Subjects
- WordPress
- Mobile Computing-4649303 Practical Solution
- Android Programming New Syllabus Theory
- PHP LAMP Question Bank
- PHP LAMP Theory
- Step by Step Android Example
- Android Practical
- Android Theory
- Android Question Bank
- Networking FON Practical
- Networking FON Theory
- OS Practical
- OS Theory
- HTML
- JavaScript
- J2EE WTAD Theory
- J2EE WTAD Question Bank
- J2EE WTAD Quick Guide
- J2EE WTAD GTU Papers
- J2EE WTAD Practical
- Python
- JAVA Theory
- JAVA Practical
- MIS
Categories
- Android (55)
- c (11)
- Configure Tomcat7 (2)
- CSS (3)
- Decryption (16)
- Difference (1)
- Encryption (16)
- Error Detection and Correction Techniques (3)
- FON (27)
- Framing Technic (2)
- install Tomcat (2)
- J2EE (29)
- JAVA (13)
- JavaScript (19)
- linux (8)
- OS (17)
- PHP (11)
- Protocol (3)
- SERVER SOCKET PROGRAMING (7)
- Servlet (13)
- shell script (33)
- unix (22)
- WTAD (34)
Blog Archive
-
▼
2013
(64)
-
▼
June
(25)
- Example of command line argument in C on Linux
- Example of command line argument in Java
- AES with CBC mode example in Java
- AES with CFM mode example in Java
- AES with ECB mode example in Java
- 3DES with CFM mode example in Java
- 3DES with CBC mode example in Java
- 3DES with ECB mode example in Java
- RSA Encryption Example in Java
- JSP JDBC Example
- example of preparedstatement in java
- example of page directive in jsp
- example of jsp scripting elements
- Example of requestdispatcher
- JSTL standard tag library Example
- DES with CBC mode example in Java
- DES with CFM mode example in Java
- DES with ECB mode example in Java
- One Time Pad Encryption Example using Java code
- S-Box Example using Java
- P-Box Example using Java
- Transposition cipher example using Java
- Mono-alphabetic Substitution Cipher example using ...
- Generalized Caesar Cipher example using java
- java socket programming example
-
▼
June
(25)
Total Pageviews
© BipinRupadiya.com. Powered by Blogger.
0 comments:
Post a Comment