⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 new_des.java

📁 java的通讯程序(socket)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    	int bigright_new[]=new int[6];
    
    	//char[] bb=new char[2];
    	byte r0,r1,r2,r3;

    	//bb = bigright;
    	r0 = right[j++]; 
    	r1 = right[j++]; 
    	r2 = right[j++]; 
    	r3 = right[j++];
    	
    	//System.out.println( "r0==="+r0);
    	//System.out.println( "r1==="+r1);
    	//System.out.println( "r2==="+r2);
    	//System.out.println( "r3==="+r3);
    	//*bb++ = ((r3 & 0001) << 7) |//32
    	bigright_new[k++]=
    		((r3 & 0001) << 7)|
 			((r0 & 0370) >> 1)| //1 2 3 4 5
 			((r0 & 0030) >> 3);//4 5
    	//*bb++ = ((r0 & 0007) << 5) |//6 7 8
    	bigright_new[k++]= 
    		((r0 & 0007) << 5) |
 			((r1 & 0200) >> 3) |//9
 			((r0 & 0001) << 3) |//8
 			((r1 & 0340) >> 5);//9 10 11
    	//*bb++ = ((r1 & 0030) << 3) |//12 13
    	bigright_new[k++]=
    		((r1 & 0030) << 3) |
            ((r1 & 0037) << 1) |        //12 13 14 15 16
            ((r2 & 0200) >> 7);         //17
    	//*bb++ = ((r1 & 0001) << 7) |        //16
    	bigright_new[k++]= 
    		((r1 & 0001) << 7) |
            ((r2 & 0370) >> 1) |        //17 18 19 20 21
            ((r2 & 0030) >> 3);         //20 21
    	//*bb++ = ((r2 & 0007) << 5) |        //22 23 24
    	bigright_new[k++]= 
    		((r2 & 0007) << 5) |
            ((r3 & 0200) >> 3) |        //25
            ((r2 & 0001) << 3) |        //24
            ((r3 & 0340) >> 5);         //25 26 27
    	//*bb++ = ((r3 & 0030) << 3) |        //28 29
    	bigright_new[k++]= 
    		((r3 & 0030) << 3) |
            ((r3 & 0037) << 1) |        //28 29 30 31 32
            ((r0 & 0200) >> 7);         //1
            
        String new_bigright="";
        for(int m=0;m<6;m++){
        	right_new[m]=(byte)(bigright_new[m]);
        	//System.out.println("---^^^^^^--"+right_new[m]);
        	//new_bigright=new_bigright+String.valueOf(bigright_new[m]);
        }    
    	return right_new;
	}

	//contract f from 48 to 32 bits using 12-bit pieces into bytes
	public static byte[] contract(byte[] in48)
	{
    	char[] ii=new char[6];
    	byte[] cc=new byte[4];
    	byte i0, i1, i2, i3, i4, i5,i,j=0,m=0;

    	//i = in48;
    	//for(i=0;i<in48.length();i++){
    	//	ii[i]=in48.charAt(i);
    	//}
    
    	//i0 = *i++; i1 = *i++; i2 = *i++; i3 = *i++; i4 = *i++; i5 = *i++;
    	i0 = in48[j++]; 
    	i1 = in48[j++]; 
    	i2 = in48[j++]; 
    	i3 = in48[j++]; 
    	i4 = in48[j++]; 
    	i5 = in48[j++];
    	//c = out32;                  //do output a byte at a time
    	cc[m++] = s[0][07777 & ((i0 << 4) | ((i1 >> 4) & 017  ))];
    	cc[m++] = s[1][07777 & ((i1 << 8) | ( i2       & 0377 ))];
    	cc[m++] = s[2][07777 & ((i3 << 4) | ((i4 >> 4) & 017  ))];
    	cc[m++] = s[3][07777 & ((i4 << 8) | ( i5       & 0377 ))];
    	
    	//for(i=0;i<4;i++){
    	//	System.out.println("cc =="+cc[i]);
    	//}
    	return(cc);
	}

	//32-bit permutation at end of the f crypto function
	public static byte[] perm32(byte[] inblock)
	{
    	int j,i,m=0,n=0,pos=0,dif=0;
    	String buff_test="";
    	byte[] ob=new byte[4];
    	byte[] ib=new byte[4];
		// register int i;//rwo: unused
    	//char *ib, *ob;
    	byte[] q=new byte[4];
    	for(i=0;i<4;i++){
    		ib[i]=inblock[i];
    		//System.out.println("ib[i]=="+ib[i]);
    	}

    	//ob = outblock;//clear output block
    	ob[m++] = 0; ob[m++] = 0; ob[m++] = 0; ob[m++] = 0;
    	//ib = inblock;//ptr to 1st byte of input
    	m=0;
    	for (j = 0; j < 4; j++,n++)//for each input byte
    	{	
    		//for(dif=0;dif<4;dif++){
 			//	buff_test = buff_test+String.valueOf(p32[j][(ib[n] & 0377)+dif]);
 			//}
 			for(i=0;i<4;i++){
 				//q[i]=buff_test.charAt(i);
 				q[i]=(byte)(p32[j][(ib[n] & 0377)][i]);
 				//System.out.println("----"+q[i]);
 				ob[i] |= q[i];
 			}
 			//pos=0;
 			//ob = outblock;//and each output byte
 			//OR the 16 masks together
 			//ob[m++] |= q[pos++];
 			//ob[m++] |= q[pos++];
 			//ob[m++] |= q[pos++];
 			//ob[m++] |= q[pos++];
    	}
    	//for(i=0;i<4;i++){
    	//	System.out.println("ob[i]=="+ob[i]);
    	//}
    	return(ob);
	}


	//critical cryptographic trans 32 bits each index number of this iter
	public static byte[] f(byte[] right, int num)
	{
    	//char *kb, *rb, *bb;//ptr to key selection &c
    	int i=0;
    	byte[] f_test=new byte[4];
    	byte[] bigright=new byte[6];
    	String test_buf="";
    	//char[] bigright=new char[6];//right expanded to 48 bits
    	String result="";//expand(R) XOR keyselect[num]
    	byte[] preout=new byte[4];//result of 32-bit permutation
		byte[] fret=new byte[4];
		//System.out.println(" f()===> String right=["+right+"] num=["+num+"]");
		
		for(i=0;i<4;i++){
			f_test[i]=right[i];
			//System.out.print("["+f_test[i]+"]");
		}
		System.out.print("\n");
		
    	char[] kb=new char[6];
    	for(i=0;i<6;i++){
    		kb[i]=kn[num][i];	//fast version of iteration
    		//System.out.println("kb[0]=="+(byte)kn[num][i]);
    	}
		bigright=expand(f_test); //expand to 48 bits
		for(i=0;i<6;i++){
			//System.out.println("bigright=="+bigright[i]);
		}
		byte[] rb=new byte[6];
    	for(i=0;i<6;i++){
    		rb[i] = (byte)(bigright[i] ^ kb[i]);//expanded R XOR chunk of key
    		//System.out.println("---rb["+i+"]---"+rb[i]);
    	}
    	
    	preout=contract(rb);
    	fret=perm32(preout);
    	//rb = result;
    	/****
    	int[] rb=new int[6];
    	for(i=0;i<6;i++){
    		rb[i] = bb[i] ^ kb[i];//expanded R XOR chunk of key
    		//rb[i] = bb[i] ^ kb[i];
    		//rb[i] = bb[i] ^ kb[i];
    		//rb[i] = bb[i] ^ kb[i];
    		//rb[i] = bb[i] ^ kb[i];
    		//rb[i] = bb[i] ^ kb[i];
		}
    	for(int m=0;m<6;m++){
        	result=result+String.valueOf(rb[m]);
    	}    
    	preout=contract(result);//use S fns to get 32 bits
    	fret=perm32(preout);//and do final 32-bit perm
    	//System.out.println("--- f()-->return fret=["+fret+"]");
    	****/
    	return (fret);
	}
	
	
	//####################### permute() success !!! #####################
	//permute inblock with perm, result into outblock, 64 bits 2K bytes defining perm.
	public static byte[] permute(byte[] inblock,char perm[][][])
	{
     	int  i, j,m,len_p;
     	String permute_test="";
     	//char *ib, *ob;	//ptr to input or output block
     	byte[] p=new byte[8];
     	byte[] q=new byte[8];
     	byte ib[]=new byte[8];
     	byte ob[]=new byte[8];
     	//System.out.println("----"+len_p+"==permute() inblock=="+inblock); //---ok
     	for(i=0;i<8;i++){
     		ib[i]=inblock[i];
     		//System.out.println("permute()->ib-->["+i+"]"+"==="+ib[i]);
     	}
     	for(i=0;i<8;i++){
     		ob[i]=0;
     		//System.out.print(ob[i]);
     	}
     	//System.out.print("\n\n");

		for(j=0,i=0;j<16;j+=2,i++){
			//p=perm[j][(Integer.parseInt(inblock.substring(i,i+1)) >> 4) & 017];
			//System.out.println("ib["+ib[i]+"]");
			for(m=0;m<8;m++){
				//permute_test=String.valueOf(perm[j][((ib[i] >>4)& 017)]);
				//System.out.println("=====["+(byte)((ib[i]>>4)&017));
				p[m]=(byte)(perm[j][((ib[i]>>4)&017)][m]);
				//System.out.println("permute-->perm=[]"+(byte)(perm[j][((ib[i]>>4)&017)][m])+"--"+p[m]);
				//permute_test="";
				//permute_test=String.valueOf(perm[j+1][(ib[i] &017)]);
				q[m]=(byte)(perm[j+1][ib[i]&017][m]);
				
				//System.out.print(q[m]);
				//if(m==7)
				//	System.out.print("\n");
			}
			//p=perm[j][(ib[i] >> 4) & 017];
			//q=perm[j+1][ib[i] & 017];
			for(m=0;m<8;m++){
				ob[m] |= p[m] | q[m];
				//System.out.print(ob[m]);
				//if(m==7)
				//	System.out.print("\n");
			}//--------------------- here ok !!!!----------------------------
		}
		//String outblock=new String(ob);
		//System.out.println("===== permute ---test="+ outblock);
		return(ob);

	}
	
	private void Desmemcpy (byte[] output, byte[] input,int outpos, int inpos, int len)
    {
    	int i;

		for (i = 0; i < len; i++)
			output[outpos + i] = input[inpos + i];
	}
	
	//################ desinit() success ####################
	public static void desinit(String  key)
	{
    	perminit(iperm, ip);   //initial permutation
    	perminit(fperm, fp);   //final permutation
    	kinit(key);            //key schedule
    	sinit();               //compression functions
    	p32init();             //32-bit permutation in f
    	System.out.println(" desinit is success !!");
	}
	
	
	//################## perminit() success ####################
	public static void perminit(char perm[][][], char p[])
	{
    	int l, j, k;
    	int i,m;
    	String test_one="";

    	for (i = 0; i < 16; i++)            //each input nibble position
        	for (j = 0; j < 16; j++)        //all possible input nibbles
            	for (k = 0; k < 8; k++){     //each byte of the mask
                	perm[i][j][k]=0;      //clear permutation array
                	//System.out.println("perm=["+perm[i][j][k]+"]");
                }
    	for (i = 0; i < 16; i++)            //each input nibble position
        	for (j = 0; j < 16; j++)        //each possible input nibble
            	for (k = 0; k < 64; k++)    //each output bit position
            	{
                	l = p[k] - 1;       //where does this bit come from
                	if ((l >> 2) != i)  //does it come from input posn?
                    	continue;       //if not, bit k is 0
                	/*chose if (!(j & nibblebit[l & 3])) 不为 0 时 continue *********/
                	
                	test_one=String.valueOf(j&nibblebit[l&3]);
                	//System.out.println("--- ["+test_one+"]");
                	if(test_one.equals("0")){
                		//System.out.println("--- ["+(j&nibblebit[l&3])+"]");
                	    continue;       //any such bit in input?
                	 }
                	m = k & 07;         //which bit is this in the byte
                	//System.out.println("-------m=["+m+"]");
                	//System.out.println(" perminit=["+(perm[i][j][k>>3]|=bytebit[m])+"]");
                	
                	perm[i][j][k >> 3] |= bytebit[m];
                	//System.out.println(" perminit=["+(byte)(perm[i][j][k>>3])+"]");
                	
            	}
	}
	//################## check success ####################	
	//initialize key schedule array 64 bits (will use only 56)
	public static void kinit(String key_new)
	{
    	
    	int i, j, l;
    	int m,_flag;
		
		//System.out.println(" recv key=["+key_new+"]");
		for(i=0;i<8;i++){
			key[i]=key_new.charAt(i);
		}
		
    	for (j = 0; j < 56; j++)    //convert pc1 to bits of key
    	{
       		l = pc1[j] - 1;         //integer bit location
        	m = l & 07;             //find bit
        	//find which key byte l is in and which bit of that byte and store 1-bit result
        	//-----------------chose pc1m[j] = (key[l >> 3] & bytebit[m]) ? 1 : 0 ;
        	_flag=(key[l>>3]&bytebit[m]);
        	if(_flag>0){
        		pc1m[j]=1;
        	}else{
        		pc1m[j]=0;
        	}
        	//System.out.println(" kinit() pc1m["+j+"]==["+pc1m[j]+"]");
    	}
    	for (i = 0; i < 16; i++)    //for each key sched section
        	for (j = 0; j < 6; j++) //and each byte of the kn
            	kn[i][j] = 0;       //clear it for accumulation
            	
    	for (i = 0; i < 16; i++)    //key chunk for each iteration
    	{
        	for (j = 0; j < 56; j++){        //rotate pc1 the right amount
            	pcr[j] = pc1m[(l = j + totrot[i]) < (j < 28 ? 28 : 56) ? l : l - 28];
            	//System.out.println("-------["+pcr[j]+"]");
            }
                        //rotate left and right halves independently
        	for (j = 0; j < 48; j++){        //select bits individually
        		//System.out.println("__"+i+"__"+j+"______=["+(pcr[pc2[j]-1])+"]");
        		//test_two=String.valueOf(pcr[pc2[j] -1]);
            	if (pcr[pc2[j]-1]>0)        //check bit that goes to kn[j]
            	{
            		//System.out.println("___"+j+"______=["+(pcr[pc2[j]-1])+"]");
                	l = j & 07;
                	//System.out.println("--------["+l+"]");
                	
                	kn[i][j >> 3] |= bytebit[l];
                	//System.out.println("--------["+(byte)(kn[i][j>>3])+"]");
            	}           //mask it in if it's there
            	
            }
    	}
	}
	
	//#################### getcomp success !!! ##################
	public static int getcomp(int k, int v)
	{
    	int i, j;   //correspond to i and j in FIPS

    	i = ((v & 040) >> 4) | (v & 1);     //first and last bits make row
    	j = (v & 037) >> 1;                 //middle 4 bits are column
    	return (int) si[k][(i << 4) + j];   //result is ith row, jth col
	}

	//################# sinit success !!! ####################
	public static void sinit( )
	{
    	int i, j;
    	String test="";
    	String hexB="";

    	for (i = 0; i < 4; i++)         //each 12-bit position
        	for (j = 0; j < 4096; j++){  //each possible 12-bit value
        	
        		//---------------------------- chose s[i][j]
            	//s[i][j] = Byte.parseByte(String.valueOf((getcomp(i * 2, j >> 6) << 4) | (017 & getcomp(i * 2 + 1, j & 077))));
            	/***** 转换成 Byte *************
            	byte b=(byte)((getcomp(i * 2, j >> 6) << 4) | (017 & getcomp(i * 2 + 1, j & 077)));
            	String hexB = Integer.toHexString(b).toUpperCase();
            	System.out.println("--------s["+i+"]"+"["+j+"]"+"==="+hexB+"+++"+b);
            	********************/
            	byte b=(byte)((getcomp(i * 2, j >> 6) << 4) | (017 & getcomp(i * 2 + 1, j & 077)));
            	//hexB = Integer.toHexString(b).toUpperCase();
            	//test=String.valueOf((getcomp(i * 2, j >> 6) << 4) | (017 & getcomp(i * 2 + 1, j & 077)));
            	//System.out.println("===========["+test+"]");
            	//s[i][j] =(getcomp(i * 2, j >> 6) << 4) | (017 & getcomp(i * 2 + 1, j & 077));
                s[i][j]=b;
                //System.out.println("---------------["+s[i][j]+"]");
                                    //store 2 compressions per char
             }
	}
	
	//#################### p32init() success !!! #################
	public static void p32init( )
	{
    	int l, j, k;
    	int i,m;

    	for (i = 0; i < 4; i++)             //each input byte position
        	for (j = 0; j < 256; j++)       //all possible input bytes
            	for (k = 0; k < 4; k++)     //each byte of the mask
                	p32[i][j][k] = 0;       //clear permutation array
    	for (i = 0; i < 4; i++)             //each input byte position
        	for (j = 0; j < 256; j++)       //each possible input byte
            	for (k = 0; k < 32; k++)    //each output bit position
            	{
                	l = p32i[k] - 1;    //invert this bit (0-31)
                	if ((l >> 3) != i)  //does it come from input posn?
                    	continue;       //if not, bit k is 0
                	if ((j & bytebit[l & 07])==0){
                		//System.out.println("=========["+(j&bytebit[l &07])+"]");
                    	continue;       //any such bit in input?
                    }
                	m = k & 07;         //which bit is it?
                	p32[i][j][k >> 3] |= bytebit[m];
                	//System.out.println("=========["+(byte)(p32[i][j][k >> 3])+"]");
            	}
	}


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -