📄 des.java
字号:
import java.lang.Character;
class des{
char[] m=new char[4]; //64位明文
char[] key=new char[4];//64位密钥
int[] pc_1 = {57,49,41,33,25,17,9, //置换选择函数PC-1
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} ;
int[] pc_2 = {14,17,11,24,1,5, //置换选择函数PC-2
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[] moveTab= {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1} ;//移位对照表
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[] 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[] e = {32,1, 2, 3, 4, 5, /*为选择表E*/
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
} ;
des(){};
char[] bitToByte(char[] bit)//把64位转化为64个字符,以便于置换等操作
{ char[] bytes=new char[64];
int i=0 ;
for(i=0 ; i<4 ; i++)
{
if(((short)bit[i] & 0x8000) == 0x8000) bytes[i*16+0] ='\u0001';
if(((short)bit[i] & 0x4000) == 0x4000) bytes[i*16+1] ='\u0001' ;
if(((short)bit[i] & 0x2000) == 0x2000) bytes[i*16+2] ='\u0001' ;
if(((short)bit[i] & 0x1000) == 0x1000) bytes[i*16+3] ='\u0001';
if(((short)bit[i] & 0x0800) == 0x0800) bytes[i*16+4] ='\u0001' ;
if(((short)bit[i] & 0x0400) == 0x0400) bytes[i*16+5] ='\u0001' ;
if(((short)bit[i] & 0x0200) == 0x0200) bytes[i*16+6] ='\u0001';
if(((short)bit[i] & 0x0100) == 0x0100) bytes[i*16+7] ='\u0001' ;
if(((short)bit[i] & 0x0080) == 0x0080) bytes[i*16+8] ='\u0001';
if(((short)bit[i] & 0x0040) == 0x0040) bytes[i*16+9] ='\u0001';
if(((short)bit[i] & 0x0020) == 0x0020) bytes[i*16+10] ='\u0001';
if(((short)bit[i] & 0x0010) == 0x0010) bytes[i*16+11] ='\u0001';
if(((short)bit[i] & 0x0008) == 0x0008) bytes[i*16+12] ='\u0001';
if(((short)bit[i] & 0x0004) == 0x0004) bytes[i*16+13] ='\u0001';
if(((short)bit[i] & 0x0002) == 0x0002) bytes[i*16+14] ='\u0001' ;
if(((short)bit[i] & 0x0001) == 0x0001) bytes[i*16+15] ='\u0001';
}
return (bytes);
}/* end of bittobyte */
char[] byteToBit(char[] bytes)//把64个字符转化为64位数
{char[] bit=new char[4];
int i;
/*byte1*/
if(bytes[0] =='\u0001') bit[0] =(char)((short)bit[0] | 0x8000) ;
else bit[0] = (char)((short)bit[0] & 0x7fff) ;
if(bytes[1] == '\u0001') bit[0] =(char)((short)bit[0] | 0x4000) ;
else bit[0] = (char)((short)bit[0] & 0xbfff) ;
if(bytes[2] =='\u0001') bit[0] =(char)((short)bit[0] | 0x2000) ;
else bit[0] = (char)((short)bit[0] & 0xdfff) ;
if(bytes[3] =='\u0001') bit[0] =(char)((short)bit[0] | 0x1000) ;
else bit[0] = (char)((short)bit[0] & 0xefff) ;
if(bytes[4] =='\u0001') bit[0] =(char)((short)bit[0] | 0x0800) ;
else bit[0] = (char)((short)bit[0] & 0xf7ff) ;
if(bytes[5] == '\u0001') bit[0] =(char)((short)bit[0] | 0x0400) ;
else bit[0] = (char)((short)bit[0] & 0xfbff) ;
if(bytes[6] =='\u0001') bit[0] =(char)((short)bit[0] | 0x0200) ;
else bit[0] = (char)((short)bit[0] & 0xfdff) ;
if(bytes[7] =='\u0001') bit[0] =(char)((short)bit[0] | 0x0100) ;
else bit[0] = (char)((short)bit[0] & 0xfeff) ;
if(bytes[8] =='\u0001') bit[0] =(char)((short)bit[0] | 0x0080) ;
else bit[0] = (char)((short)bit[0] & 0xff7f) ;
if(bytes[9] == '\u0001') bit[0] =(char)((short)bit[0] | 0x0040) ;
else bit[0] = (char)((short)bit[0] & 0xffbf) ;
if(bytes[10] =='\u0001') bit[0] =(char)((short)bit[0] | 0x0020) ;
else bit[0] = (char)((short)bit[0] & 0xffdf) ;
if(bytes[11] =='\u0001') bit[0] =(char)((short)bit[0] | 0x0010) ;
else bit[0] = (char)((short)bit[0] & 0xffef) ;
if(bytes[12] =='\u0001') bit[0] =(char)((short)bit[0] | 0x0008) ;
else bit[0] = (char)((short)bit[0] & 0xfff7) ;
if(bytes[13] == '\u0001') bit[0] =(char)((short)bit[0] | 0x0004) ;
else bit[0] = (char)((short)bit[0] & 0xfffb) ;
if(bytes[14] =='\u0001') bit[0] =(char)((short)bit[0] | 0x0002) ;
else bit[0] = (char)((short)bit[0] & 0xfffd) ;
if(bytes[15] =='\u0001') bit[0] =(char)((short)bit[0] | 0x0001) ;
else bit[0] = (char)((short)bit[0] & 0xfffe) ;
/*byte2*/
if(bytes[16] =='\u0001') bit[1] =(char)((short)bit[1] | 0x8000) ;
else bit[1] = (char)((short)bit[1] & 0x7fff) ;
if(bytes[17] == '\u0001') bit[1] =(char)((short)bit[1] | 0x4000) ;
else bit[1] = (char)((short)bit[1] & 0xbfff) ;
if(bytes[18] =='\u0001') bit[1] =(char)((short)bit[1] | 0x2000) ;
else bit[1] = (char)((short)bit[1] & 0xdfff) ;
if(bytes[19] =='\u0001') bit[1] =(char)((short)bit[1] | 0x1000) ;
else bit[1] = (char)((short)bit[1] & 0xefff) ;
if(bytes[20] =='\u0001') bit[1] =(char)((short)bit[1] | 0x0800) ;
else bit[1] = (char)((short)bit[1] & 0xf7ff) ;
if(bytes[21] == '\u0001') bit[1] =(char)((short)bit[1] | 0x0400) ;
else bit[1] = (char)((short)bit[1] & 0xfbff) ;
if(bytes[22] =='\u0001') bit[1] =(char)((short)bit[1] | 0x0200) ;
else bit[1] = (char)((short)bit[1] & 0xfdff) ;
if(bytes[23] =='\u0001') bit[1] =(char)((short)bit[1] | 0x0100) ;
else bit[1] = (char)((short)bit[1] & 0xfeff) ;
if(bytes[24] =='\u0001') bit[1] =(char)((short)bit[1] | 0x0080) ;
else bit[1] = (char)((short)bit[1] & 0xff7f) ;
if(bytes[25] == '\u0001') bit[1] =(char)((short)bit[1] | 0x0040) ;
else bit[1] = (char)((short)bit[1] & 0xffbf) ;
if(bytes[26] =='\u0001') bit[1] =(char)((short)bit[1] | 0x0020) ;
else bit[1] = (char)((short)bit[1] & 0xffdf) ;
if(bytes[27] =='\u0001') bit[1] =(char)((short)bit[1] | 0x0010) ;
else bit[1] = (char)((short)bit[1] & 0xffef) ;
if(bytes[28] =='\u0001') bit[1] =(char)((short)bit[1] | 0x0008) ;
else bit[1] = (char)((short)bit[1] & 0xfff7) ;
if(bytes[29] == '\u0001') bit[1] =(char)((short)bit[1] | 0x0004) ;
else bit[1] = (char)((short)bit[1] & 0xfffb) ;
if(bytes[30] =='\u0001') bit[1] =(char)((short)bit[1] | 0x0002) ;
else bit[1] = (char)((short)bit[1] & 0xfffd) ;
if(bytes[31] =='\u0001') bit[1] =(char)((short)bit[1] | 0x0001) ;
else bit[1] = (char)((short)bit[1] & 0xfffe) ;
/*byte3*/
if(bytes[32] =='\u0001') bit[2] =(char)((short)bit[2] | 0x8000) ;
else bit[2] = (char)((short)bit[2] & 0x7fff) ;
if(bytes[33] == '\u0001') bit[2] =(char)((short)bit[2] | 0x4000) ;
else bit[2] = (char)((short)bit[2] & 0xbfff) ;
if(bytes[34] =='\u0001') bit[2] =(char)((short)bit[2] | 0x2000) ;
else bit[2] = (char)((short)bit[2] & 0xdfff) ;
if(bytes[35] =='\u0001') bit[2] =(char)((short)bit[2] | 0x1000) ;
else bit[2] = (char)((short)bit[2] & 0xefff) ;
if(bytes[36] =='\u0001') bit[2] =(char)((short)bit[2] | 0x0800) ;
else bit[2] = (char)((short)bit[2] & 0xf7ff) ;
if(bytes[37] == '\u0001') bit[2] =(char)((short)bit[2] | 0x0400) ;
else bit[2] = (char)((short)bit[2] & 0xfbff) ;
if(bytes[38] =='\u0001') bit[2] =(char)((short)bit[2] | 0x0200) ;
else bit[2] = (char)((short)bit[2] & 0xfdff) ;
if(bytes[39] =='\u0001') bit[2] =(char)((short)bit[2] | 0x0100) ;
else bit[2] = (char)((short)bit[2] & 0xfeff) ;
if(bytes[40] =='\u0001') bit[2] =(char)((short)bit[2] | 0x0080) ;
else bit[2] = (char)((short)bit[2] & 0xff7f) ;
if(bytes[41] == '\u0001') bit[2] =(char)((short)bit[2] | 0x0040) ;
else bit[2] = (char)((short)bit[2] & 0xffbf) ;
if(bytes[42] =='\u0001') bit[2] =(char)((short)bit[2] | 0x0020) ;
else bit[2] = (char)((short)bit[2] & 0xffdf) ;
if(bytes[43] =='\u0001') bit[2] =(char)((short)bit[2] | 0x0010) ;
else bit[2] = (char)((short)bit[2] & 0xffef) ;
if(bytes[44] =='\u0001') bit[2] =(char)((short)bit[2] | 0x0008) ;
else bit[2] = (char)((short)bit[2] & 0xfff7) ;
if(bytes[45] == '\u0001') bit[2] =(char)((short)bit[2] | 0x0004) ;
else bit[2] = (char)((short)bit[2] & 0xfffb) ;
if(bytes[46] =='\u0001') bit[2] =(char)((short)bit[2] | 0x0002) ;
else bit[2] = (char)((short)bit[2] & 0xfffd) ;
if(bytes[47] =='\u0001') bit[2] =(char)((short)bit[2] | 0x0001) ;
else bit[2] = (char)((short)bit[2] & 0xfffe) ;
/*byte4*/
if(bytes[48] =='\u0001') bit[3] =(char)((short)bit[3] | 0x8000) ;
else bit[3] = (char)((short)bit[3] & 0x7fff) ;
if(bytes[49] == '\u0001') bit[3] =(char)((short)bit[3] | 0x4000) ;
else bit[3] = (char)((short)bit[3] & 0xbfff) ;
if(bytes[50] =='\u0001') bit[3] =(char)((short)bit[3] | 0x2000) ;
else bit[3] = (char)((short)bit[3] & 0xdfff) ;
if(bytes[51] =='\u0001') bit[3] =(char)((short)bit[3] | 0x1000) ;
else bit[3] = (char)((short)bit[3] & 0xefff) ;
if(bytes[52] =='\u0001') bit[3] =(char)((short)bit[3] | 0x0800) ;
else bit[3] = (char)((short)bit[3] & 0xf7ff) ;
if(bytes[53] == '\u0001') bit[3] =(char)((short)bit[3] | 0x0400) ;
else bit[3] = (char)((short)bit[3] & 0xfbff) ;
if(bytes[54] =='\u0001') bit[3] =(char)((short)bit[3] | 0x0200) ;
else bit[3] = (char)((short)bit[3] & 0xfdff) ;
if(bytes[55] =='\u0001') bit[3] =(char)((short)bit[3] | 0x0100) ;
else bit[3] = (char)((short)bit[3] & 0xfeff) ;
if(bytes[56] =='\u0001') bit[3] =(char)((short)bit[3] | 0x0080) ;
else bit[3] = (char)((short)bit[3] & 0xff7f) ;
if(bytes[57] == '\u0001') bit[3] =(char)((short)bit[3] | 0x0040) ;
else bit[3] = (char)((short)bit[3] & 0xffbf) ;
if(bytes[58] =='\u0001') bit[3] =(char)((short)bit[3] | 0x0020) ;
else bit[3] = (char)((short)bit[3] & 0xffdf) ;
if(bytes[59] =='\u0001') bit[3] =(char)((short)bit[3] | 0x0010) ;
else bit[3] = (char)((short)bit[3] & 0xffef) ;
if(bytes[60] =='\u0001') bit[3] =(char)((short)bit[3] | 0x0008) ;
else bit[3] = (char)((short)bit[3] & 0xfff7) ;
if(bytes[61] == '\u0001') bit[3] =(char)((short)bit[3] | 0x0004) ;
else bit[3] = (char)((short)bit[3] & 0xfffb) ;
if(bytes[62] =='\u0001') bit[3] =(char)((short)bit[3] | 0x0002) ;
else bit[3] = (char)((short)bit[3] & 0xfffd) ;
if(bytes[63] =='\u0001') bit[3] =(char)((short)bit[3] | 0x0001) ;
else bit[3] = (char)((short)bit[3] & 0xfffe) ;
return bit;
}//end of byteToBit;
char[][] subKey(char[] oldKey)//生成16*48位子密钥,该程序中生成了16*64位,只是64位中只有前48有用到
{des s=new des();
char[] oldKey_byte=new char[64];
char[] oldKey_byte1=new char[64];
char[] oldKey_byte2=new char[64];
char[] oldkey_c=new char[28];
char[] oldkey_d=new char[28];
char tmp;
char[][] newKey_byte=new char[16][64];//16*64个字符
char[][] newKey=new char[16][4];//16*4*16=16*64位
int i,j,k;
oldKey_byte=s.bitToByte(oldKey);//把64位密钥变为64个字符
for(i=0;i<=55;i++)/*使用pc-1表现去除奇偶校验位*/
oldKey_byte1[i] = oldKey_byte[s.pc_1[i] - 1] ;
for(i=0;i<=27;i++) /*把56位分成两半*/
oldkey_c[i] = oldKey_byte1[i] ;
for(i=28;i<=55;i++)
oldkey_d[i -28] = oldKey_byte1[i] ;
/*根据移位表生成c1~c16,d1~d16*/
for(i=0;i<=15;i++) //16次循环生成16个子密钥
{
for(j=0;j<s.moveTab[i];j++)//根据移位表作相应的移位
{
tmp=oldkey_c[0];
for(k=0;k<27;k++)
{oldkey_c[k]=oldkey_c[k+1];
}
oldkey_c[27]=tmp; //oldkey_c完成一次循环左移
tmp=oldkey_d[0];
for(k=0;k<27;k++)
{oldkey_d[k]=oldkey_d[k+1];
}
oldkey_d[27]=tmp;//oldkey_d完成一次循环左移
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -