问题描述
java的DES加密问题,请大家解决一下,就是输出密文只有15位,还有帮我修改一下代码,可以达到每轮输出,谢谢好心的人了啊。//ip置换publicStringBufferworkip(StringBufferok){StringBuffertemp=newStringBuffer();for(intj=0;j<64;j++){temp.append(ok.charAt(ip[j]-1));}returntemp;}//密钥生成publicStringBuffer[]key_out(StringBufferok2){Stringc=newString();Stringd=newString();StringBuffer[]key=newStringBuffer[17];for(inti=0;i<key.length;i++){key[i]=newStringBuffer();}//置换1StringBuffertempok2=newStringBuffer();for(intj=0;j<28;j++){tempok2.append(ok2.charAt(zhihuanc[j]-1));}for(intj=0;j<28;j++){tempok2.append(ok2.charAt(zhihuand[j]-1));}ok2=tempok2;//完成c=ok2.substring(0,28);d=ok2.substring(28,56);StringBuffercc=newStringBuffer();StringBufferdd=newStringBuffer();cc.append(c);dd.append(d);inttime=0;while(time<16){if(time==0||time==1||time==8||time==15){chartempc=cc.charAt(0);chartempd=dd.charAt(0);cc.deleteCharAt(0);dd.deleteCharAt(0);cc.append(tempc);dd.append(tempd);}else{char[]tempc=newchar[2];char[]tempd=newchar[2];tempc[0]=cc.charAt(0);tempc[1]=cc.charAt(1);tempd[0]=dd.charAt(0);tempd[1]=dd.charAt(1);cc.delete(0,2);dd.delete(0,2);cc.append(tempc);dd.append(tempd);}//循环左移完成StringBuffersb=newStringBuffer();sb.append(cc).append(dd);for(intj=0;j<48;j++){key[time+1].append(sb.charAt(zhihuan2[j]-1));}//置换2time++;sb=null;}returnkey;}//e扩展publicStringe_function(Strings){StringBuffertemp=newStringBuffer();for(intj=0;j<48;j++){temp.append(s.charAt(e[j]-1));}returntemp.toString();}//s盒publicStrings_box(Strings){String[]s1=newString[8];inti=0;intj=0;int[]col=newint[8];int[]row=newint[8];while(i<48){s1[j]=s.substring(i,i+6);i=i+6;j++;}for(i=0;i<8;i++){StringBuffersb1=newStringBuffer();StringBuffersb2=newStringBuffer();sb1.append(s1[i].charAt(0)).append(s1[i].charAt(5));sb2.append(s1[i].substring(1,5));row[i]=Integer.parseInt(sb1.toString(),2);col[i]=Integer.parseInt(sb2.toString(),2);}i=row[0]*16+col[0];j=S1[i];s1[0]=Integer.toBinaryString(j);i=row[1]*16+col[1];j=S2[i];s1[1]=Integer.toBinaryString(j);i=row[2]*16+col[2];j=S3[i];s1[2]=Integer.toBinaryString(j);i=row[3]*16+col[3];j=S4[i];s1[3]=Integer.toBinaryString(j);i=row[4]*16+col[4];j=S5[i];s1[4]=Integer.toBinaryString(j);i=row[5]*16+col[5];j=S6[i];s1[5]=Integer.toBinaryString(j);i=row[6]*16+col[6];j=S7[i];s1[6]=Integer.toBinaryString(j);i=row[7]*16+col[7];j=S8[i];s1[7]=Integer.toBinaryString(j);for(i=0;i<8;i++){if(s1[i].length()<4){intb=4-s1[i].length();for(;b>0;b--){s1[i]="0"+s1[i];}}}StringBuffersb3=newStringBuffer();for(i=0;i<8;i++){sb3.append(s1[i]);}returnsb3.toString();}//置换ppublicStringp_function(Strings){StringBuffertemp=newStringBuffer();for(intj=0;j<32;j++){temp.append(s.charAt(p[j]-1));}returntemp.toString();}//函数FpublicStringBufferkey_function(Stringr,StringBufferk){r=e_function(r);r=xor(r,k);r=s_box(r);r=p_function(r);StringBuffertemp=newStringBuffer();temp.append(r);returntemp;}//异或函数publicStringxor(Strings1,StringBuffersb){Strings2=sb.toString();StringBuffers3=newStringBuffer();char[]c1=s1.toCharArray();char[]c2=s2.toCharArray();for(inti=0;i<c1.length;i++){inta=Integer.parseInt(Character.toString(c1[i]))^Integer.parseInt(Character.toString(c2[i]));s3.append(a);}returns3.toString();}//ip逆置换publicStringreip_function(Strings){StringBuffertemp=newStringBuffer();for(intj=0;j<64;j++){temp.append(s.charAt(reip[j]-1));}returntemp.toString();}publicvoidencrypt(){Stringmessage=input.getText();//获得inputchar[]s1=message.toCharArray();StringBufferok=newStringBuffer();//ok是明文(密文)位表示int[]i=newint[65];for(inta=0;a<s1.length;a++){i[a]=Integer.parseInt(Character.toString(s1[a]),16);//16进制Stringtemp=Integer.toBinaryString(i[a]);//2进制if(temp.length()<4){intb=4-temp.length();for(;b>0;b--){temp="0"+temp;}}ok.append(temp);}//ip置换ok=workip(ok);//获得密钥Stringkey=miyue.getText();s1=key.toCharArray();StringBufferok2=newStringBuffer();//ok2是密钥位表示int[]ii=newint[65];for(inta=0;a<s1.length;a++){ii[a]=Integer.parseInt(Character.toString(s1[a]),16);Stringtemp=Integer.toBinaryString(ii[a]);if(temp.length()<4){intb=4-temp.length();for(;b>0;b--){temp="0"+temp;}}ok2.append(temp);}//StringBuffer[]k=key_out(ok2);//密钥生成//Stringl=ok.substring(0,32);Stringr=ok.substring(32,64);for(inttime=0;time<15;time++){Stringtemp1=r;StringBuffertemp=key_function(r,k[time+1]);//函数Fr=xor(l,temp);l=temp1;}Stringtemp1=r;StringBuffertemp=key_function(r,k[16]);r=xor(l,temp);l=temp1;Stringfinish=r+l;finish=reip_function(finish);//变为16进制String[]t=newString[4];for(intx=0;x<4;x++){t[x]=newString();}t[0]=finish.substring(0,16);t[1]=finish.substring(16,32);t[2]=finish.substring(32,48);t[3]=finish.substring(48,64);StringBufferover=newStringBuffer();for(intx=0;x<4;x++){intnum=Integer.parseInt(t[x],2);over.append(Integer.toHexString(num));}output.setText(over.toString());this.validate();}//加密完成publicvoiddecrypt(){Stringmessage=input.getText();//获得inputchar[]s1=message.toCharArray();StringBufferok=newStringBuffer();//ok是明文(密文)位表示int[]i=newint[65];for(inta=0;a<s1.length;a++){i[a]=Integer.parseInt(Character.toString(s1[a]),16);//16进制Stringtemp=Integer.toBinaryString(i[a]);//2进制if(temp.length()<4){intb=4-temp.length();for(;b>0;b--){temp="0"+temp;}}ok.append(temp);}//ip置换ok=workip(ok);//获得密钥Stringkey=miyue.getText();s1=key.toCharArray();StringBufferok2=newStringBuffer();//ok2是密钥位表示int[]ii=newint[65];for(inta=0;a<s1.length;a++){ii[a]=Integer.parseInt(Character.toString(s1[a]),16);Stringtemp=Integer.toBinaryString(ii[a]);if(temp.length()<4){intb=4-temp.length();for(;b>0;b--){temp="0"+temp;}}ok2.append(temp);}//StringBuffer[]k=key_out(ok2);//密钥生成//Stringl=ok.substring(0,32);Stringr=ok.substring(32,64);for(inttime=0;time<15;time++){Stringtemp1=r;StringBuffertemp=key_function(r,k[16-time]);//函数Fr=xor(l,temp);l=temp1;}Stringtemp1=r;StringBuffertemp=key_function(r,k[1]);r=xor(l,temp);l=temp1;Stringfinish=r+l;finish=reip_function(finish);//变为16进制String[]t=newString[4];for(intx=0;x<4;x++){t[x]=newString();}t[0]=finish.substring(0,16);t[1]=finish.substring(16,32);t[2]=finish.substring(32,48);t[3]=finish.substring(48,64);StringBufferover=newStringBuffer();for(intx=0;x<4;x++){intnum=Integer.parseInt(t[x],2);over.append(Integer.toHexString(num));}output.setText(over.toString());this.validate();}}publicclassMySoftWare{publicstaticvoidmain
(String[]args){newmyframe("DESmadebycc");}}
这只是代码下半部分由于受字数限制,下半部分发另一个帖子,敬请谅解。
解决方案
解决方案二:
这只是代码上半部分,由于受字数限制,分了两部分,敬请谅解。importjavax.swing.*;importjava.awt.event.*;importjava.awt.*;[size=10px]classmyframeextendsJFrameimplementsActionListener{JTextAreainput;JTextAreamiyue;JTextAreaoutput;JLabell1,l2,l3;JButtonb1,b2;JPanelp1,p2,p3,p4;JScrollPanesp1,sp2,sp3;////int[]ip={58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,62,54,46,38,30,22,14,6,64,56,48,40,32,24,16,8,57,49,41,33,25,17,9,1,59,51,43,35,27,19,11,3,61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7};int[]reip={40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,34,2,42,10,50,18,58,26,33,1,41,9,49,17,57,25};int[]e={32,1,2,3,4,5,4,5,6,7,8,9,8,9,10,11,12,13,12,13,14,15,16,17,16,17,18,19,20,21,20,21,22,23,24,25,24,25,26,27,28,29,28,29,30,31,32,1};int[]p={16,7,20,21,29,12,28,17,1,15,23,26,5,18,31,10,2,8,24,14,32,27,3,9,19,13,30,6,22,11,4,25};int[]zhihuanc={57,49,41,33,25,17,9,1,58,50,42,34,26,18,10,2,59,51,43,35,27,19,11,3,60,52,44,36};int[]zhihuand={63,55,47,39,31,23,15,7,62,54,46,38,30,22,14,6,61,53,45,37,29,21,13,5,28,20,12,4};int[]zhihuan2={14,17,11,24,1,5,3,28,15,6,21,10,23,19,12,4,26,8,16,7,27,20,13,2,41,52,31,37,47,55,30,40,51,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32};int[]S1={14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13};int[]S2={15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9};int[]S3={10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12};int[]S4={7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14};int[]S5={2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3};int[]S6={12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8,9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13};int[]S7={4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12};int[]S8={13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11};publicmyframe(Strings){super(s);input=newJTextArea();miyue=newJTextArea();output=newJTextArea();l1=newJLabel("输入");l2=newJLabel("密钥");l3=newJLabel("输出");b1=newJButton("加密");b2=newJButton("解密");p1=(JPanel)this.getContentPane();p2=newJPanel();p3=newJPanel();p4=newJPanel();sp1=newJScrollPane(input);sp2=newJScrollPane(miyue);sp3=newJScrollPane(output);b1.addActionListener(this);b2.addActionListener(this);this.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});b1.setSize(30,50);b2.setSize(30,50);p2.setLayout(newGridLayout(2,1));p2.add(b1);p2.add(b2);p3.setLayout(newGridLayout(3,1));p3.add(l1);p3.add(l2);p3.add(l3);p4.setLayout(newGridLayout(3,1));p4.add(sp1);p4.add(sp2);p4.add(sp3);p1.setLayout(newGridLayout(1,5));p1.add(p2);p1.add(p3);p1.add(p4);this.setBounds(10,10,400,200);output.setEditable(false);this.setVisible(true);this.validate();}publicvoidactionPerformed(ActionEvente){if(e.getSource()==b1){//加密intlength=input.getText().length();intlength2=miyue.getText().length();booleanflag=true;booleanflag2=true;char[]temp=input.getText().toCharArray();char[]temp2=miyue.getText().toCharArray();for(inti=0;i<temp.length&&flag==true;i++){if((temp[i]>=48&&temp[i]<=57)||(temp[i]>=65&&temp[i]<=70)||(temp[i]>=97&&temp[i]<=102))flag=true;elseflag=false;}for(inti=0;i<temp2.length&&flag2==true;i++){if((temp2[i]>=48&&temp2[i]<=57)||(temp2[i]>=65&&temp2[i]<=70)||(temp2[i]>=97&&temp2[i]<=102))flag2=true;elseflag2=false;}if(length==16&&flag==true&&flag2==true&&length2==16){encrypt();}elseJOptionPane.showMessageDialog(null,"请输入16位16进制明文或密文或密钥!","错误提示",JOptionPane.ERROR_MESSAGE);}elseif(e.getSource()==b2){//解密intlength=input.getText().length();intlength2=miyue.getText().length();booleanflag=true;char[]temp=input.getText().toCharArray();for(inti=0;i<temp.length&&flag;i++){if((temp[i]>=48&&temp[i]<=57)||(temp[i]>=65&&temp[i]<=90)||(temp[i]>=97&&temp[i]<=122))flag=true;elseflag=false;}if(length==16&&flag&&length2==16){decrypt();}elseJOptionPane.showMessageDialog(null,"请输入16位16进制明文或密文或密钥!","错误提示",JOptionPane.ERROR_MESSAGE);}}这只是代码上半部分,由于受字数限制,分了两部分,敬请谅解。
解决方案三:
第一个回复贴就是代码上半部分,谢谢了