| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 820 人关注过本帖
标题:帮忙看一下,越界异常,汗!毛病在哪啊?
只看楼主 加入收藏
lulu820830
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2008-3-24
收藏
 问题点数:0 回复次数:3 
帮忙看一下,越界异常,汗!毛病在哪啊?
下面的这段程序是DES加密算法,但是老是出问题:
public class des2
{
                        int[] IP_1={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[] IP={58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,462,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[] pc_1={57,49,41,33,25,17,9,1,58,50,42,34,26,18,10,2,59,51,43,35,27, 9,11,3,60,52,44,36,
                                 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[] pc_2={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[] movebyte={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,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[] 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[][][] s={{{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}},  //s1
                         {{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}},  //s2             
                               {{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}},  //s3
                               {{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}},  //s4
                               {{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}},  //s5
                               {{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}},  //s6
                         {{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}},  //s7
                         {{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, 4,7,4,10,8,13,15,12,9,0,3,5,6,11}}}; //s8
                        char[] read(String s)    //明文密文存入数组
                        {
                                    char[] a=new char[8];
                                    for(int i=0;i<8;i++)      
                               {
                                       a[i]=s.charAt(i);
                               }
                               return a;
                        }
                    
                String jmsf(char[] y,char[] m)    //char[] jmsf(char[] y,char[] m)
                        {
                                    int[] erkey=new int[64];String zf="";
                                    int[] erdata=new int[64];
                                    int[] dataresult=new int[64];
                                    int[] dataip=new int[64];
                                    int[][] ki=new int[16][48];
                                    int[] resultzf=new int[8];
                                    erkey=varyer(y);
                                    erdata=varyer(m);
                                    dataip=varyip(erdata);
                                    ki=varykey(erkey);
                                    dataresult=varyjm(dataip,ki);
                                    //char[] zf=new char[8];
                                    for(int i=0;i<8;i++)
                                    {
                                                System.arraycopy(dataresult, i*8,resultzf, 0 , 8);
                                                int sum=resultzf[0]*128+resultzf[1]*64+resultzf[2]*32+resultzf[3]*16+resultzf[4]*8+resultzf[5]*4+resultzf[6]*2+resultzf[7];
                                                zf+=sum;//System.arraycopy(sum, 0,zf,i , 1);
                                    }
                                    return zf;
                        }
                        int[] varyjm(int[] x,int[][] t)
                        {
                                    int[] datae=new int[48];
                                    int[] f=new int[48];
                                    int[] F=new int[32];
                                    int[] R=new int[32];
                                    int[] L=new int[32];
                                    int[] R1=new int[32];
                                    int[] L1=new int[32];
                                    int[] result=new int[64];
                                    for(int i=0;i<32;i++)
                                    {
                                                L[i]=x[i];
                                                R[i]=x[i+32];
                                    }
                                    L1=R;
                                    R1=varyYH(L,F);
                                    for(int i=0;i<16;i++)     
                                    {
                                            L1=R1;
                                            datae=varye(R);
                                            f=varyyh(datae,t[i]);
                                            F=varys(f,s[i]);
                                            R1=varyYH(L1,F);
                                    }
                                    int[] L0=new int[32];
                                    int[] R0=new int[32];
                                    L0=R1;R0=L1;
                                    for(int i=0;i<32;i++)
                                    {
                                                result[i]=L0[i];
                                                result[i+32]=R0[i];
                                    }
                                    int[] theresult=new int[64];
                                    for(int i=0;i<64;i++)
                                    {
                                                theresult[i]=result[IP_1[i]-1];
                                    }
                                    return theresult;
                        }
                        int[] varyYH(int[] s,int[] t)
                        {
                                    int[] x=new int[32];
                                    for(int i=0;i<32;i++)
                                    {
                                            if(s[i]==t[i]) x[i]=0;
                                            else x[i]=1;
                                    }
                                    return x;
                        }
                        int[] varys(int[] s,int[][] t)
                        {
                                    int[] x=new int[8];
                                    int[] y=new int[4];
                                    int[] z=new int[32];
                                    int hang;
                                    int lie;
                                    int dian;
                                    for(int i=0;i<8;i++)
                                    {
                                                System.arraycopy(s, i*6, x, 0, 6);
                                                hang=x[5]+x[0]*2;
                                                lie=x[4]+x[3]*2+x[2]*4+x[1]*8;
                                                dian=t[hang][lie];
                                                y[3]=dian%2; dian=dian/2;
                                                y[2]=dian%2; dian=dian/2;
                                                y[1]=dian%2; dian=dian/2;
                                                y[0]=dian%2;
                                              System.arraycopy(y, 0, z, i*4, 4);    
                                    }
                                    int[] m=new int[32];
                                    for(int i=0;i<32;i++)
                                    {
                                                m[i]=z[p[i]-1];
                                    }
                                    return m;
                        }
                        int[] varyyh(int[] s,int[] t)
                        {
                                    int[] x=new int[48];
                                    for(int i=0;i<48;i++)
                                    {
                                                if(s[i]==t[i]) x[i]=0;
                                                else x[i]=1;
                                    }    
                                    return x;        
                        }
                        int[] varye(int[] s)
                        {
                                    
                                    int[] datae=new int[48];
                                    for(int i=0;i<48;i++)
                                    {
                                                datae[i]=s[E[i]-1];
                                    }
                                    return datae;
                        }
                        int[][] varykey(int[] s)
                        {
                                    int[] k=new int[56];
                                    int[] m=new int[56];
                                    for(int i=0;i<56;i++)
                                    {
                                                m[i]=s[pc_1[i]-1];
                                    }
                                    int[] c=new int[28];
                                    int[] d=new int[28];
                                    for(int i=0;i<28;i++)
                                    {
                                                c[i]=m[i];
                                                d[i]=m[i+28];
                                    }
                                    int[] C=new int[28];
                                    int[] D=new int[28];
                                    int[][] K=new int[16][48];
                                    for(int i=0;i<16;i++)
                                   {
                                               if(movebyte[i]==1)            //左移一位
                                               {
                                                       for(int j=0;j<27;j++)
                                                       {
                                                           C[j]=c[j+1];
                                                           D[j]=d[j+1];
                                                       }
                                                       C[27]=c[0];
                                                       D[27]=d[0];
                                               }
                                               if(movebyte[i]==2)                //左移两位
                                               {
                                                       for(int j=0;j<26;j++)
                                                       {
                                                               C[j]=c[j+2];
                                                               D[j]=d[j+2];
                                                       }
                                                 C[26]=c[0];
                                                       C[27]=c[1];
                                                       D[26]=d[0];
                                                       D[27]=d[1];
                                               }
                                for(int n=0;n<28;n++)               //连接高低密钥组成56位
                                               {
                                                       k[n]=c[n];
                                                       k[n+28]=d[n];
                                               }
                                               
                                               for(int j=0;j<48;j++)
                                               {
                                                       K[i][j]=k[pc_2[j]-1];    
                                               }
                                   }
                                   return K;
                        }
                        int[] varyip(int[] s)
                        {
                                    int[] m=new int[64];
                                    for(int i=1;i<=64;i++)
                                    {
                                                m[i]=s[IP[i]];
                                    }
                                    return m;
                        }
                        int[] varyer(char[] s)
                        {
                                    int[] zcz=new int[64];   //存对应8个字符的64位二进制
                              for(int i=0;i<8;i++)
                              {
                                      for(int j=0;j<8;j++)
                                      {
                                          zcz[(i*8)+7-j]=s[i]%2;
                                          s[i]/=2;
                                      }
                               }
                               return zcz;
                        }
                        
                        public static void main(String[] args)
                        {
                                String zj="Asdfrthj";
                              String zj2="12345678";
                              des2 Des=new des2();
                              char[] mw=Des.read(zj);
                              char[] my=Des.read(zj2);
                              String shuchu="";//char[] shuchu=new char[8];
                            shuchu=Des.jmsf(my,mw);
                            System.out.println(shuchu);
               }
}



每次编绎都能通过,运行就会出现下面的异常:
Exception in thread "main"java.lang.ArrayIndexOutOfBoundsException:462
        at des2.varyip(des2.java:212)
        at des2.jmsf(des2.java:43)
        at des2.main(des2.java:238)

[[it] 本帖最后由 lulu820830 于 2008-4-12 18:05 编辑 [/it]]
搜索更多相关主题的帖子: 毛病 越界 
2008-04-12 15:54
nima2004
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2007-11-26
收藏
得分:0 
for(int i=1;i<=64;i++){
    m[i]=s[IP[i]];
}
问题跟你另外的一个帖子差不多,
你可以看一下,IP.length是63,而且IP里边有好几个元素的值都大于62.
还有s[i].length是4, s.length 才是8.
希望我的发现对你有用。

[[it] 本帖最后由 nima2004 于 2008-4-12 21:45 编辑 [/it]]

我是新手,我谁都怕!
2008-04-12 21:31
freish
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1223
专家分:437
注 册:2007-6-1
收藏
得分:0 
眼睛都看花了……
2008-04-13 13:24
lulu820830
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2008-3-24
收藏
得分:0 
回复 2# 的帖子
谢谢,我太大意了,这么粗心,连赋值错了都没发现

生命里的所有,不过是从得到失的过程
2008-04-13 16:45
快速回复:帮忙看一下,越界异常,汗!毛病在哪啊?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017702 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved