置换加密算法
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MainFrame extends JFrame{//置换加密算法 private panel1 = null ;
private JPanel panel2 = null ;
private JPanel panel11 = null ;
private JPanel panel12 = null ;
private JPanel panel13 = null ;
private JPanel panel14 = null ;
private JPanel panel15 = null ;
private JPanel panel21 = null ;
private JPanel panel22 = null ;
private JPanel panel23 = null ;
private JPanel panel24 = null ;
private JPanel panel25 = null ;
private JLabel label1 = null ;
private JLabel label2 = null ;
private JLabel label3 = null ;
private JLabel label4 = null ;
private JLabel label5 = null ;
private JLabel label6 = null ;
private JLabel label7 = null ;
private JLabel label8 = null ;
private JButton button1=null ;
private JButton button2=null ;
private JButton button3=null ;
private JButton button4=null ;
private JTextArea ta1 = null ;
private JTextArea ta2 = null ;
private JTextArea ta3 = null ;
private JTextArea ta4 = null ;
private JTextField tf1 = null ;
private JTextField tf2 = null ;
private Container container ;
private String text =null ; //用于明文
private String key1=null ; //用于密钥1
private String cipher =null ; //用于密文 private String key2=null ; //用于密钥2
public MainFrame () {
container = this .getContentPane();
label1=new JLabel(" 置换加密" );
label2=new JLabel(" 置换解密" );
label3=new JLabel(" 明文:" );
label4=new JLabel(" 密钥:" );
label5=new JLabel(" 密文:" );
label6=new JLabel(" 密文:" );
label7=new JLabel(" 密钥:" );
label8=new JLabel(" 明文:" );
button1 = new JButton(" 加密" );
button2 = new JButton(" 清空" );
button3 = new JButton(" 解密" );
button4 = new JButton(" 清空" );
button1.addActionListener(new ActionLis());
button2.addActionListener(new ActionLis()); button3.addActionListener(new ActionLis()); button4.addActionListener(new ActionLis()); ta1= new JTextArea(3, 15); ta2= new JTextArea(3, 15); ta3= new JTextArea("",3, 15); ta4= new JTextArea("",3, 15); ta3.setLineWrap(true ); ta3.setLineWrap(true ); tf1 = new JTextField(10); tf2 = new JTextField(10); panel1 = new JPanel(); panel2 = new JPanel(); panel11 = new JPanel(); panel12 = new JPanel(); panel13 = new JPanel(); panel14 = new JPanel(); panel15 = new JPanel(); panel21 = new JPanel(); panel22 = new JPanel(); panel23 = new JPanel(); panel24 = new JPanel(); panel25 = new JPanel(); panel11.add(label1); panel12.add(label3); panel12.add(ta1); panel13.add(label4); panel13.add(tf1); panel14.add(button1); panel14.add(button2); panel15.add(label5); panel15.add(ta3); panel1.add(panel11); panel1.add(panel12); panel1.add(panel13); panel1.add(panel14); panel1.add(panel15); panel21.add(label2); panel22.add(label6); panel22.add(ta2); panel23.add(label7); panel23.add(tf2); panel24.add(button3); panel24.add(button4); panel25.add(label8); panel25.add(ta4); panel2.add(panel21); panel2.add(panel22); panel2.add(panel23); panel2.add(panel24); panel2.add(panel25); panel1.setLayout(new GridLayout(5, 1)); panel2.setLayout(new GridLayout(5, 1)); container .add(panel1); container .add(panel2); container .setLayout(new GridLayout(1, 2));
} this .setTitle(" 置换加密解密" ); this .setSize(600, 500); this .setVisible(true ); } class ActionLis implements ActionListener{ public void actionPerformed(ActionEvent e) { if (e.getSource() == button1) { text =ta1.getText(); key1=tf1.getText(); Crypt c =new Crypt(text , key1); ta3.setText(c.getcrypt()); } else if (e.getSource() == button2) { ta1.setText("" ); tf1.setText("" ); ta3.setText("" ); } else if (e.getSource() == button3){ cipher =ta2.getText(); key2=tf2.getText(); Decrypt d =new Decrypt(cipher , key2); ta4.setText(d.gettext()); } else { ta2.setText("" ); tf2.setText("" ); ta4.setText("" ); } } } public static void main(String[] args) { new MainFrame(); }
public class Crypt {
private String text , key , ctext , otext ;
private int ctextl , keyl ;
private char [] ch1, ch3, ch4;
private int [] in1;
private char [][] ch2;
private int ch2r ;
public Crypt(String text,String key) {
this . text =text;
this . key =key;
String ctext=text.replaceAll(" ", "" ); //消去明文中空格 ch1=ctext.toCharArray();
ctextl =ctext.length();
keyl =key.length();
if (ctextl %keyl ==0)//设置二维数组行大小
{ch2r =ctextl /keyl ;}
else
{ch2r =ctextl /keyl +1;}
ch2=new char [ch2r ][keyl ];
for (int i=0,j=0;i
{ch2[i][k]=ch1[j];
j++;}}
in1=new int [keyl ];
Keys x=new Keys(key);
in1=x.getkey();
ch4=new char [ch2r *keyl ];//存储输出的字符数组
for (int i=0,k=0;i
{
for (int j=0;j
ch4[k]=ch2[j][in1[i]];
k++;
}
}
otext =String.valueOf (ch4);
}
String getcrypt()
{
return otext ;
}
}
public class Decrypt {
private String cipher , key , otext ;
private int cipherl , keyl ;
private char [] ch1, ch3, ch4;
private char [][] ch2;
private int ch2r ;
private int [] in ;
public Decrypt(String cipher,String key) {
this . cipher =cipher;
this . key =key;
cipherl =cipher.length();
keyl =key.length();
ch1=cipher.toCharArray();
if (cipherl %keyl ==0)//设置二维数组行大小
{ch2r =cipherl /keyl ;}
else
{ch2r =cipherl /keyl +1;}
in =new int [keyl ];
Keys x=new Keys(key);
in =x.getkey();
/*ch3=key.toCharArray();
for(int i=0;i
{
in[i]=Integer.parseInt(String.valueOf(ch3[i])); }*/
ch2=new char [ch2r ][keyl ];
for (int i=0,k=0;i
{
for (int j=0;j
ch2[j][in [i]]=ch1[k];
k++;
}
}
ch4=new char [ch2r *keyl ];
for (int i=0,k=0;i
{
for (int j=0;j
ch4[k]=ch2[i][j];
k++;
}
}
otext =String.valueOf (ch4);
}
String gettext()
{
return otext ;
}
}
public class Keys {
private String key ;
private char [] ch1, ch2;
private int [] in ;
private int keyl ;
public Keys(String key) {
ch1=key.toCharArray();
ch2=key.toCharArray();
keyl =key.length();
in =new int [keyl ];
char temp ;
for (int i=0;i
int k=i;
for (int j=i+1;ji)
{ temp=ch1[i];
ch1[i]=ch1[k];
ch1[k]=temp;}
}
for (int i=0;i
for (int j=0;j
in [i]=j;
else {};}
}
}
int [] getkey()
{
return in ;
}
}
加密
输入明文:we set the time at six aclock 密钥:date
预计结果:etitacwttaxoeeeil
解密
输入密文:etitacwttaxoeeeil
密钥:date
解密预计结果:
wesetthetimeatsixaclock