?? des.java
字號:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class DES{
JButton b1,b2;
JTextField t1,t2,t3,t4;
JTextArea ta;
JDialog dlog;
DESFrm desfrm;
byte [][]subk=new byte[16][48],subkjm=new byte[16][48];
byte []c=new byte[64];
//sbox[8][64]
byte sbox[][]={{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
},
{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
},
{ 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
},
{ 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
},
{
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
},
{
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
},
{
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
},
{ 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,16,15,3,5,8,
2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11}};
DES()
{
desfrm=new DESFrm();
desfrm.setVisible(true);
b1=desfrm.getB1();
b2=desfrm.getB2();
t1=desfrm.getT1();
t2=desfrm.getT2();
t3=desfrm.getT3();
t4=desfrm.getT4();
ta=desfrm.getTa();
b1.addActionListener(new EvDes1());
b2.addActionListener(new EvDes2());
}
public static void main(String args[])
{
DES dsf=new DES();
}
public void SToBytes(String s,byte []b) //將字符串轉化為數值存入數組
{ byte []st=new byte[8];
byte tmp;
int i,j;
st=s.getBytes();
System.out.println("\nsTOBYTES before:");
for(i=0;i<8;i++)
System.out.print(st[i]+" ");
for(i=0;i<8;i++)
{tmp=st[i];
for(j=0;j<8;j++)
{b[i*8+j]=(byte)(tmp%2);
tmp=(byte)(tmp/2);
}
}
System.out.println("\nsTOBYTES after:");
for(i=0;i<64;i++)
{
System.out.print(b[i]);
if((i+1)%8==0)
System.out.print(" ");
}
}
public void BToStr(byte []a,byte []b) //將字符串轉化為數值存入數組
{
int i,j;
System.out.println("\nBtoStr before:");
for(i=0;i<64;i++)
{
System.out.print(a[i]);
if((i+1)%8==0)
System.out.print(" ");
}
for(i=0;i<8;i++)
{b[i]=0;
for(j=7;j>=0;j--)
{b[i]=(byte)(b[i]*2);
b[i]=(byte)(b[i]+a[i*8+j]);
}
}
System.out.println("\nBtoStr after:");
for(i=0;i<8;i++)
System.out.print(b[i]+" ");
}
public void IPChange(byte mm[])
{//ip[64] 實現ip置換功能
byte [] mt=new byte[64];//臨時數組
byte 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};
for(int i=0;i<64;i++)
mt[i]=mm[i];
for(int i=0;i<64;i++)
mm[i]=mt[ip[i]-1];
}
public void FIP(byte mm[])
{//ip[64] 實現ip逆置換功能
byte [] mt=new byte[64];//臨時數組
byte fp[]={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};
for(int i=0;i<64;i++)
mt[i]=mm[i];
for(int i=0;i<64;i++)
mm[i]=mt[fp[i]-1];
}
public void jiamik(byte []k)
{//加密過程中求子密鑰,測試通過??!完成
byte []k0=new byte[56],cx=new byte[30],dx=new byte[30],kx=new byte[56];
byte ls[]={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1}; //迭代過程中子密鑰左移位數
byte pc2[]={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}; //pc-2數組
byte pc1[]={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};
byte tmp;//臨時變量
int i,j,si;
for(i=0,j=0;i<64;i++)
{if((i+1)%8!=0)
{kx[j]=k[i];
j++;
}
}
/*System.out.print("\n input K seq:\n");//測試代碼
for(i=0;i<56;i++)
{ System.out.print(kx[i]);
if((i+1)%8==0)
System.out.print(" ");
} */
for(i=0;i<56;i++)
k0[i]=k[pc1[i]-1];
for(i=0;i<28;i++)
cx[i]=k0[i];
for(i=0;i<28;i++)
dx[i]=k0[i+28];
/*System.out.println("\nC0:");
for(i=0;i<28;i++)
{System.out.print(cx[i]);
if((i+1)%4==0)
System.out.print(" ");
}
System.out.println("\nD0:");
for(i=0;i<28;i++)
{System.out.print(dx[i]);
if((i+1)%4==0)
System.out.print(" ");
} */
for(i=0;i<16;i++) //i控制循環次數
{ tmp=ls[i];
for(j=0;j<tmp;j++)
cx[28+j]=cx[j];
for(j=0;j<=27;j++)
cx[j]=cx[j+tmp];
//上處理cx移位,下面處理dx移位
for(j=0;j<tmp;j++)
dx[28+j]=dx[j];
for(j=0;j<=27;j++)
dx[j]=dx[j+tmp];
//移位完畢
//下面調試用
/* System.out.println("\nC"+i);
for(int kt=0;kt<28;kt++)
{System.out.print(cx[kt]);
if((kt+1)%4==0)
System.out.print(" ");
}
System.out.println("\nD"+i);
for(int kt=0;kt<28;kt++)
{System.out.print(dx[kt]);
if((kt+1)%4==0)
System.out.print(" ");
} */
//上面調試用
for(j=0;j<28;j++)
{ k0[j]=cx[j];
k0[28+j]=dx[j];
}
for(j=0;j<48;j++)
subk[i][j]=k0[pc2[j]-1];
//下面調試用
/* System.out.println("\nsub k"+i);
for(j=0;j<48;j++)
{System.out.print(subk[i][j]);
if((j+1)%8==0)
System.out.print(" ");
}
System.out.println(); */
}
//子密鑰產生于subk數組完畢,下面產生解密密鑰
for(i=0;i<16;i++)
for(j=0;j<48;j++)
subkjm[15-i][j]=subk[i][j];
}
public void Xor(byte []a,byte []b,byte []c,int len)
{for(int i=0;i<len;i++)
{
if(((a[i]==0)&&(b[i]==0))||((a[i]==1)&&(b[i]==1)))
c[i]=0;
else
c[i]=1;
}
}
public void DieDai(byte []left,byte [] right,byte [][]subkk) //每次迭代算法
{
//e[48]
byte 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};
//置換p矩陣
byte 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};
byte []tmpr=new byte[32],er=new byte[48],tmps_p=new byte[32],tmp_p=new byte[32],tmp_k=new byte[48],bef_box=new byte[48];
byte []lbyte=new byte[32],rbyte=new byte[32];
int i,j,si,seq;
byte b1,b2,b3,b4,b5,b6,sb16,sb2_5,t;
for(seq=0;seq<16;seq++)
{
for(i=0;i<32;i++)
tmpr[i]=right[i];
for(i=0;i<48;i++)
er[i]=right[e[i]-1];
for(i=0;i<48;i++)
tmp_k[i]=subkk[seq][i];
//下段測試用
// System.out.println("\nK:"+seq);
// for(i=0;i<48;i++)
// {System.out.print(tmp_k[i]);
// if((i+1)%8==0)
// System.out.print(" ");
// }
//上測試用
Xor(er,tmp_k,bef_box,48); //在此和k子密鑰異或,完成
for(si=0;si<8;si++)
{b1=bef_box[si*6];
b2=bef_box[si*6+1];
b3=bef_box[si*6+2];
b4=bef_box[si*6+3];
b5=bef_box[si*6+4];
b6=bef_box[si*6+5];
sb16=(byte)(b1*2+b6);
sb2_5=(byte)(b2*8+b3*4+b4*2+b5);
t=sbox[si][sb16*16+sb2_5];
for(i=0;i<4;i++)
{tmps_p[si*4+3-i]=(byte)(t%2);
t=(byte)(t/2);
}
}//縮合完成,存于tmps_p中
for(i=0;i<32;i++)//p置換
tmp_p[i]=tmps_p[p[i]-1];
Xor(left,tmp_p,right,32); // //與異或操作,32bits
for(i=0;i<32;i++)
{left[i]=tmpr[i];
lbyte[i]=(byte)(tmpr[i]+48);
rbyte[i]=(byte)(right[i]+48);
}
ta.append("\nL"+(seq+1)+":");
ta.append(new String(lbyte));
ta.append("\nR"+(seq+1)+":");
ta.append(new String(rbyte));
//下代碼測試用
// System.out.println("\nL:"+seq);
// for(i=0;i<32;i++)
// {System.out.print(left[i]);
// if((i+1)%8==0)
// System.out.print(" ");
// }
// System.out.println("\nR:"+seq);
// for(i=0;i<32;i++)
// {System.out.print(right[i]);
// if((i+1)%8==0)
// System.out.print(" ");
// }
//上代碼測試用
}
}//迭代算法結束
public void showDlog()
{dlog=new JDialog(desfrm,"錯誤",true);
dlog.getContentPane().add(new JLabel("明文或密鑰的位數不正確,要求為64位!"));
dlog.setBounds(200,180,270,80);
dlog.setVisible(true);
//desfrm.setDefaultCloseOperation()
}
class EvDes1 implements ActionListener{
public void actionPerformed(ActionEvent e)
{ //System.out.println("Button1 pressed!");
int i;
String st1,st2;
byte lenmk,lenmk2;
byte [] m=new byte[64],k=new byte[64],cbyte=new byte[8];
byte []left=new byte[32],right=new byte[32],lbyte=new byte[32],rbyte=new byte[32];
st1=t1.getText();
st2=t2.getText();
lenmk=(byte)(st1.length());
lenmk2=(byte)(st2.length());
if((lenmk!=8)||(lenmk2!=8))
{showDlog();
}
System.out.println("\nm origin");
SToBytes(st1,m);
System.out.println("\nk origin");
SToBytes(st2,k);
// m=st1.getBytes();
// SToBytes(m);
// k=st2.getBytes();
// SToBytes(k);
jiamik(k);//調用加密子密鑰算法
IPChange(m);
for(i=0;i<32;i++)
{left[i]=m[i];
lbyte[i]=(byte)(m[i]+48);
}
for(i=32;i<64;i++)
{right[i-32]=m[i];
rbyte[i-32]=(byte)(m[i]+48);
}
ta.append("L0:");
ta.append(new String(lbyte));
ta.append("\nR0:");
ta.append(new String(rbyte));
//下測試代碼
// System.out.println("L0");
// for(i=0;i<32;i++)
// {System.out.print(left[i]);
// if((i+1)%8==0)
// System.out.print(" ");
// }
// System.out.println("\nR0");
// for(i=0;i<32;i++)
// {System.out.print(right[i]);
// if((i+1)%8==0)
// System.out.print(" ");
// }
//上測試代碼
DieDai(left,right,subk);//調用整個迭代函數
for(i=0;i<32;i++)
{c[32+i]=left[i];
c[i]=right[i];
}
FIP(c);
BToStr(c,cbyte);
t3.setText(new String(cbyte));
// for(i=0;i<64;i++)
// c[i]=(byte)(c[i]+48);
// t3.setText(new String(c));
}
}
class EvDes2 implements ActionListener{
public void actionPerformed(ActionEvent e)
{
//解密子密鑰直接用在加密過程中產生的,在subkjm中存放
int i;
byte []m=new byte[64],mbyte=new byte[8];
byte []left=new byte[32],right=new byte[32];
String st3;
// SToBytes(st3,m);
// System.out.println("\n密文C:");
// for(int kt=0;kt<64;kt++)
// {System.out.print(c[kt]);
// if((kt+1)%4==0)
// System.out.print(" ");
// }
//完
IPChange(c);
for(i=0;i<32;i++)
left[i]=c[i];
for(i=32;i<64;i++)
right[i-32]=c[i];
DieDai(left,right,subkjm);
for(i=0;i<32;i++)
{m[32+i]=left[i];
m[i]=right[i];
}
FIP(m);
System.out.println("\njiemi m origin");
BToStr(m,mbyte);
t4.setText(new String(mbyte));
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -