static   bool   SubKey[16][48];//   16
圈子密钥   
    
  void   Des_Run(char   Out[8],   char   In[8],int   i)   
  {   
          static   bool   M[64],   Tmp[32],   *Li   =   &M[0],   *Ri   =   &M[32];   
          ByteToBit(M,   In,   64);//   字节组转换成位组   
          Transform(M,   M,   IP,   64);//   初始变换   
          if(   i   ==   0   ){   //加密   
                  for(int   i=0;   i<16;   i++)   {   
                          memcpy(Tmp,   Ri,   32);   
                          F_func(Ri,   SubKey[i]);   
                          Xor(Ri,   Li,   32);   
                          memcpy(Li,   Tmp,   32);   
                  }   
          }else{   //解密   
                  for(int   i=15;   i>=0;   i--)   {   
                          memcpy(Tmp,   Li,   32);   
                          F_func(Li,   SubKey[i]);   
                          Xor(Li,   Ri,   32);   
                          memcpy(Ri,   Tmp,   32);   
                  }   
  }   
          Transform(M,   M,   IPR,   64);   
          BitToByte(Out,   M,   64);   
  }   
    
  void   Des_SetKey(const   char   Key[8])         //设置密钥   
  {   
          static   bool   K[64],   *KL   =   &K[0],   *KR   =   &K[28];   
          ByteToBit(K,   Key,   64);//   字节组转换成位组   
          Transform(K,   K,   PC1,   56);//   密钥变换   PC-1   
          for(int   i=0;   i<16;   i++)   {   
                  RotateL(KL,   28,   LOOP[i]);   
                  RotateL(KR,   28,   LOOP[i]);   
                  Transform(SubKey[i],   K,   PC2,   48);//   密钥压缩变换   PC-2     
          }   
  }   
  //   F   函数   
  void   F_func(bool   In[32],   const   bool   Ki[48])   
  {   
          static   bool   MR[48];   
          Transform(MR,   In,   E,   48);   
          Xor(MR,   Ki,   48);   
          S_func(In,   MR);   
          Transform(In,   In,   P,   32);   
  }   
  //   S   盒置换   
  void   S_func(bool   Out[32],   const   bool   In[48])   
  {   
          for(char   i=0,j,k;   i<8;   i++,In+=6,Out+=4)   {   
                  j   =   (In[0]<<1)   +   In[5];   
                  k   =   (In[1]<<3)   +   (In[2]<<2)   +   (In[3]<<1)   +   In[4];   
  ByteToBit(Out,   &S_Box[i][j][k],   4);   
          }   
  }   
  //   通用置换函数   
  void   Transform(bool   *Out,   bool   *In,   const   char   *Table,   int   len)   
  {   
          static   bool   Tmp[256];   
          for(int   i=0;   i<len;   i++)   
                  Tmp[i]   =   In[   Table[i]-1   ];   
          memcpy(Out,   Tmp,   len);   
  }   
  //   异或运算   
  void   Xor(bool   *InA,   const   bool   *InB,   int   len)   
  {   
          for(int   i=0;   i<len;   i++)   
                  InA[i]   ^=   InB[i];   
  }   
  //   循环左移   
  void   RotateL(bool   *In,   int   len,   int   loop)   
  {   
          static   bool   Tmp[256];   
          memcpy(Tmp,   In,   loop);//loop   为位移的个数   
          memcpy(In,   In+loop,   len-loop);   
          memcpy(In+len-loop,   Tmp,   loop);   
  }   
  //   字节转换成位   
  void   ByteToBit(bool   *Out,   const   char   *In,   int   bits)   
  {   
          for(int   i=0;   i<bits;   i++)   
                  Out[i]   =   (In[i/8]>>(i%8))   &   1;   
  }   
  //   位转换成字节   
  void   BitToByte(char   *Out,   const   bool   *In,   int   bits)   
  {   
          memset(Out,   0,   (bits+7)/8);   
          for(int   i=0;   i<bits;   i++)   
                  Out[i/8]   |=   In[i]<<(i%8);   
  }   
  void   main(){   
  char   key[8]={6,0,5,3,9,4,7},message[8];   
  cout<<"Please   input   The   string   before   encrypting:   \n";   
  cin>>message;   
  puts("Before   encrypting");   
  puts(message);   
  Des_SetKey(key);   
  Des_Run(message,   message,   0);   
  puts("After   encrypting");   
  puts(message);   
  puts("After   decrypting");   
  Des_Run(message,   message,   1);   
  puts(message);   
  }
三.  实验总结
通过这两次上机实验,使我了解了DES算法的原理。上一页  [1] [2] [3] 
DES加密解密算法实现 第3页下载如图片无法显示或论文不完整,请联系qq752018766