📄 mydes.java
字号:
import java.io.*;
import java.lang.*;
import java.util.ArrayList;
public class myDES
{
byte plain[];
byte code[];
byte key[];
boolean isEncrypt;
boolean is3DES;
boolean subkey[][][];
static boolean temp[];
public myDES(byte plainText[],byte key[],boolean isEncrypt)throws NumberFormatException
{
int len=((plainText.length)%8)!=0?((plainText.length)+7)&0xfffffff8:plainText.length;
plain=new byte[len];
arraycopy(plainText,0,plain,0,plainText.length);
for(int i=plainText.length;i<len;i++)
{
plain[i]=0x20;
}
this.key=key;
this.isEncrypt=isEncrypt;
subkey=new boolean[2][16][56];
temp=new boolean[256];
code=new byte[plain.length];;
for(int i=0;i<code.length;i++)
code[i]=0x00;
}
final int initialP[]={
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
};
final int finalP[]={
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
};
final int extension[]={
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
};
final int p_box[]={
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
};
final int s_box[][][]={
// 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}}
};
final int key_p[]={
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
};
final int loop[]={
1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1
};
final int keyCompress[]={
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
};
void byteToBit(boolean out[],int begOut,byte in[],int begIn,int bits)
{
for(int i=0;i<bits;i++)
{
out[begOut+i]=((in[begIn+(i>>3)]>>(i&7))&0x01)==1?true:false;
}
}
void bitToByte(byte out[],int begOut,boolean in[],int begIn,int bits)
{
//for(int k=0;k<(bits>>3);k++)
// out[begOut+k]=0;
for(int i=0;i<bits;i++)
{
int b=(boolToInt(in[begIn+i]))<<(i&(0x07));
out[begOut+(i>>3)]=(new Integer((out[begOut+(i>>3)])|b)).byteValue();
}
}
void transform(boolean out[],int begOut,boolean in[],int begIn,int table[],int len)
{
//boolean temp[]=new boolean[len];
for(int i=0;i<len;i++)
{
temp[i]=in[begIn+table[i]-1];
}
arraycopy(temp,0,out,begOut,len);
}
void xor(boolean out[],int begOut,boolean in[],int begIn,int len)
{
for(int i=0;i<len;i++)
out[begOut+i]=intToBool((boolToInt(out[begOut+i]))^(boolToInt(in[begIn+i])));
}
void rotateLeft(boolean in[],int begIn,int len,int loop)
{
arraycopy(in,begIn+loop,temp,0,len-loop);
arraycopy(in,begIn,in,len-loop,loop);
arraycopy(temp,0,in,begIn,len-loop);
}
void s_function(boolean out[],int outbase,boolean in[],int inbase)
{
int indexIn=0;
int indexOut=0;
int k=0,j=0;
for(int i=0;i<8;i++)
{
j=((boolToInt(in[inbase+indexIn]))<<1)+(boolToInt(in[inbase+indexIn+5]));
k=(boolToInt(in[inbase+indexIn+1])<<3)+(boolToInt(in[inbase+indexIn+2])<<2)+(boolToInt(in[inbase+indexIn+3])<<1)+(boolToInt(in[inbase+indexIn+4]));
byte[] b=new byte[1];
b[0]=(new Integer(s_box[i][j][k])).byteValue();
byteToBit(out,outbase+indexOut,b,0,4);
indexOut+=4;
indexIn+=6;
}
}
void f_function(boolean in[],int begIn,boolean subkey[])
{
transform(temp,0,in,begIn,extension,48);
xor(temp,0,subkey,0,48);
s_function(in,begIn,temp,0);
transform(in,begIn,in,begIn,p_box,32);
}
void setSubKey(boolean skey[][],byte key[],int beg)
{
boolean t[]=new boolean[64];
byteToBit(t,0,key,beg,64);
transform(t,0,t,0,key_p,56);
for(int i=0;i<16;i++)
{
rotateLeft(t,0,28,loop[i]);
rotateLeft(t,28,28,loop[i]);
transform(skey[i],0,t,0,keyCompress,48);
}
}
void setKey(byte key[])
{
if(key.length>=16)
{
setSubKey(subkey[1],key,8);
setSubKey(subkey[0],key,0);
is3DES=true;
}
else if(key.length>=8)
{
setSubKey(subkey[0],key,0);
is3DES=false;
}
else
{
System.out.println("the key should be 8 bytes or 16 bytes");
System.exit(1);
}
}
void encript(byte out[],int begOut,byte in[],int begIn,boolean key[][],boolean type)
{
boolean t[]=new boolean[64];
boolean shr[]=new boolean[32];
//print("crypt words= "+new String(in,begIn,8));
byteToBit(t,0,in,begIn,64);
// System.out.print("work before: ");disp(t,0,64);
transform(t,0,t,0,initialP,64);
if(type==true)
{
for(int i=0;i<16;i++)
{
arraycopy(t,32,shr,0,32);
f_function(t,32,key[i]);
xor(t,32,t,0,32);//disp(t,32,32);
arraycopy(shr,0,t,0,32);//disp(t,0,64);
}
}
else
for(int i=15;i>=0;i--)
{
arraycopy(t,0,shr,0,32);
f_function(t,0,key[i]);
xor(t,0,t,32,32);
arraycopy(shr,0,t,32,32);
}
transform(t,0,t,0,finalP,64); //System.out.print("work end: ");disp(t,0,64);
bitToByte(out,begOut,t,0,64);
//System.out.println("at encrypt: code= "+new String(out,begOut,8));
}
void disp(boolean in[],int beg,int len)
{
for(int i=beg;i<beg+len;i++)
{
if((i-beg)%8==0)
System.out.printf(" ");
System.out.printf(boolToInt(in[i])+"");
}
System.out.println();
}
boolean doEncrypt()
{
if(plain==null||subkey==null)
return false;
setKey(key);
int begIn=0;
int begOut=0;
//code=new byte[plain.length];
if(!is3DES)
{
for(int i=0,j=plain.length>>3;i<j;i++,begOut+=8,begIn+=8)
{
encript(code,begIn,plain,begOut,subkey[0],isEncrypt);
}
}
else
for(int i=0,j=((plain.length)>>3);i<j;i++,begOut+=8,begIn+=8)
{
encript(code,begIn,plain,begOut,subkey[0],isEncrypt);
encript(code,begIn,plain,begOut,subkey[1],!isEncrypt);
encript(code,begIn,plain,begOut,subkey[0],isEncrypt);
}
return true;
}
int stableBit(int i)
{
if(code==null)
return -1;
int bit=processBit(code,i,false);
int newBit=-1;
boolean flag=false;
int len=plain.length<<3;
//System.out.println("len= "+len);
ArrayList<Integer> record=new ArrayList<Integer>();
while(!flag)
{
int index=(int)Math.floor((len*Math.random()));
if(record.contains(index))
{
//System.out.println(index+" has been in the queue");
break;
}
else
{
record.add(index);
//System.out.println("index= "+index);
processBit(plain,index,true);
doEncrypt();
newBit=processBit(code,i,false);
if(newBit!=bit)
flag=true;
}
}
return record.size();
}
int processBit(byte in[],int bitIndex,boolean changeBit)
{
int byteIndex=bitIndex>>3;
bitIndex=bitIndex&7;
int bit=-1;
if(!changeBit)
{
bit=(in[byteIndex]>>(8-bitIndex))&1;
}
else
{
//System.out.printf("before changed: %1$h\n",in[byteIndex]);
//byte b=(byte)((in[byteIndex]>>bitIndex)&1);
//System.out.printf("b= %1$h",b);
//System.out.println();
byte b=1;
//System.out.printf("b= %1$h",b);
in[byteIndex]=(byte)((b<<(8-bitIndex))^in[byteIndex]);
//System.out.printf("after changed: %1$h\n",in[byteIndex]);
}
return bit;
}
int selectBit(int id)
{
int r=processBit(code,id,false);
for(int i=0;i<plain.length<<3;i++)
{
//System.out.println("i= "+i);
processBit(plain,i,true);
doEncrypt();
if(r!=processBit(code,id,false))
return i;
processBit(plain,i,true);
}
// System.out.println("one bit cannot change the "+id+"th bit");
return -1;
}
int[] select2Bit(int in)
{
int result[]=new int[2];
int r=processBit(code,in,false);
int i=0,j=0;
for(i=0;i<plain.length<<3;i++)
{
processBit(plain,i,true);
result[0]=i;
for(j=0;j<i;j++)
{
processBit(plain,j,true);
if(r!=processBit(code,in,false))
{
result[1]=j;
return result;
}
processBit(plain,j,true);
}
processBit(plain,i,true);
}
//System.out.println("i="+i+" j="+j+" cannot find two bits to change "+in+"th bit");
return null;
}
public static void main(String args[])
{
String plain=new String("welcome");
char a[]={1,2,3,4,5,6,7,8};
byte key[]=new byte[a.length];
for(int i=0;i<a.length;i++)
key[i]=new Integer(a[i]).byteValue();
myDES des=new myDES(plain.getBytes(),key,true);
if(des.doEncrypt())
{
System.out.printf("after encrypted:");
System.out.println(new String(des.code));
int sum=0;
int i=0;
for(i=0;i<des.plain.length<<3;i++)
{
int all=0;
for(int p=0;p<100;p++)
{
int k=des.stableBit(i);
all+=k;
}
//System.out.println("take "+k+" bits change the "+i+"th bit.");
System.out.printf(all/100+" ");
sum+=all/100;
}
System.out.println("\n average takes "+sum/i+" bits to change a bit.");
int r=des.selectBit(2);
if(r!=-1)
System.out.println("just change the "+r+"th bit");
int b[]=des.select2Bit(1);
if(b!=null)
System.out.println(b[0]+" "+b[1]);
}
/*
des=new myDES((des.code),key,false);
//des.show(des.plain,0,des.plain.length);
if(des.doEncrypt())
{
System.out.printf("after decrypted: ");
des.print(new String(des.code));
}
//System.out.println(new String(new Byte(0x20))+"a");
*/
}
int boolToInt(boolean b)
{
return b==true?1:0;
}
boolean intToBool(int i)
{
return i!=0?true:false;
}
void arraycopy(Object s,int beg,Object d,int st,int len)
{
System.arraycopy(s,beg,d,st,len);
}
void show(byte in[],int beg,int len)
{
boolean a[]=new boolean[len<<3];
byteToBit(a,0,in,beg,len<<3);
disp(a,0,len<<3);
}
void print(String w)
{
System.out.println(w);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -