java 的DES加密问题

问题描述

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);}}这只是代码上半部分,由于受字数限制,分了两部分,敬请谅解。
解决方案三:
第一个回复贴就是代码上半部分,谢谢了

时间: 2024-10-01 00:26:44

java 的DES加密问题的相关文章

java 的DES加密问题,请大家解决一下,就是输出密文只有15位,还有帮我修改一下代码,可以达到每轮输出,谢谢好心的人了啊。

问题描述 java的DES加密问题,请大家解决一下,就是输出密文只有15位,还有帮我修改一下代码,可以达到每轮输出,谢谢好心的人了啊.importjavax.swing.*;importjava.awt.event.*;importjava.awt.*;[size=10px]classmyframeextendsJFrameimplementsActionListener{JTextAreainput;JTextAreamiyue;JTextAreaoutput;JLabell1,l2,l3;J

9999:谁有c++版des加密源代码 可以跟java版des加密库匹配 可以互相加解密

问题描述 9999:谁有c++版des加密源代码可以跟java版des加密库匹配可以互相加解密急需:万分感谢!本人在网上已经找了很多个版本,都无法跟java版des库加密出来的结果一致.最好的一个版本是:(40字节的明文(如:100@05963010591062666@013001791717991233)8位的密钥)c++加密出来密文是40个字节.java加密出来的密文是48个字节.前40个字节都是一致的.java加密调用代码如下:[DESKeySpecdks=newDESKeySpec(ra

Java/Android DES加密

问题描述 DES的简单封装,适用于Java和Android.先简单定义几个常量/***DES工具类*@authorunicorn*@version@2015年3月10日下午3:45:05*/publicclassDES{//初始化向量,必须八位privatestaticbyte[]iv={1,2,3,4,5,6,7,8};staticfinalStringDES="DES";staticfinalStringDES_TRANSFORMATION="DES/CBC/PKCS5P

兼容PHP和Java的des加密解密代码分享_php实例

php代码: <?php class DES { var $key; var $iv; //偏移量 function DES($key, $iv=0) { $this->key = $key; if($iv == 0) { $this->iv = $key; } else { $this->iv = $iv; } } //加密 function encrypt($str) { $size = mcrypt_get_block_size ( MCRYPT_DES, MCRYPT_MO

java使用des加密解密示例分享_java

复制代码 代码如下: import java.security.Key;import java.security.SecureRandom;import java.security.spec.AlgorithmParameterSpec; import javax.crypto.Cipher;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;import javax.crypto.spec.IvPar

一个java的DES加解密类转换成C#

原文:一个java的DES加解密类转换成C# 一个java的des加密解密代码如下: //package com.visionsky.util; import java.security.*; //import java.util.regex.Pattern; //import java.util.Hashtable; import javax.crypto.*; import javax.crypto.spec.*; import sun.misc.*; /** * des加密解密 */ pu

Java实现DES加解密算法解析_java

本文实例讲述了Java实现DES加解密算法解析.分享给大家供大家参考,具体如下:   简介: 数据加密算法(Data Encryption Algorithm,DEA)是一种对称加密算法,很可能是使用最广泛的密钥系统,特别是在保护金融数据的安全中,最初开发的DEA是嵌入硬件中的.通常,自动取款机(Automated Teller Machine,ATM)都使用DEA.它出自IBM的研究工作,IBM也曾对它拥有几年的专利权,但是在1983年已到期后,处于公有范围中,允许在特定条件下可以免除专利使用

C# des加密解密

问题描述 java的DES加密解密转成C#的des加密解密java的DES加密解密代码/****使用DES加密与解密,可对byte[],String类型进行加密与解密密文可使用String,byte[]存储.**方法:voidgetKey(StringstrKey)从strKey的字条生成一个Key*setKey()从默认defaultKey的字条生成一个Key*StringgetEncString(StringstrMing)对strMing进行加密,返回String密文String*getD

PHP、Java des加密解密实例

  这篇文章主要介绍了PHP.Java des加密解密实例,des加密是对称加密中在互联网应用的比较多的一种加密方式,本文分别给出了PHP和JAVA版本的实现代码,需要的朋友可以参考下 des加密是对称加密中在互联网应用的比较多的一种加密方式,php 通过mcrypt扩展库来支持des加密,要在Php中使用des加密,需要先安装mcrypt扩展库 下面是加密解密的实例 代码如下: $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_