欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

DES算法

程序员文章站 2022-05-15 14:58:44
...

DES算法


算法原理概述

DES(Data Encryption Standard)算法为密码*中的对称密码*,又被称为美国数据加密标准,是1972年美国IBM公司研制的对称密码*加密算法。 明文按64位进行分组,**长64位,**事实上是56位参与DES运算(第8、16、24、32、40、48、56、64位是校验位, 使得每个**都有奇数个1)分组后的明文组和56位的**按位替代或交换的方法形成密文组的加密方法。
其入口参数有三个:key、data、mode。key为加密解密使用的**,data为加密解密的数据,mode为其工作模式。当模式为加密模式时,明文按照64位进行分组,形成明文组,key用于对数据加密,当模式为解密模式时,key用于对数据解密。实际运用中,**只用到了64位中的56位,这样才具有高的安全性。


总体结构

加密过程

DES算法

  1. 对64位明文进行初始置换(IP_Transform)
  2. 将IP置换后的64位数据分为两部分,左32位记为L,右32位记为R
  3. 去掉奇偶校验位的**为56位(PC1_Transform),将其左右分为28位,分别将左右28位进行循环左移位(shift_left)再合并为56位,再经压缩(PC2_Transform)后为48位子**(GenerateSubKey)
  4. 对R进行扩展置换(E_Transform),得到48位的数据与**压缩后的48位异或
  5. 将经过异或后的48位数据分为8组,每组6位放入S盒中,输出8组4位,总共为32位(SBOX),再经过(P_Transform)
  6. 将上面得到的32位数据与L异或结果赋给R
  7. 前15次依次交换L和R位置(Swap)
  8. 重复以上2-7过程16次
  9. 将最后的到的L和R合并做逆置换(IP_1_Transform)

解密过程

与加密过程一样,只是每次迭代使用左移**的时候是使用对称的,也就是加密是第i轮,则解密使用第17-i轮。


模块分解

将每次位数置换变换,异或,交换,字节位转换和加密解密分为一个模块

    void CharToBit(char ch[8],char bit[64]);
    void BitToChar(char bit[64],char ch[8]);
    void GenerateSubKey(char key[64],char subKeys[16][48]);
    void PC1_Transform(char key[64], char tempbts[56]);
    void PC2_Transform(char key[56], char tempbts[48]);
    void Shift_Left(char data[56], int time);
    void IP_Transform(char data[64]);
    void IP_1_Transform(char data[64]);
    void E_Transform(char data[48]);
    void P_Transform(char data[32]);
    void SBOX(char data[48]);
    void XOR(char R[48], char L[48], int count);
    void Swap(char left[32],char right[32]);
    void Encrypt64Bit(char plainBlock[8], char subKeys[16][48], char cipherBlock[8]);
    void Decrypt64Bit(char cipherBlock[8], char subKeys[16][48], char plainBlock[8]);
    void Encrypt(char *plainFile, char *keyStr,char *cipherFile);
    void Decrypt(char *cipherFile, char *keyStr,char *plainFile);  
  • IP置换
    int IP_Table[64] = 
    {
        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,
        56,48,40,32,24,16,8,0,
        58,50,42,34,26,18,10,2,
        60,52,44,36,28,20,12,4,
        62,54,46,38,30,22,14,6
    }; 

表的意思就是第一次变换时,第57位移到第1位,第49位移到第2位,…… 依此类推。得到64位数据

  • **变换
    int PC_1[56] = 
    {
        56,48,40,32,24,16,8,
        0,57,49,41,33,25,17,
        9,1,58,50,42,34,26,
        18,10,2,59,51,43,35,
        62,54,46,38,30,22,14,
        6,61,53,45,37,29,21,
        13,5,60,52,44,36,28,
        20,12,4,27,19,11,3
    };
    int PC_2[48] = 
    {
        13,16,10,23,0,4,2,27,
        14,5,20,9,22,18,11,3,
        25,7,15,6,26,19,12,1,
        40,51,30,36,46,54,29,39,
        50,44,32,46,43,48,38,55,
        33,52,45,41,49,35,28,31
    };
    //左移次数的规定
    int MOVE_TIMES[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};

**经PC_1变换去奇偶校验位后由64位变为56位,再经过PC_2压缩变为48位,分别对左右28位在第i次迭代中使用上述对应的移动次数。

  • 扩展置换
    int E_Table[48] = 
    {
        31, 0, 1, 2, 3, 4,
        3,  4, 5, 6, 7, 8,
        7,  8,9,10,11,12,
        11,12,13,14,15,16,
        15,16,17,18,19,20,
        19,20,21,22,23,24,
        23,24,25,26,27,28,
        27,28,29,30,31, 0
    };

扩展置换是将R由32位扩展为48位。

  • S-盒
    int S[8][4][16] =//S1
        {{{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}},
        //S2
        {{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}},
        //S3
        {{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}},
        //S4
        {{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}},
        //S5
        {{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}},
        //S6
        {{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}},
        //S7
        {{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}},
        //S8
        {{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}}};

使用S[i]替换R与K1异或结的果B[i][6]。过程如下: 取出B[i][6]的第1位和第6位连成一个2位数m, m就是S[i]中对应的行数(0-3),取出B[i][6]的第2到第5位连成一个4位数n(0-15),n就是S[i]中对应的列数,用S[i][m][n]代替B[i][6]。S是4行16列的对应表,里面是4位的数,一共有8个S。

  • P置换
    int P_Table[32] = {
        15,6,19,20,28,11,27,16,
        0,14,22,25,4,17,30,9,
        1,7,23,13,31,26,2,8,
        18,12,29,5,21,10,3,24
    };

P置换是将经过S-盒后的32位数据再经一个置换。

  • IP逆置换
    int IP_1_Table[64] = 
    {
        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,
        32,0,40,8,48,16,56,24
    };

数据结构

采用char的数据结构来存储输入明文以及保存的密文和其每个位。通过读取两个txt文本文件,要加密的plain.txt和**key.txt。加密输出为密文,解密输出解密后的明文。

C++类代码实现

源代码

des.h

class DES {
  private:
    int IP_Table[64] = 
    {
        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,
        56,48,40,32,24,16,8,0,
        58,50,42,34,26,18,10,2,
        60,52,44,36,28,20,12,4,
        62,54,46,38,30,22,14,6
    }; 
    //逆初始置换表IP^-1
    int IP_1_Table[64] = 
    {
        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,
        32,0,40,8,48,16,56,24
    };

    //扩充置换表E
    int E_Table[48] = 
    {
        31, 0, 1, 2, 3, 4,
        3,  4, 5, 6, 7, 8,
        7,  8,9,10,11,12,
        11,12,13,14,15,16,
        15,16,17,18,19,20,
        19,20,21,22,23,24,
        23,24,25,26,27,28,
        27,28,29,30,31, 0
    };

    //置换函数P
    int P_Table[32] = {
        15,6,19,20,28,11,27,16,
        0,14,22,25,4,17,30,9,
        1,7,23,13,31,26,2,8,
        18,12,29,5,21,10,3,24
    };

    //S盒
    int S[8][4][16] =//S1
        {{{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}},
        //S2
        {{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}},
        //S3
        {{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}},
        //S4
        {{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}},
        //S5
        {{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}},
        //S6
        {{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}},
        //S7
        {{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}},
        //S8
        {{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}}};
    //置换选择1
    int PC_1[56] = 
    {
        56,48,40,32,24,16,8,
        0,57,49,41,33,25,17,
        9,1,58,50,42,34,26,
        18,10,2,59,51,43,35,
        62,54,46,38,30,22,14,
        6,61,53,45,37,29,21,
        13,5,60,52,44,36,28,
        20,12,4,27,19,11,3
    };

    //置换选择2
    int PC_2[48] = 
    {
        13,16,10,23,0,4,2,27,
        14,5,20,9,22,18,11,3,
        25,7,15,6,26,19,12,1,
        40,51,30,36,46,54,29,39,
        50,44,32,46,43,48,38,55,
        33,52,45,41,49,35,28,31
    };

    //对左移次数的规定
    int MOVE_TIMES[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};

  public:
    DES();
    ~DES();
    void CharToBit(char ch[8],char bit[64]);
    void BitToChar(char bit[64],char ch[8]);
    void GenerateSubKey(char key[64],char subKeys[16][48]);
    void PC1_Transform(char key[64], char tempbts[56]);
    void PC2_Transform(char key[56], char tempbts[48]);
    void Shift_Left(char data[56], int time);
    void IP_Transform(char data[64]);
    void IP_1_Transform(char data[64]);
    void E_Transform(char data[48]);
    void P_Transform(char data[32]);
    void SBOX(char data[48]);
    void XOR(char R[48], char L[48], int count);
    void Swap(char left[32],char right[32]);
    void Encrypt64Bit(char plainBlock[8], char subKeys[16][48], char cipherBlock[8]);
    void Decrypt64Bit(char cipherBlock[8], char subKeys[16][48], char plainBlock[8]);
    void Encrypt(char *plainFile, char *keyStr,char *cipherFile);
    void Decrypt(char *cipherFile, char *keyStr,char *plainFile);  
}; 

Des.cpp

#include "des.h"
#include <iostream>
#include <fstream>
#include <bitset>
#include <cstring>
using namespace std;

DES::DES() {}
DES::~DES() {}

void DES::IP_Transform(char data[64]) {
  char temp[64];
  for(int i = 0; i < 64; i++) {
    temp[i] = data[IP_Table[i]];
  }
  memcpy(data,temp,64);
}

void DES::GenerateSubKey(char key[64], char subKeys[16][48]) {
  char temp[56];
  PC1_Transform(key,temp);
  for(int i = 0; i < 16; i++) {
    Shift_Left(temp, MOVE_TIMES[i]);
    PC2_Transform(temp, subKeys[i]);
  }
}

void DES::PC1_Transform(char key[64], char tempbts[56]) {
  for(int i = 0; i < 56; i++) {
    tempbts[i] = key[PC_1[i]];
  }
}

void DES::PC2_Transform(char key[56], char tempbts[48]) {
  for(int i = 0; i < 48; i++) {
    tempbts[i] = key[PC_2[i]];
  }
}

void DES::Shift_Left(char data[56], int time){    
  char temp[56];
  memcpy(temp,data,time);
  memcpy(temp+time,data+28,time);
  memcpy(data,data+time,28-time);
  memcpy(data+28-time,temp,time);

  memcpy(data+28,data+28+time,28-time);
  memcpy(data+56-time,temp+time,time);    

}

void DES::IP_1_Transform(char data[64]) {
  char temp[64];
  for(int i = 0; i < 64; i++) {
    temp[i] = data[IP_1_Table[i]];
  }
  memcpy(data,temp,64);
}

void DES::E_Transform(char data[48]) {
  char temp[48];
  for(int i = 0; i < 48; i++) {
    temp[i] = data[E_Table[i]];
  }
  memcpy(data,temp,48);
}

void DES::P_Transform(char data[32]) {
  char temp[32];
  for(int i = 0; i < 32; i++) {
    temp[i] = data[P_Table[i]];
  }
  memcpy(data,temp,32);
}

void DES::XOR(char R[48], char L[48] ,int count) {
  for(int i = 0; i < count; i++) {
    R[i] ^= L[i];
  }
}

void DES::SBOX(char data[48]) {
  int line,row,output;
  int cur1,cur2;
  for(int i = 0; i < 8; i++){
    cur1 = i * 6;
    cur2 = i<< 2;

    line = (data[cur1]<<1) + data[cur1+5];
    row = (data[cur1+1]<<3) + (data[cur1+2]<<2)
        + (data[cur1+3]<<1) + data[cur1+4];
    output = S[i][line][row];

    data[cur2] = (output&0X08)>>3;
    data[cur2+1] = (output&0X04)>>2;
    data[cur2+2] = (output&0X02)>>1;
    data[cur2+3] = output&0x01;
  }
}

void DES::Swap(char left[32], char right[32]) {
  char temp[32];
  memcpy(temp,left,32);    
  memcpy(left,right,32);    
  memcpy(right,temp,32);
}

void DES::CharToBit(char ch[8],char bit[64]){
  for(int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
      *(bit + i * 8 + j) = (ch[i]>> j) & 1;
    }
  }
}

void DES::BitToChar(char bit[64],char ch[8]){
  memset(ch, 0, 8);
  for(int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
      *(ch + i) |= bit[i * 8 + j]<< j;
    }
  }
}

void DES::Encrypt64Bit(char plainBlock[8], char subKeys[16][48], char cipherBlock[8]){
  char plainBits[64];
  char copyRight[48];
  int i;

  CharToBit(plainBlock,plainBits);        
  IP_Transform(plainBits);

  for(i = 0; i < 16; i++){        
    memcpy(copyRight,plainBits+32,32);
    E_Transform(copyRight);
    XOR(copyRight,subKeys[i],48);    
    SBOX(copyRight);
    P_Transform(copyRight);
    XOR(plainBits,copyRight,32);
    if(i != 15){
      Swap(plainBits,plainBits+32);
    }
  }
  IP_1_Transform(plainBits);
  BitToChar(plainBits,cipherBlock);
}

void DES::Decrypt64Bit(char cipherBlock[8], char subKeys[16][48],char plainBlock[8]){
  char cipherBits[64];
  char copyRight[48];

  CharToBit(cipherBlock,cipherBits);        
  IP_Transform(cipherBits);

  for(int i = 15; i >= 0; i--){        
    memcpy(copyRight,cipherBits+32,32);
    E_Transform(copyRight);
    XOR(copyRight,subKeys[i],48);        
    SBOX(copyRight);
    P_Transform(copyRight);        
    XOR(cipherBits,copyRight,32);
    if(i != 0) {
      Swap(cipherBits,cipherBits+32);
    }
  }
  IP_1_Transform(cipherBits);
  BitToChar(cipherBits,plainBlock);
}

void DES::Encrypt(char *plainFile, char *keyStr,char *cipherFile){
  FILE *plain,*cipher;
  int count;
  char plainBlock[8],cipherBlock[8],keyBlock[8];
  char bKey[64];
  char subKeys[16][48];
  if((plain = fopen(plainFile,"rb")) == NULL){
    cout<< "read error"<<endl;
    return;
  }    
  if((cipher = fopen(cipherFile,"wb")) == NULL){
    cout<< "write error"<< endl;
    return;
  }
  memcpy(keyBlock,keyStr,8);
  CharToBit(keyBlock,bKey);
  GenerateSubKey(bKey,subKeys);

  while(!feof(plain)){
    if((count = fread(plainBlock, sizeof(char), 8, plain)) == 8){
      Encrypt64Bit(plainBlock, subKeys, cipherBlock);
      fwrite(cipherBlock, sizeof(char), 8, cipher);    
    }
  }
  if(count){//不足8位时填充
    memset(plainBlock + count,'\0', 7 - count);
    plainBlock[7] = 8 - count;
    Encrypt64Bit(plainBlock,subKeys,cipherBlock);
    fwrite(cipherBlock, sizeof(char), 8, cipher);
  }
  fclose(plain);
  fclose(cipher);
}

void DES::Decrypt(char *cipherFile, char *keyStr,char *plainFile){
  FILE *plain, *cipher;
  int count,times = 0;
  long fileLen;
  char plainBlock[8], cipherBlock[8], keyBlock[8];
  char bKey[64];
  char subKeys[16][48];
  if((cipher = fopen(cipherFile,"rb")) == NULL){
    cout<< "read error"<< endl;
    return;
  }
  if((plain = fopen(plainFile,"wb")) == NULL){
    cout<< "write error"<< endl;
    return;
  }
  memcpy(keyBlock,keyStr,8);
  CharToBit(keyBlock,bKey);
  GenerateSubKey(bKey,subKeys); 
  fseek(cipher, 0, SEEK_END);    //将文件指针置尾
  fileLen = ftell(cipher);    //取文件指针当前位置
  rewind(cipher);                //将文件指针重指向文件头
  while(1){
    fread(cipherBlock,sizeof(char), 8, cipher);
    Decrypt64Bit(cipherBlock, subKeys, plainBlock);                        
    times += 8;
    if(times < fileLen){
      fwrite(plainBlock, sizeof(char), 8, plain);
    } else {
      break;
    }
  }
  if(plainBlock[7] < 8) {
    for(count = 8 - plainBlock[7]; count < 7; count++){
      if(plainBlock[count] != '\0'){
        break;
      }
    }
  }    
  if(count == 7) {
    fwrite(plainBlock, sizeof(char), 8 - plainBlock[7], plain);
  } else {
    fwrite(plainBlock, sizeof(char), 8, plain);
  }
  fclose(plain);
  fclose(cipher);
}

int main() {
  DES d;
  d.Encrypt("plain.txt","key.txt","cipher.txt");
  d.Decrypt("cipher.txt","key.txt","newplain.txt");  
  return 0;  
}

测试结果

输入key.txt
yes
输入plain.txt
helloworld
加密输出cipher.txt
鬣aaa@qq.com喯E絟l亖
解密输出newplain.txt
helloworld