您好,欢迎访问三七文档
importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclassMainFrameextendsJFrame{//置换加密算法privateJPanelpanel1=null;privateJPanelpanel2=null;privateJPanelpanel11=null;privateJPanelpanel12=null;privateJPanelpanel13=null;privateJPanelpanel14=null;privateJPanelpanel15=null;privateJPanelpanel21=null;privateJPanelpanel22=null;privateJPanelpanel23=null;privateJPanelpanel24=null;privateJPanelpanel25=null;privateJLabellabel1=null;privateJLabellabel2=null;privateJLabellabel3=null;privateJLabellabel4=null;privateJLabellabel5=null;privateJLabellabel6=null;privateJLabellabel7=null;privateJLabellabel8=null;privateJButtonbutton1=null;privateJButtonbutton2=null;privateJButtonbutton3=null;privateJButtonbutton4=null;privateJTextAreata1=null;privateJTextAreata2=null;privateJTextAreata3=null;privateJTextAreata4=null;privateJTextFieldtf1=null;privateJTextFieldtf2=null;privateContainercontainer;privateStringtext=null;//用于明文privateStringkey1=null;//用于密钥1privateStringcipher=null;//用于密文privateStringkey2=null;//用于密钥2publicMainFrame(){container=this.getContentPane();label1=newJLabel(置换加密);label2=newJLabel(置换解密);label3=newJLabel(明文:);label4=newJLabel(密钥:);label5=newJLabel(密文:);label6=newJLabel(密文:);label7=newJLabel(密钥:);label8=newJLabel(明文:);button1=newJButton(加密);button2=newJButton(清空);button3=newJButton(解密);button4=newJButton(清空);button1.addActionListener(newActionLis());button2.addActionListener(newActionLis());button3.addActionListener(newActionLis());button4.addActionListener(newActionLis());ta1=newJTextArea(3,15);ta2=newJTextArea(3,15);ta3=newJTextArea(显示解密结果,3,15);ta4=newJTextArea(显示解密结果,3,15);ta3.setLineWrap(true);ta3.setLineWrap(true);tf1=newJTextField(10);tf2=newJTextField(10);panel1=newJPanel();panel2=newJPanel();panel11=newJPanel();panel12=newJPanel();panel13=newJPanel();panel14=newJPanel();panel15=newJPanel();panel21=newJPanel();panel22=newJPanel();panel23=newJPanel();panel24=newJPanel();panel25=newJPanel();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(newGridLayout(5,1));panel2.setLayout(newGridLayout(5,1));container.add(panel1);container.add(panel2);container.setLayout(newGridLayout(1,2));this.setTitle(置换加密解密);this.setSize(600,500);this.setVisible(true);}classActionLisimplementsActionListener{publicvoidactionPerformed(ActionEvente){if(e.getSource()==button1){text=ta1.getText();key1=tf1.getText();Cryptc=newCrypt(text,key1);ta3.setText(c.getcrypt());}elseif(e.getSource()==button2){ta1.setText();tf1.setText();ta3.setText();}elseif(e.getSource()==button3){cipher=ta2.getText();key2=tf2.getText();Decryptd=newDecrypt(cipher,key2);ta4.setText(d.gettext());}else{ta2.setText();tf2.setText();ta4.setText();}}}publicstaticvoidmain(String[]args){newMainFrame();}}加密publicclassCrypt{privateStringtext,key,ctext,otext;privateintctextl,keyl;privatechar[]ch1,ch3,ch4;privateint[]in1;privatechar[][]ch2;privateintch2r;publicCrypt(Stringtext,Stringkey){this.text=text;this.key=key;Stringctext=text.replaceAll(,);//消去明文中空格ch1=ctext.toCharArray();ctextl=ctext.length();keyl=key.length();if(ctextl%keyl==0)//设置二维数组行大小{ch2r=ctextl/keyl;}else{ch2r=ctextl/keyl+1;}ch2=newchar[ch2r][keyl];for(inti=0,j=0;ich2r;i++)//明文存入二维数组中{for(intk=0;kkeyl&&jctextl;k++){ch2[i][k]=ch1[j];j++;}}in1=newint[keyl];Keysx=newKeys(key);in1=x.getkey();ch4=newchar[ch2r*keyl];//存储输出的字符数组for(inti=0,k=0;ikeyl;i++){for(intj=0;jch2r&&k(ch2r*keyl);j++){ch4[k]=ch2[j][in1[i]];k++;}}otext=String.valueOf(ch4);}Stringgetcrypt(){returnotext;}}解密publicclassDecrypt{privateStringcipher,key,otext;privateintcipherl,keyl;privatechar[]ch1,ch3,ch4;privatechar[][]ch2;privateintch2r;privateint[]in;publicDecrypt(Stringcipher,Stringkey){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=newint[keyl];Keysx=newKeys(key);in=x.getkey();/*ch3=key.toCharArray();for(inti=0;ikeyl;i++){in[i]=Integer.parseInt(String.valueOf(ch3[i]));}*/ch2=newchar[ch2r][keyl];for(inti=0,k=0;ikeyl;i++){for(intj=0;jch2r&&kcipherl;j++){ch2[j][in[i]]=ch1[k];k++;}}ch4=newchar[ch2r*keyl];for(inti=0,k=0;ich2r;i++){for(intj=0;jkeyl&&k(ch2r*keyl);j++){ch4[k]=ch2[i][j];k++;}}otext=String.valueOf(ch4);}Stringgettext(){returnotext;}}publicclassKeys{privateStringkey;privatechar[]ch1,ch2;privateint[]in;privateintkeyl;publicKeys(Stringkey){ch1=key.toCharArray();ch2=key.toCharArray();keyl=key.length();in=newint[keyl];chartemp;for(inti=0;ikeyl-1;i++){intk=i;for(intj=i+1;jkeyl;j++)if(c
本文标题:置换加密算法
链接地址:https://www.777doc.com/doc-5423080 .html