📄 new_des.java
字号:
// ------------------------------------ No success !!!!
package Socket_java;
import java.io.*;
import java.lang.*;
import java.util.*;
import java.sql.*;
public class new_des {
static final int ENCRYPT = 0;
static char iperm [][][]=new char [16][16][8];
static char fperm [][][]=new char [16][16][8];
static char perm [][][]=new char [16][16][8];
static byte p[]=new byte[64];
static char pc1m[]=new char[56]; //place to modify pc1 into
static char pcr[]=new char[56]; //place to rotate pc1 into
static char kn[][]=new char[16][6];
static byte s[][]=new byte [4][4096];//S1 thru S8 precomputed
static char p32[][][]= new char[4][256][4]; //for permuting 32-bit f output
//static char si[][]= new si[8][64];
static char[] key=new char[8];
static byte[] new_block=new byte[8];
static final char[] ip = //initial permutation P
{
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
};
static final int[] nibblebit =
{
010, 04, 02, 01
};
static final int bytebit[] = //bit 0 is left-most in byte
{
0200, 0100, 040, 020, 010, 04, 02, 01
};
static char totrot[] = //number left rotations of pc1
{
1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28
};
static char p32i[] = //32-bit permutation function
{
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
};
static final char[] fp = //final permutation F
{
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
};
static final char pc1[] = //permuted choice table (key)
{
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,
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
};
static final char pc2[] = //permuted choice key (table)
{
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
};
//static final char
static char si[][]=
//48->32 bit compression tables
{ //S[1]
{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},
//S[2]
{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},
//S[3]
{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},
//S[4]
{ 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},
//S[5]
{ 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},
//S[6]
{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},
//S[7]
{ 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},
//S[8]
{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}
};
public new_des(){
}
public static void main(String arg[]){
int i,len;
String inbuf,temp="";
inbuf="88888888";
len=inbuf.length();
temp= DES(0,"12345678",inbuf,8);
//System.out.println("temp=="+temp);
//inbuf=DES(1,"12345678",temp,8);
//System.out.println("inbuf=="+inbuf);
}
public static String DES(int op, String key_new, String inblock ,int len)
{
int i=0;
String temp[]=new String[8];
String test="";
byte[] out_buf=new byte[8];
String kkk="";
String jh="";
StringBuffer per=new StringBuffer();
char c;
Character.UnicodeBlock ub;
System.out.println("_______inblock==["+inblock+"]");
desinit(key_new);
for (i = 0; i < len; i += 8)
{
//memset(temp, '\0', 8);
test=inblock.substring(i,i+8);
//System.out.println("====test--"+test);
//out_buf=permute(test,iperm);
if(op==0){
//System.out.println(" 加密 !!");
out_buf=endes(test);
char[] buffer=new char[8];
int j=0;
for(i=0;i<8;i++){
byte z=out_buf[i];
String hexB = Integer.toHexString(z).toUpperCase();
//per.append("byte=");
//per.append(z);
//per.append(" --->");
per.append((char)z);
//per.append('\n');
System.out.println(" outblock==["+hexB+"]"+" -------->"+Character.UnicodeBlock.of((char)z));
//jh=jh+hexB;
}
System.out.println("%%%%=="+per.toString());
kkk=new String(out_buf);
//kkk=per.toString();
//out_buf=dedes(kkk);
System.out.println("----------["+kkk+"]"+" String Len ="+kkk.length());
//System.out.println("----------["+out_buf+"]");
}else if(op==1){4
out_buf=dedes(kkk); //decrypt
kkk=new String(test);
System.out.println("-------------["+test+"]");
}
//op == ENCRYPT ? endes(temp[i], temp[i]) : dedes(temp[i], temp[i]);
//outblock=temp.substring(i,8);
}
return kkk;
}
//decrypt 64-bit inblock
public static byte[] dedes(String inblock)
{
byte[][] iters=new byte[17][8]; //workspace for each iteration
byte[] swap=new byte[8]; //place to interchange L and R
int i,k,s=0,n=0;
byte[] t=new byte[4];
//char *s, *t;
String dedes_test="";
byte[] de_test=new byte[8];
byte[] outblock=new byte[8];
char[] test_buff=new char[8];
byte[] out_buf=new byte[8];
//out_buf[0]=-37;
//out_buf[1]=-17;
//out_buf[2]=-2;
//out_buf[3]=-59;
//out_buf[4]=-33;
//out_buf[5]=23;
//out_buf[6]=-110;
//out_buf[7]=122;
for(i=0;i<8;i++){
System.out.println("out_buf[i]=="+out_buf[i]);
}
//for(i=0;i<inblock.length();i++){
// test_buff[i]=inblock.charAt(i);
// out_buf[i]=(byte)(test_buff[i]);
// System.out.println(" out_buf==="+out_buf[i]);
//}
de_test=permute(out_buf, iperm); //apply initial permutation
for(i=0;i<8;i++){
iters[0][i]=out_buf[i];
}
for (i = 0; i < 16; i++){ //16 churning operations
//String d_test=new String(iters[i]);
iters[i+1]=iter(15-i,iters[i]);
//iter(15 - i, iters[i], iters[i + 1]);
//for(k=0;k<8;k++){
// iters[i+1][k]=de_test.charAt(k);
//}
}
for(i=0;i<4;i++){ //don't re-copy to save space
t[i] = iters[16][4+i]; //interchange left
}
//reverse order from encrypting
//t = &iters[16][4]; //interchange left
swap[s++]=t[n++];
swap[s++]=t[n++];
swap[s++]=t[n++];
swap[s++]=t[n++];
for(i=0;i<4;i++){ //don't re-copy to save space
t[i] = iters[16][i]; //interchange left
}
k=0;
//t = &iters[16][0]; //and right
swap[s++]=t[k++];
swap[s++]=t[k++];
swap[s++]=t[k++];
swap[s++]=t[k++];
//String t_buf=new String(swap);
outblock=permute(swap, fperm); //apply final permutation
return(outblock);
//printf("----->swap=[%s]\n",swap);
}
//############################## endes success() #########################
//encrypt 64-bit inblock
public static byte[] endes(String inblock)
{
byte[][] iters=new byte[17][8]; //workspace for each iteration
byte[] swap=new byte[8]; //place to interchange L and R
int i,s=0,k=0;
byte[] t=new byte[4];
byte[] endes_test=new byte[8];
byte[] outblock=new byte[8];
char[] test_buff=new char[8];
byte[] in_buf=new byte[8];
//System.out.println("---endes() inblock--"+inblock);
for(i=0;i<inblock.length();i++){
test_buff[i]=inblock.charAt(i);
in_buf[i]=(byte)(test_buff[i]);
}
endes_test=permute(in_buf, iperm); //apply initial permutation
for(i=0;i<8;i++){
iters[0][i]=endes_test[i];
//System.out.println("_________["+iters[0][i]+"]");
}
for (i = 0; i < 16; i++){ //16 churning operations
//String test_=new String(iters[i]);
iters[i+1]=iter(i,iters[i]);
//for(k=0;k<8;k++){
// System.out.print("["+iters[i][k]+"]");
//}
//System.out.print("\n\n");
}
//System.exit(0);
for(i=0;i<4;i++){ //don't re-copy to save space
t[i] = iters[16][4+i]; //interchange left
//System.out.println("++++++++==="+t[i]);
}
s=0;
swap[s++]=t[k++];
swap[s++]=t[k++];
swap[s++]=t[k++];
swap[s++]=t[k++];
for(i=0;i<4;i++){
t[i] = iters[16][0+i]; //and right
}
k=0;
swap[s++]=t[k++];
swap[s++]=t[k++];
swap[s++]=t[k++];
swap[s++]=t[k++];
//for(i=0;i<8;i++){
// System.out.println(" swap=="+swap[i]);
//}
outblock=permute(swap, fperm); //apply final permutation
//String send_buf=new String(swap);
//System.out.println(" jiao mi buf=["+send_buf);
//outblock=permute(send_buf, fperm); //apply final permutation
return (outblock);
}
//########################## iter success !!! ###############################
//1 churning operation, i.e. the num-th one 64 bits each
public static byte[] iter(int num, byte[] new_block)
{
int i,m=0,n=0,k=0;
byte[] fret=new byte[4];
String in_buf=""; //return from f(R[i-1],key)
byte[] ib=new byte[8];
byte[] ob=new byte[8];
byte[] iter_ib=new byte[4];
//char[] fb=new char[];
// register int i; //rwo: unused
for(i=0;i<8;i++){
ib[i]=new_block[i];
//System.out.println("--iter=["+ib[i]+"]"+num);
}
iter_ib[0]=new_block[4];
iter_ib[1]=new_block[5];
iter_ib[2]=new_block[6];
iter_ib[3]=new_block[7];
fret=f(iter_ib,num);
ob[m++] = iter_ib[n++]; //L[i] = R[i-1]
ob[m++] = iter_ib[n++];
ob[m++] = iter_ib[n++];
ob[m++] = iter_ib[n++];
n=0;k=0;
ob[m++]=(byte)(new_block[n++]^fret[k++]);
ob[m++]=(byte)(new_block[n++]^fret[k++]);
ob[m++]=(byte)(new_block[n++]^fret[k++]);
ob[m++]=(byte)(new_block[n++]^fret[k++]);
//for(i=0;i<8;i++){
// System.out.println("ob[m]==="+ob[i]);
//}
/***
in_buf=String.valueOf(ib[4]);
fret=f(in_buf, num); //the primary transformation
ob[m++] = ib[n++]; //L[i] = R[i-1]
ob[m++] = ib[n++];
ob[m++] = ib[n++];
ob[m++] = ib[n++];
//ib = inblock; fb = fret; //R[i]=L[i] XOR f(R[i-1],key)
char[] fb=new char[4];
n=0;
k=0;
for(i=0;i<4;i++){
fb[i]=fret.charAt(i);
}
for(i=4;i<8;i++,n++,k++){
String test_buff=String.valueOf(ib[n]^fb[k]);
ob[i] =test_buff.charAt(0);
}
String outblock=new String(ob);
return (outblock);
***/
return(ob);
}
//
//###################### expand() success !!!! ########################
//
//32 to 48 bits with E oper right is 32, bigright 48
public static byte[] expand(byte[] right)
{
int len,i,j=0,k=0;
//len=right.length();
byte[] right_new=new byte[6];
//len=bigright.length();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -