Switch to full style
Project under GPL source codes are posted here
Post a reply

Row Transposition cipher - encryption-decryption java

Mon Jan 21, 2013 9:57 pm

* Project Name:   Row Transposition cipher - encryption-decryption java
* Programmer:   msi_333
* Type:   Security
* Technology:  Java
* IDE:   NetBeans
* Description:   Row Transposition cipher - encryption-decryption java code , easy to learn code
java code
import java.util.Scanner;

public class RowTrans {

/** Creates a new instance of RowTrans */
public RowTrans(String key, String plain,String Dec) {
keyword=key;
PlainText=plain;
DecKeyWord=Dec;
}
public void UserEntery() {


Len=PlainText.length();
if(PlainText.length()%5!=0) {
if(PlainText.length()%5==1) {

Len=Len+4;

}
if(PlainText.length()%5==2) {

Len=Len+3;
}
if(PlainText.length()%5==3) {

Len=Len+2;
}
if(PlainText.length()%5==4) {

Len=Len+1;
}
}




}

public String getCipher(){
return cyphir;

}

public String getDecipher(){
return dcyphir;
}

public void FillMatrix() {
int ROW=Len/5;
int COL=5;
charmatriex=new String[ROW][COL];
String mychar=" ";
String temp=" ";
int startcol=0;
int startrow=0;
for(int i=0; i<Len; i++) {
if(PlainText.length()>i) {
mychar=PlainText.substring(i,i+1);

temp+=mychar;
charmatriex[startrow][startcol]=mychar;



} else {
charmatriex[startrow][startcol]="X";
}
if(startcol==4)
startrow++;
startcol=(startcol+1)%5;

}
System.out.println(" ");
for(int i=0;i<ROW;i++) {

System.out.print(" | ");
for(int j=0;j<COL;j++) {
if(matrixv==null)
matrixv=charmatriex[i][j];
else {
matrixv+=charmatriex[i][j];

}
System.out.print(" "+charmatriex[i][j]+" ");
}
System.out.println(" | ");

}

}
public void Encrypt() {
int ROW=Len/5;
int COL=5;

int startcol=0;
int startrow=0;
int startcol1=0;
int startrow1=0;
int keyword_loc;
String sub=" ", sub1=" ";
int index1,index2;

for(int i=0; i<Len; i++) {

sub=charmatriex[startrow][startcol];
sub1=keyword.substring(startcol1,startcol1+1);
startcol1++;
keyword_loc=Integer.parseInt(sub1)-1;


if(cyphir==null)
cyphir=charmatriex[startrow][keyword_loc];
else
cyphir+=charmatriex[startrow][keyword_loc];


if(startcol==4) {

startcol1=0;
startrow++;
}


startcol=(startcol+1)%5;



}

System.out.println("The Ciphir Text: "+cyphir);
}
public void FillMatrixdecrypt(){
ROW=Len/5;
COL=5;
charmatriex=new String[ROW][COL];
String mychar=" ";
String temp=" ";
int startcol=0;
int startrow=0;
for(int i=0; i<cyphir.length(); i++) {

mychar=cyphir.substring(i,i+1);

temp+=mychar;
charmatriex[startrow][startcol]=mychar;

if(startcol==4)
startrow++;
startcol=(startcol+1)%5;

}
for(int i=0;i<ROW;i++) {

System.out.print(" | ");
for(int j=0;j<COL;j++) {
if(matrixv==null)
matrixv=charmatriex[i][j];
else {
matrixv+=charmatriex[i][j];

}
System.out.print(" "+charmatriex[i][j]+" ");
}
System.out.println(" | ");

}


}
public void Decrypt() {
keyword=DecKeyWord;
int startcol=0;
int startrow=0;
int startcol1=0;
int startrow1=0;
int keyword_loc;
String[][] temp=new String[ROW][COL];
String sub=" ", sub1=" ";
int index1,index2;
String keywordrep;
keywordrep=Integer.toString(keyword.indexOf("1"));
keywordrep+=Integer.toString(keyword.indexOf("2"));
keywordrep+=Integer.toString(keyword.indexOf("3"));
keywordrep+=Integer.toString(keyword.indexOf("4"));
keywordrep+=Integer.toString(keyword.indexOf("5"));


for(int i=0; i<Len; i++) {

sub=charmatriex[startrow][startcol];
sub1=keywordrep.substring(startcol1,startcol1+1);
startcol1++;
keyword_loc=Integer.parseInt(sub1);


if(dcyphir==null)
dcyphir=charmatriex[startrow][keyword_loc];
else
dcyphir+=charmatriex[startrow][keyword_loc];


if(startcol==4) {

startcol1=0;
startrow++;
}


startcol=(startcol+1)%5;



}
//
System.out.println("The Dcipher Text: "+dcyphir);

}



private int Len=0;
private String keyword;
private String PlainText;
private String cyphir;
private String [][] charmatriex;
private String matrixv;
private String dcyphir;
private int ROW;
private int COL;
private String DecKeyWord;

}


java code
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.DocumentFilter;


public class RowTransMain{

/** Creates a new instance of DesMain */
public RowTransMain() {
}

public static void main(String [] rr){
DesPanel mm = new DesPanel();
mm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mm.setVisible(true);
}
}
class DesPanel extends JFrame{
public DesPanel(){
this.setTitle("PolyTrans");
this.setSize(800,800);
this.setVisible(true);

GenerateGUI();
}

public static JTextArea StepsText = new JTextArea(10,40);

RowTrans tr;
private Container c=this.getContentPane();
private JButton btnCihper = new JButton("Cipher");
private JButton btnDeCihper = new JButton("Decipher");

private JTextArea CipherText = new JTextArea(4,40);
private JTextArea OriginalText = new JTextArea(4,40);
private JTextArea DeCipherText = new JTextArea(4,40);


private JScrollPane OriginalScorl=new JScrollPane(OriginalText,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
private JScrollPane CipherScorl=new JScrollPane(CipherText,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
private JScrollPane DecipherScorl=new JScrollPane(DeCipherText,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
private JScrollPane StepScorl=new JScrollPane(StepsText,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
private JTextField KeyWord = new JTextField(16);
private JTextField DecKeyWord = new JTextField(16);
private JPanel PanelCipher = new JPanel();
private JPanel PanelDecipher = new JPanel();
private JPanel PanelKeyWord = new JPanel();
private JPanel PanelDecKeyWord = new JPanel();

private JPanel PanelOriginaltxt = new JPanel();
private JPanel jpstep=new JPanel();

private JLabel lblKeyWord= new JLabel("KeyWord : ");
private JLabel lblDeKeyWord= new JLabel("Decrypt KeyWord : ");


private void GenerateGUI(){
c.setLayout(new GridLayout(4,1));

c.setLayout(new FlowLayout());

PanelKeyWord.setLayout(new FlowLayout());
PanelKeyWord.add(lblKeyWord);
PanelKeyWord.add(KeyWord);

PanelDecKeyWord.setLayout(new FlowLayout());
PanelDecKeyWord.add(lblDeKeyWord);
PanelDecKeyWord.add(DecKeyWord);


Document doc = KeyWord.getDocument();
AbstractDocument absDoc = (AbstractDocument )doc;
absDoc.setDocumentFilter(new DocumentSizeFilter(8));
KeyWord.setSize(5,1);

Document doc1 = DecKeyWord.getDocument();
AbstractDocument absDoc1 = (AbstractDocument )doc1;
absDoc1.setDocumentFilter(new DocumentSizeFilter(8));
DecKeyWord.setSize(5,1);



PanelOriginaltxt.setBorder(BorderFactory.createTitledBorder("Original Text"));
PanelOriginaltxt.setLayout(new FlowLayout());
PanelOriginaltxt.add(OriginalScorl);

PanelCipher.setBorder(BorderFactory.createTitledBorder("Cipher Text"));
PanelCipher.setLayout(new FlowLayout());
PanelCipher.add(CipherScorl);
PanelCipher.add(btnCihper);

PanelDecipher.setBorder(BorderFactory.createTitledBorder("Decipher Text"));
PanelDecipher.setLayout(new FlowLayout());
PanelDecipher.add(DecipherScorl);
PanelDecipher.add(btnDeCihper);
jpstep.setBorder(BorderFactory.createTitledBorder("Mointor"));
jpstep.setLayout(new FlowLayout());
jpstep.add(StepScorl);


c.add(PanelKeyWord);
c.add(PanelDecKeyWord);

c.add(PanelOriginaltxt);
c.add(PanelCipher);
c.add(PanelDecipher);
c.add(jpstep);

DoActions();


}
private void DoActions(){
ActionHandler action = new ActionHandler();

btnCihper.addActionListener(action);
btnDeCihper.addActionListener(action);

}

private class ActionHandler implements ActionListener{


public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnCihper){
String plaintext=OriginalText.getText();
String keyworD=KeyWord.getText();
String DecKey=DecKeyWord.getText();
StepsText.append("keyword : "+keyworD+'\n');
StepsText.append("PlainText : "+plaintext+'\n');

tr= new RowTrans(keyworD,plaintext,DecKey);
tr.UserEntery();
tr.FillMatrix();
tr.Encrypt();
tr.FillMatrixdecrypt();


CipherText.append(tr.getCipher());

}
if(e.getSource()==btnDeCihper){


tr.Decrypt();



DeCipherText.append("\n"+tr.getDecipher() );
}
}

}
private String fina;

}

class DocumentSizeFilter extends DocumentFilter {
int maxCharacters;

public DocumentSizeFilter(int maxChars) {
maxCharacters = maxChars;
}

public void insertString(FilterBypass fb, int offs, String str, AttributeSet a) throws BadLocationException {

if ((fb.getDocument().getLength() + str.length()) <= maxCharacters)
super.insertString(fb, offs, str, a);
else
Toolkit.getDefaultToolkit().beep();
}

public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a) throws BadLocationException {

if ((fb.getDocument().getLength() + str.length()- length) <= maxCharacters)
super.replace(fb, offs, length, str, a);
else
Toolkit.getDefaultToolkit().beep();
}

}




Post a reply
  Related Posts  to : Row Transposition cipher - encryption-decryption java
 Row Transposition cipher - encryption-decryption Csharp(C#)     -  
 RSA encryption decryption cipher algorithm java     -  
 Encryption and Decryption encryption Affine cipher code     -  
 Ceasar encryption-decryption-cipher-decipher code     -  
 program for transposition encryption     -  
 program for transposition encryption     -  
 How Row Transposition Encryption Algorithm Work     -  
 encryption/ decryption without key using C++     -  
 encryption and decryption in c++     -  
 codes for encryption/decryption algorithms     -  

Topic Tags

Java Algorithms