• Rail Fence Cipher.
• Transposition Cipher ( column permutations)
• Vigenere Cipher
i write my code but i didn't work i don't know what is the wrong please help
import java.awt.FlowLayout;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.awt.Font;
import java.awt.event.*;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.Color;
import javax.swing.BorderFactory;
import java.awt.SystemColor;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import java.lang.*;
public class security extends JFrame implements ItemListener
,ActionListener
{
int R_flag=0,T_flag=0,V_flag=0,en_flag=0,de_flag=0;
private JButton Encryption = new JButton();
private JButton Decryption = new JButton();
private JRadioButton jRadioButton1 =
new RadioButton("",true);
private JLabel jLabel1 = new JLabel();
private JRadioButton jRadioButton2 =
new JRadioButton("",false);
private JLabel jLabel5 = new JLabel();
private JLabel jLabel6 = new JLabel();
private JLabel jLabel7 = new JLabel();
private JTextArea plainText = new JTextArea();
private JTextField key= new JTextField();
private JTextField cipherText = new JTextField();
private JRadioButton jRadioButton3 =
new JRadioButton("",false);
private Font f;
private ButtonGroup radioGroup;
String Keyword[][]={{"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"},
{"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"},
{"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"},
{"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"},
{"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"},
{"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"},
{"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"},
{"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"},
{"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"},
{"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"},
{"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"},
{"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"},
{"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"},
{"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"},
{"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"},
{"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"},
{"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"},
{"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"},
{"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"},
{"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"},
{"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"},
{"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"},
{"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"},
{"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"},
{"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"},
{"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"}
};
public security()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
setVisible(true);
}
private void jbInit() throws Exception
{
this.getContentPane().setLayout(null);
this.setSize(new Dimension(597, 406));
this.setTitle("Validate Digital Signiture");
this.setForeground(SystemColor.inactiveCaptionText);
this.setBackground(Color.white);
Encryption.setText("Encryption");
Encryption.setBounds(new Rectangle(165, 315, 115, 30));
Encryption.setFont(new Font("Dialog", 1, 13));
Encryption.setForeground(new Color(112, 13, 0));
Encryption.addActionListener(this);
Decryption.setText("Decryption");
Decryption.setBounds(new Rectangle(385, 315, 125, 30));
Decryption.setFont(new Font("Dialog", 1, 13));
Decryption.setForeground(new Color(112, 13, 12));
Decryption.addActionListener(this);
jRadioButton1.setText("Rail Fence Cipher");
jRadioButton1.setBounds(new Rectangle(205, 55, 125, 15));
jRadioButton1.setForeground(SystemColor.desktop);
jRadioButton1.setFont(new Font("Dialog", 1, 12));
jLabel1.setText("choose the encryption algorithm");
jLabel1.setBounds(new Rectangle(205, 25, 210, 15));
jLabel1.setForeground(Color.magenta);
jLabel1.setFont(new Font("Dialog", 1, 13));
jRadioButton2.setText("Vigenere Cipher ");
jRadioButton2.setBounds(new Rectangle(205, 120, 140, 15));
jRadioButton2.setForeground(SystemColor.desktop);
jRadioButton2.setFont(new Font("Dialog", 1, 12));
jLabel5.setText("Plain text");
jLabel5.setBounds(new Rectangle(195, 170, 70, 20));
jLabel5.setForeground(Color.magenta);
jLabel5.setFont(new Font("Dialog", 3, 13));
jLabel6.setText("Key");
jLabel6.setBounds(new Rectangle(205, 225, 35, 15));
jLabel6.setForeground(Color.magenta);
jLabel6.setFont(new Font("Dialog", 3, 13));
jLabel7.setText("Ciphertext");
jLabel7.setBounds(new Rectangle(185, 255, 65, 15));
jLabel7.setForeground(Color.magenta);
jLabel7.setFont(new Font("Dialog", 3, 13));
plainText.setBounds(new Rectangle(270, 165, 200, 35));
plainText.setSelectedTextColor(SystemColor.desktop);
// plainText.setForeground(SystemColor.desktop);
plainText.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
key.setBounds(new Rectangle(270, 220, 200, 20));
key.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
//key.setForeground(SystemColor.desktop);
cipherText.setBounds(new Rectangle(270, 255, 200, 20));
cipherText.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
//cipherText.setForeground(SystemColor.desktop);
jRadioButton3.setText("Transposition Cipher ");
jRadioButton3.setBounds(new Rectangle(205, 85, 170, 25));
jRadioButton3.setFont(new Font("Dialog", 1, 12));
jRadioButton3.setForeground(SystemColor.desktop);
this.getContentPane().add(jRadioButton3, null);
this.getContentPane().add(cipherText, null);
this.getContentPane().add(key, null);
this.getContentPane().add(plainText, null);
this.getContentPane().add(jLabel7, null);
this.getContentPane().add(jLabel6, null);
this.getContentPane().add(jLabel5, null);
this.getContentPane().add(jRadioButton2, null);
this.getContentPane().add(jLabel1, null);
this.getContentPane().add(jRadioButton1, null);
this.getContentPane().add(Decryption, null);
this.getContentPane().add(Encryption, null);
f= new Font("Dialog",Font.BOLD,12);
radioGroup= new ButtonGroup();
radioGroup.add(jRadioButton1);
radioGroup.add(jRadioButton2);
radioGroup.add(jRadioButton3);
jRadioButton1.addItemListener(this);
jRadioButton2.addItemListener(this);
jRadioButton3.addItemListener(this);
}
public void itemStateChanged(ItemEvent re)
{
String s;
char h;
JRadioButton r;
r=(JRadioButton)re.getSource();
s=r+"";
System.out.println(""+s);
int index=s.indexOf("text=");
h=s.charAt(index+5);
System.out.println(""+h);
String et=plainText.getText();
if(h=='R'){
R_flag=1;
System.out.println("Rail cipher");
}
else if(h=='V'){
V_flag=1;
System.out.println("Vigener cipher");
}
else if(h=='T'){
T_flag=1;
System.out.println("Transposition cipher");
}
}
public void actionPerformed(ActionEvent e)
{
JButton b;
String str,kt,pt,t="",tt="",st,stt;
char ch,c,cc;
pt=plainText.getText();
kt=key.getText();
b=(JButton)e.getSource();
str=b+"";
ch=str.charAt(21);
int res=str.indexOf("text=");
ch=str.charAt(res+5);
System.out.println(""+ch);
switch (ch)
{
case 'E':
int plenght;
en_flag=1;
if (R_flag==1&& en_flag==1){
char len=kt.charAt(0);
for(int i=0; i<pt.length()-1; i+=len){
c=pt.charAt(i);
st=c+"";
t.concat(st);
}
for(int j=1; j<pt.length()-1;j+=len){
cc=pt.charAt(j);
stt=cc+"";
t.concat(stt);
}
cipherText.setText(t);
}
else if(T_flag==1 && en_flag==1){
int cn=1;String mtt="";
for(int i=0; i<kt.length()-1;){
char no=kt.charAt(i);
if(no>cn){
i++;
no=kt.charAt(i);
}
else if (no ==i){
char me= pt.charAt(no);
String mt=me+"";
mtt.concat(mt);
cn++;
}
cipherText.setText(mtt);
}
}
else if(V_flag==1&& en_flag==1){
int count=0;
char noo=kt.charAt(count);
String y;
y=noo+"";
while(kt.length()!=pt.length())
kt.concat(y);
count++;
}
for(int i=0; i<pt.length()-1; i++){
for(int j=0;j<kt.length()-1;j++){
char p=pt.charAt(j);
char k= kt.charAt(i);
int np =pt.indexOf(p);
int nk =pt.indexOf(k);
for(int x=0; x<25;x++){
for (int z=0; z<25; z++){
while(z==np && x==nk)
cipherText.setText(Keyword[x][z]);
}
}
}
}
break;
case 'D':
de_flag=1;
if(de_flag==1)
cipherText.setText(pt);
break;
}
}
public static void main(String[] args)
{
security f2=new security();
}
}
i hope u can help me please
This post has been edited by queen_night: 23 November 2007 - 03:45 PM

New Topic/Question
Reply




MultiQuote





|