算法系列1《DES》

来源:互联网 发布:广西网络问政 编辑:程序博客网 时间:2024/05/22 09:06

目录(?)[-]

  1. DES算法简介
  2. DES实现源码
  3. DES加解密工具

     

1. DES算法简介


     DES算法全称为Data Encryption Standard,即数据加密算法,它是IBM公司于1975年研究成功并公开发表的。DES算法的入口参数有三个:Key、Data、Mode。其中Key为8个字节共64位,是DES算法的工作密钥;Data也为8个字节64位,是要被加密或被解密的数据;Mode为DES的工作方式,有两种:加密或解密。

 

     DES 使用一个56 位的密钥以及附加的 8 位奇偶校验位,产生最大 64 位的分组大小。这是一个迭代的分组密码,使用称为 Feistel 的技术,其中将加密的文本块分成两半。使用子密钥对其中一半应用循环功能,然后将输出与另一半进行“异或”运算;接着交换这两半,这一过程会继续下去,但最后一个循环不交换。DES 使用16 个循环,使用异或置换代换移位操作四种基本运算。

 

    DES 的常见变体是三重 DES,使用168 位的密钥对资料进行三次加密的一种机制;它通常(但非始终)提供极其强大的安全性。如果三个 56 位的子元素都相同,则三重 DES 向后兼容DES。

 

    攻击 DES 的主要形式被称为蛮力的或彻底密钥搜索,即重复尝试各种密钥直到有一个符合为止。如果 DES 使用56 位的密钥,则可能的密钥数量是 2 的 56次方个。随着计算机系统能力的不断发展,DES 的安全性比它刚出现时会弱得多,然而从非关键性质的实际出发,仍可以认为它是足够的。不过,DES 现在仅用于旧系统的鉴定,而更多地选择新的加密标准 — 高级加密标准(AdvancedEncryption Standard,AES).

 

   该算法被允许用于安全报文传送MAC机制密文运算,算法的详细过程在ISO8731-1、ISO8732、ISO/IEC10116中定义。







2. DES实现源码


<<DES.h>>

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. namespace Des  
  2. {  
  3.     enum  
  4.     {  
  5.         ECB = 0,  
  6.         CBC = 1  
  7.     };  
  8.     enum  
  9.     {  
  10.         ENCRYPT = 0,  
  11.         DECRYPT = 1  
  12.     };  
  13.   
  14.     typedef BYTE (*PSUBKEY)[16][48];  
  15.   
  16.     void ByteToBit(const BYTE* pIn, BYTE byBits, BYTE* pOut);  
  17.     void BitToByte(const BYTE* pIn, BYTE byBits, BYTE* pOut);  
  18.     void LeftShift(BYTE* pIn, BYTE byInLen, BYTE byOffset);  
  19.     void Xor(const BYTE* pIn, BYTE byLen, BYTE* pInOut);  
  20.     void Transform(const BYTE* pIn, const bool* pTable, BYTE len, bool* pOut);  
  21.     void S_func(const BYTE in[48], BYTE out[32]);  
  22.     void F_func(const BYTE ki[48], BYTE out[32]);  
  23.   
  24.     void SetSubKey(PSUBKEY pSubKey, const BYTE Key[8]);  
  25.     void DoDes(int nMode, int nOperator, const BYTE* input, int nInLen, const BYTE* key, int nKeyLen, BYTE* output, const BYTE* init_Vector= NULL);  
  26.     BOOL DoDes(int nMode, int nOperator, string strText, string KEK, string &OutData,const BYTE* init_Vector = NULL);  
  27.     void DoDesMac(string intText, string KEK, string &OutData, const BYTE* init_Vector = NULL);  
  28.     void DoSSMac(string intText, string KEK, string &OutData,int _Length);  
  29.     void DoGPMac(string intText, string KEK, string &OutData);  
  30.     void RunDes(const BYTE In[8], int nType, BYTE* Key, BYTE Out[8]);  
  31.     void DoDesMac(const BYTE* input, int nDataLen, const BYTE* key, int nKeyLen, BYTE* output, const BYTE* init_Vector = NULL);  
  32.     void DoSSMac(const BYTE* input, int nDataLen, const BYTE* key, int nKeyLen, BYTE* output);  
  33.     void DoGPMac(const BYTE* input, int nInLen, const BYTE* key, int nKeyLen, BYTE* output);  
  34.     string DesVerify(string Stxt);  
  35. }  
  36.    

<<DES.CPP>>

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. namespace Des  
  2. {  
  3.     // initial permutation IP  
  4.     const BYTE IP_Table[64] = {  
  5.         58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,  
  6.             62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,  
  7.             57, 49, 41, 33, 25, 17,  9, 1, 59, 51, 43, 35, 27, 19, 11, 3,  
  8.             61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7  
  9.     };  
  10.   
  11.     // final permutation IP^-1   
  12.     const BYTE IPR_Table[64] = {  
  13.         40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31,  
  14.             38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29,  
  15.             36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27,  
  16.             34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41,  9, 49, 17, 57, 25  
  17.     };  
  18.   
  19.     // expansion operation matrix  
  20.     const BYTE E_Table[48] = {  
  21.         32, 1,  2,  3,  4,  5,  4,  5,  6,  7,  8,  9,  
  22.             8,  9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17,  
  23.             16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25,  
  24.             24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32,  1  
  25.     };  
  26.   
  27.     // 32-bit permutation function P used on the output of the S-boxes   
  28.     const BYTE P_Table[32] = {  
  29.         16, 7, 20, 21, 29, 12, 28, 17, 1,  15, 23, 26, 5,  18, 31, 10,  
  30.             2,  8, 24, 14, 32, 27, 3,  9,  19, 13, 30, 6,  22, 11, 4,  25  
  31.     };  
  32.   
  33.     // permuted choice table (key)   
  34.     const BYTE PC1_Table[56] = {  
  35.         57, 49, 41, 33, 25, 17,  9,  1, 58, 50, 42, 34, 26, 18,  
  36.             10,  2, 59, 51, 43, 35, 27, 19, 11,  3, 60, 52, 44, 36,  
  37.             63, 55, 47, 39, 31, 23, 15,  7, 62, 54, 46, 38, 30, 22,  
  38.             14,  6, 61, 53, 45, 37, 29, 21, 13,  5, 28, 20, 12,  4  
  39.     };  
  40.   
  41.     // permuted choice key (table)   
  42.     const BYTE PC2_Table[48] = {  
  43.         14, 17, 11, 24,  1,  5,  3, 28, 15,  6, 21, 10,  
  44.             23, 19, 12,  4, 26,  8, 16,  7, 27, 20, 13,  2,  
  45.             41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48,  
  46.             44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32  
  47.     };  
  48.   
  49.     // number left rotations of pc1   
  50.     const BYTE LR_Table[16] = {1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1};  
  51.   
  52.     // The (in)famous S-boxes   
  53.     const BYTE S_Box[8][4][16] = {  
  54.         // S1   
  55.         14, 4,  13,  1,  2, 15, 11,  8,  3, 10,  6, 12,  5,  9,  0,  7,  
  56.             0, 15,  7,  4, 14,  2, 13,  1, 10,  6, 12, 11,  9,  5,  3,  8,  
  57.             4,  1, 14,  8, 13,  6,  2, 11, 15, 12,  9,  7,  3, 10,  5,  0,  
  58.             15, 12,  8,  2,  4,  9,  1,  7,  5, 11,  3, 14, 10,  0,  6, 13,  
  59.             // S2   
  60.             15,  1,  8, 14,  6, 11,  3,  4,  9,  7,  2, 13, 12,  0,  5, 10,  
  61.             3, 13,  4,  7, 15,  2,  8, 14, 12,  0,  1, 10,  6,  9, 11,  5,  
  62.             0, 14,  7, 11, 10,  4, 13,  1,  5,  8, 12,  6,  9,  3,  2, 15,  
  63.             13,  8, 10,  1,  3, 15,  4,  2, 11,  6,  7, 12,  0,  5, 14,  9,  
  64.             // S3   
  65.             10,  0,  9, 14,  6,  3, 15,  5,  1, 13, 12,  7, 11,  4,  2,  8,  
  66.             13,  7,  0,  9,  3,  4,  6, 10,  2,  8,  5, 14, 12, 11, 15,  1,  
  67.             13,  6,  4,  9,  8, 15,  3,  0, 11,  1,  2, 12,  5, 10, 14,  7,  
  68.             1, 10, 13,  0,  6,  9,  8,  7,  4, 15, 14,  3, 11,  5,  2, 12,  
  69.             // S4   
  70.             7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,  5, 11, 12,  4, 15,  
  71.             13,  8, 11,  5,  6, 15,  0,  3,  4,  7,  2, 12,  1, 10, 14,  9,  
  72.             10,  6,  9,  0, 12, 11,  7, 13, 15,  1,  3, 14,  5,  2,  8,  4,  
  73.             3, 15,  0,  6, 10,  1, 13,  8,  9,  4,  5, 11, 12,  7,  2, 14,  
  74.             // S5   
  75.             2, 12,  4,  1,  7, 10, 11,  6,  8,  5,  3, 15, 13,  0, 14,  9,  
  76.             14, 11,  2, 12,  4,  7, 13,  1,  5,  0, 15, 10,  3,  9,  8,  6,  
  77.             4,  2,  1, 11, 10, 13,  7,  8, 15,  9, 12,  5,  6,  3,  0, 14,  
  78.             11,  8, 12,  7,  1, 14,  2, 13,  6, 15,  0,  9, 10,  4,  5,  3,  
  79.             // S6   
  80.             12,  1, 10, 15,  9,  2,  6,  8,  0, 13,  3,  4, 14,  7,  5, 11,  
  81.             10, 15,  4,  2,  7, 12,  9,  5,  6,  1, 13, 14,  0, 11,  3,  8,  
  82.             9, 14, 15,  5,  2,  8, 12,  3,  7,  0,  4, 10,  1, 13, 11,  6,  
  83.             4,  3,  2, 12,  9,  5, 15, 10, 11, 14,  1,  7,  6,  0,  8, 13,  
  84.             // S7   
  85.             4, 11,  2, 14, 15,  0,  8, 13,  3, 12,  9,  7,  5, 10,  6,  1,  
  86.             13, 0, 11,  7,  4,  9,  1, 10, 14,  3,  5, 12,  2, 15,  8,  6,  
  87.             1,  4, 11, 13, 12,  3,  7, 14, 10, 15,  6,  8,  0,  5,  9,  2,  
  88.             6, 11, 13,  8,  1,  4, 10,  7,  9,  5,  0, 15, 14,  2,  3, 12,  
  89.             // S8   
  90.             13,  2,  8,  4,  6, 15, 11,  1, 10,  9,  3, 14,  5,  0, 12, 7,  
  91.             1, 15, 13,  8, 10,  3,  7,  4, 12,  5,  6, 11,  0, 14,  9,  2,  
  92.             7, 11,  4,  1,  9, 12, 14,  2,  0,  6, 10, 13, 15,  3,  5,  8,  
  93.             2,  1, 14,  7,  4, 10,  8, 13, 15, 12,  9,  0,  3,  5,  6, 11  
  94.     };  
  95.   
  96.     void ByteToBit(const BYTE* pIn, BYTE byBits, BYTE* pOut)  
  97.     {  
  98.         for (int i = 0; i < byBits;  ++ i)  
  99.         {  
  100.             pOut[i] = (pIn[i >> 3] >> (7 - i & 7)) & 1;  
  101.         }  
  102.     }  
  103.   
  104.     void BitToByte(const BYTE* pIn, BYTE byBits, BYTE* pOut)  
  105.     {  
  106.         memset(pOut, 0, byBits >> 3);  
  107.         for (int i = 0; i < byBits;  ++ i)  
  108.         {  
  109.             pOut[i >> 3] |= (pIn[i] << (7 - i & 7));  
  110.         }  
  111.     }  
  112.   
  113.     void LeftShift(BYTE* pIn, BYTE byInLen, BYTE byOffset)  
  114.     {  
  115.         BYTE temp[256];  
  116.         memcpy(temp, pIn, byOffset);  
  117.         memcpy(pIn, pIn  +  byOffset, byInLen - byOffset);  
  118.         memcpy(pIn  +  byInLen - byOffset, temp, byOffset);  
  119.     }  
  120.       
  121.     void Xor(const BYTE* pIn1, const BYTE* pIn2, BYTE byLen, BYTE* pInOut)  
  122.     {  
  123.         for (int i = 0; i < byLen;  ++ i)  
  124.         {  
  125.             pInOut[i] = pIn1[i] ^ pIn2[i];  
  126.         }  
  127.     }  
  128.   
  129.     void Transform(const BYTE* pIn, const BYTE* pTable, BYTE len, BYTE* pOut)  
  130.     {  
  131.         BYTE temp[64];  
  132.   
  133.         for (int i = 0; i < len;  ++ i)  
  134.         {  
  135.             temp[i] = pIn[pTable[i] - 1];  
  136.         }  
  137.         memcpy(pOut, temp, len);  
  138.     }  
  139.   
  140.     void S_func(const BYTE in[48], BYTE out[32]) //4BIT 代替 6BIT  
  141.     {  
  142.         for (BYTE i = 0, j, k; i < 8;  ++ i, in  += 6, out  += 4)   
  143.         {  
  144.             j = (in[0] << 1)  +  in[5];  
  145.             k = (in[1] << 3)  +  (in[2] << 2)  +  (in[3] << 1)  +  in[4]; //组织SID下标  
  146.   
  147.             for (BYTE l = 0; l < 4;  ++ l)  //把相应4bit赋值  
  148.             {  
  149.                 out[l] = (S_Box[i][j][k] >> (3 - l)) & 1;  
  150.             }  
  151.         }  
  152.     }  
  153.   
  154.     void F_func(const BYTE ki[48], BYTE out[32])  
  155.     {  
  156.         BYTE MR[48];  
  157.         Transform(out, E_Table, 48, MR);  //扩展置换E  
  158.         Xor(ki, MR, 48, MR);  
  159.         S_func(MR, out);  
  160.         Transform(out, P_Table, 32, out);  
  161.     }  
  162.   
  163.     void SetSubKey(PSUBKEY pSubKey, const BYTE Key[8])  
  164.     {  
  165.         BYTE K[64];   
  166.         BYTE* KL = &K[0];  
  167.         BYTE* KR = &K[28];  
  168.         ByteToBit(Key, 64, K);  
  169.         Transform(K, PC1_Table, 56, K);  
  170.         for (int i = 0; i < 16;  ++ i)   
  171.         {  
  172.             LeftShift(KL, 28, LR_Table[i]);  
  173.             LeftShift(KR, 28, LR_Table[i]);  
  174.             Transform(K, PC2_Table, 48, (*pSubKey)[i]);  
  175.         }  
  176.     }  
  177.   
  178.     void RunDes(const BYTE* In, int nOperator, const PSUBKEY pSubKey, BYTE* Out)  
  179.     {  
  180.         BYTE M[64];  
  181.         BYTE temp[32];  
  182.         BYTE* li = &M[0];  
  183.         BYTE* ri = &M[32];  
  184.         ByteToBit(In, 64, M);  
  185.         Transform(M, IP_Table, 64, M); //  
  186.         if (ENCRYPT == nOperator)  
  187.         {  
  188.             for (int i = 0; i < 16;  ++ i)  
  189.             {  
  190.                 memcpy(temp, ri, 32);       //Ri[i-1] 保存  
  191.                 F_func((*pSubKey)[i], ri);  //Ri[i-1]经过转化和SBox输出为P盒  
  192.                 Xor(li, ri, 32, ri);        //Ri[i] = P XOR Li[i-1]  
  193.                 memcpy(li, temp, 32);       //Li[i] = Ri[i-1]   
  194.             }  
  195.         }  
  196.         else  
  197.         {  
  198.             for (int i = 15; i >= 0; --i)   
  199.             {  
  200.                 memcpy(temp, ri, 32);       //Ri[i-1] 保存  
  201.                 F_func((*pSubKey)[i], ri);  //Ri[i-1]经过转化和SBox输出为P  
  202.                 Xor(li, ri, 32, ri);        //Ri[i] = P XOR Li[i-1]  
  203.                 memcpy(li, temp, 32);       //Li[i] = Ri[i-1]  
  204.             }  
  205.         }  
  206.         LeftShift(M, 64, 32);               //Ri与Li换位重组M   
  207.         Transform(M, IPR_Table, 64, M);     //最后结果进行转化  
  208.         BitToByte(M, 64, Out);              //组织成字符  
  209.     }  
  210.   
  211.     void DoDes(int nMode, int nOperator, const BYTE* input, int nInLen, const BYTE* key, BYTE nKeyLen, BYTE* output, const BYTE* init_Vector)  
  212.     {  
  213.         BYTE bySubKey[3][16][48];       //秘钥  
  214.         memset(bySubKey, 0x01, sizeof(bySubKey));  
  215.   
  216.         //构造并生成SubKeys  
  217.         BYTE nKey = (nKeyLen >> 3) > 3 ? 3 : (nKeyLen >> 3);  
  218.         for (int i = 0; i < nKey; i++)  
  219.         {  
  220.             SetSubKey(&bySubKey[i], &key[i << 3]);  
  221.         }  
  222.   
  223.         int j = nInLen >> 3;  
  224.         if (nMode == ECB)   //ECB模式  
  225.         {  
  226.             if (1 == nKey)  //单Key  
  227.             {  
  228.                 for (int i = 0; i < j; ++i, output += 8, input += 8)  
  229.                 {  
  230.                     RunDes(input, nOperator, &bySubKey[0], output);  
  231.                 }  
  232.             }  
  233.             else if (2 == nKey) //3DES 2Key  
  234.             {  
  235.                 for (int i = 0; i < j; ++i, output += 8, input += 8)  
  236.                 {  
  237.                     RunDes(input, nOperator, &bySubKey[0], output);  
  238.                     RunDes(output, !nOperator, &bySubKey[1], output);  
  239.                     RunDes(output, nOperator, &bySubKey[0], output);  
  240.                 }  
  241.             }  
  242.             else            //3DES 3Key  
  243.             {  
  244.                 for (int i = 0; i < j; ++i, output += 8, input += 8)  
  245.                 {  
  246.                     RunDes(input, nOperator, &bySubKey[nOperator ? 2 : 0], output);  
  247.                     RunDes(output, !nOperator, &bySubKey[1], output);  
  248.                     RunDes(output, nOperator, &bySubKey[nOperator ? 0 : 2], output);  
  249.                 }  
  250.             }  
  251.         }  
  252.         else  //CBC模式  如果init_Vector为NULL则设置初始向量为8字节的0  
  253.         {  
  254.             BYTE byVector[8];    //扭转向量  
  255.             BYTE byTemp[8];      //中间变量  
  256.   
  257.             memset(byVector, 0x00, sizeof(byVector));  
  258.             memset(byTemp,   0x00, sizeof(byTemp));  
  259.   
  260.             if (init_Vector)  
  261.             {  
  262.                 memcpy(byVector, init_Vector, 8);  
  263.             }  
  264.   
  265.             if (nKey == 1)  //单Key  
  266.             {  
  267.                 for (int i = 0; i < j; ++i, output += 8, input += 8)  
  268.                 {  
  269.                     if (ENCRYPT == nOperator)  
  270.                     {  
  271.                         Xor(input, byVector, 8, byTemp);         //将输入与扭转变量异或  
  272.                     }  
  273.                     else  
  274.                     {  
  275.                         memcpy(byTemp, input, 8);  
  276.                     }  
  277.   
  278.                     RunDes(byTemp, nOperator, &bySubKey[0], output);  
  279.   
  280.                     if (ENCRYPT == nOperator)  
  281.                     {  
  282.                         memcpy(byVector, output, 8);            //将输出设定为扭转变量  
  283.                     }  
  284.                     else  
  285.                     {  
  286.                         Xor(output, byVector, 8, output);       //将输出与扭转变量异或  
  287.   
  288.                         memcpy(byVector, byTemp, 8);            //将输入设定为扭转变量  
  289.                     }  
  290.                 }  
  291.             }  
  292.             else if (nKey == 2) //3DES CBC 2Key  
  293.             {  
  294.                 for (int i = 0; i < j; ++i, output += 8, input += 8)  
  295.                 {  
  296.                     if (ENCRYPT == nOperator)  
  297.                     {  
  298.                         for (int j = 0; j < 8; ++j)      //将输入与扭转变量异或  
  299.                         {  
  300.                             byTemp[j] = input[j] ^ byVector[j];  
  301.                         }  
  302.                     }  
  303.                     else  
  304.                     {  
  305.                         memcpy(byTemp, input, 8);  
  306.                     }  
  307.   
  308.                     RunDes(byTemp, nOperator, &bySubKey[0], output);  
  309.                     RunDes(output, !nOperator, &bySubKey[1], output);  
  310.                     RunDes(output, nOperator, &bySubKey[0], output);  
  311.   
  312.                     if (ENCRYPT == nOperator)  
  313.                     {  
  314.                         memcpy(byVector, output, 8);            //将输出设定为扭转变量  
  315.                     }  
  316.                     else  
  317.                     {  
  318.                         for (int j = 0; j < 8; ++j)      //将输出与扭转变量异或  
  319.                         {  
  320.                             output[j] = output[j] ^ byVector[j];  
  321.                         }  
  322.                         memcpy(byVector, byTemp, 8);            //将输入设定为扭转变量  
  323.                     }  
  324.                 }  
  325.             }  
  326.             else            //3DES CBC 3Key  
  327.             {  
  328.                 for (int i = 0; i < j; ++i, output += 8, input += 8)  
  329.                 {  
  330.                     if (ENCRYPT == nOperator)  
  331.                     {  
  332.                         for (int j = 0; j < 8; ++j)      //将输入与扭转变量异或  
  333.                         {  
  334.                             byTemp[j] = input[j] ^ byVector[j];  
  335.                         }  
  336.                     }  
  337.                     else  
  338.                     {  
  339.                         memcpy(byTemp, input, 8);  
  340.                     }  
  341.   
  342.                     RunDes(byTemp, nOperator, &bySubKey[nOperator ? 2 : 0], output);  
  343.                     RunDes(output, !nOperator, &bySubKey[1], output);  
  344.                     RunDes(output, nOperator, &bySubKey[nOperator ? 0 : 2], output);  
  345.   
  346.                     if (ENCRYPT == nOperator)  
  347.                     {  
  348.                         memcpy(byVector, output, 8);            //将输出设定为扭转变量  
  349.                     }  
  350.                     else  
  351.                     {  
  352.                         for (int j = 0; j < 8; ++j)              //将输出与扭转变量异或  
  353.                         {  
  354.                             output[j] = output[j] ^ byVector[j];  
  355.                         }  
  356.                         memcpy(byVector, byTemp, 8);            //将输入设定为扭转变量  
  357.                     }  
  358.                 }  
  359.             }  
  360.         }  
  361.     }  
  362.   
  363.     BOOL DoDes(int nMode, int nOperator, string strText, string KEK, string &OutData,const BYTE* init_Vector)  
  364.     {     
  365.         BYTE key[33]= {0};  
  366.         BYTE input[512] = {0};  
  367.         BYTE output[512] = {0};  
  368.         int nInLen = strText.length()/2;  
  369.         BYTE nKeyLen=(BYTE)KEK.length()/2;  
  370.         strings::HexToAsc(strText, input);  
  371.         strings::HexToAsc(KEK, key);  
  372.         if(nInLen%8!=0 || nKeyLen%8!=0 || nInLen==0 || nKeyLen==0){  
  373.             return false;  
  374.         }  
  375.   
  376.         DoDes(nMode, nOperator, input, nInLen, key, nKeyLen, output, init_Vector);  
  377.         strings::AscToHex(output, nInLen, OutData);  
  378.         return true;  
  379.     }  
  380.   
  381.     //ANSI X9.9 MAC    DES CBC  
  382.     void DoDesMac(string intText, string KEK, string &OutData, const BYTE* init_Vector)  
  383.     {  
  384.         BYTE byVector[8];  
  385.         BYTE byTemp[8];  
  386.         BYTE byData[128];  
  387.   
  388.         BYTE Input[512]={0};  
  389.         BYTE Key[512]={0};  
  390.         int nInLen;  
  391.         BYTE nKeyLen;  
  392.         BYTE Output[512]={0};  
  393.   
  394.         BYTE *input=Input;  
  395.         BYTE *key=Key;  
  396.         BYTE *output=Output;  
  397.   
  398.         nInLen=(BYTE)intText.length()/2;  
  399.         nKeyLen=(BYTE)KEK.length()/2;  
  400.   
  401.         strings::HexToAsc(intText, input);  
  402.         strings::HexToAsc(KEK, key);  
  403.   
  404.         memset(byVector, 0x00, sizeof(byVector));  
  405.         memset(byTemp,   0x00, sizeof(byTemp));  
  406.         memset(byData,   0x00, sizeof(byData));  
  407.   
  408.         BYTE bySubKey[3][16][48];       //秘钥  
  409.   
  410.         memset(bySubKey, 0x01, sizeof(bySubKey));  
  411.   
  412.         //构造并生成SubKeys  
  413.         BYTE nKey = (nKeyLen >> 3) > 3 ? 3 : (nKeyLen >> 3);  
  414.         for (int i = 0; i < nKey; i ++ )  
  415.         {  
  416.             SetSubKey(&bySubKey[i], &key[i << 3]);  
  417.         }  
  418.   
  419.         int j = nInLen >> 3;  
  420.   
  421.         if (init_Vector != NULL)  
  422.         {  
  423.             memcpy(byVector, init_Vector, 8);  
  424.         }  
  425.   
  426.         if (1 == nKey)  //单倍长Key(8字节)  
  427.         {  
  428.             for (int i = 0; i < j;  ++ i, input  += 8)  
  429.             {  
  430.                 Xor(input, byVector, 8, byTemp);  
  431.                 RunDes(byTemp, ENCRYPT, &bySubKey[0], output);  
  432.   
  433.                 memcpy(byVector, output, 8);            //将输出设定为扭转变量  
  434.             }  
  435.         }  
  436.         else if (2 == nKey) //双倍长Key(16字节)  
  437.         {  
  438.             for (int i = 0; i < j;  ++ i, input  += 8)  
  439.             {  
  440.                 Xor(input, byVector, 8, byTemp);  
  441.                 RunDes(byTemp, ENCRYPT, &bySubKey[0], output);  
  442.                 RunDes(output, DECRYPT, &bySubKey[1], output);  
  443.                 RunDes(output, ENCRYPT, &bySubKey[0], output);  
  444.   
  445.                 memcpy(byVector, output, 8);            //将输出设定为扭转变量  
  446.             }  
  447.         }  
  448.         else  //三倍长Key(24字节)    尚未验证  
  449.         {  
  450.             for (int i = 0; i < j;  ++ i, input  += 8)  
  451.             {  
  452.                 Xor(input, byVector, 8, byTemp);  
  453.                 RunDes(byTemp, ENCRYPT, &bySubKey[0], output);  
  454.                 RunDes(output, DECRYPT, &bySubKey[1], output);  
  455.                 RunDes(output, ENCRYPT, &bySubKey[2], output);  
  456.   
  457.                 memcpy(byVector, output, 8);            //将输出设定为扭转变量  
  458.             }  
  459.         }  
  460.   
  461.         strings::AscToHex(Output, 8, OutData);  
  462.     }  
  463.   
  464.     //该函数的计算结果与卫士通dll计算MAC的结果一样  
  465.     //input中要有80 + 00.... input的前8字节作为初始向量  
  466.     void DoSSMac(string intText, string KEK, string &OutData,int _Length)  
  467.     {   
  468.         int nInLen=(int)intText.length()/2;  
  469.         int nKeyLen=(int)KEK.length()/2;  
  470.         unsigned char *input = new unsigned char[nInLen];  
  471.         unsigned char *key = new unsigned char[nKeyLen];  
  472.   
  473.         strings::HexToAsc((const unsigned char*)intText.c_str(), nInLen*2, input);  
  474.         strings::HexToAsc((const unsigned char*)KEK.c_str(), nKeyLen*2, key);  
  475.   
  476.         BYTE byInitVec[8];   //初始向量  
  477.         BYTE byTemp[8];  
  478.         BYTE output[8];  
  479.         memset(byInitVec, 0x00, sizeof(byInitVec));  
  480.         memset(byTemp,   0x00, sizeof(byTemp));  
  481.         memset(output,   0x00, sizeof(output));  
  482.   
  483.         memcpy(byInitVec, input, 8);  
  484.         BYTE bySubKey[3][16][48];       //秘钥  
  485.         memset(bySubKey, 0x01, sizeof(bySubKey));  
  486.   
  487.         int i = 0;  
  488.         int j = (nInLen >> 3);  
  489.   
  490.         //构造并生成SubKeys  
  491.         BYTE nKey = (BYTE)((nKeyLen >> 3) > 3 ? 3 : (nKeyLen >> 3));  
  492.         for (i = 0; i < nKey; i ++ )  
  493.         {  
  494.             SetSubKey(&bySubKey[i], &key[i << 3]);  
  495.         }  
  496.   
  497.         memcpy(output, input, 8);  
  498.         if (1 == nKey)  //单倍长Key(8字节)  
  499.         {  
  500.             j--;  
  501.             for (int i = 0; i < j;  ++ i)  
  502.             {  
  503.                 Xor(input  +  8 * (i  +  1), output, 8, output);  
  504.                 RunDes(output, 0, &bySubKey[0], output);  
  505.   
  506.                 //memcpy(byInitVec, output, 8);         //将输出设定为扭转变量  
  507.             }  
  508.         }  
  509.         else if (2 == nKey) //双倍长Key(16字节)  
  510.         {        
  511.             j -= 2;  
  512.             for (i = 0; i < j;  ++ i)  
  513.             {  
  514.                 Xor(input  +  8 * (i  +  1), output, 8, output);  
  515.                 RunDes(output, 0, &bySubKey[0], output);       //将输出设定为扭转变量   
  516.             }  
  517.             Xor(input  +  8 * ( ++ i), output, 8, output);        //最后一块数据和上面加密结果异或  
  518.             RunDes(output, 0, &bySubKey[0], output);  
  519.             RunDes(output, 1, &bySubKey[1], output);  
  520.             RunDes(output, 0, &bySubKey[0], output);  
  521.         }  
  522.         else  //三倍长Key(24字节)    尚未验证  
  523.         {  
  524.             //j -= 2;  
  525.             for (i = 0, j = (nInLen >> 3) - 2; i < j;  ++ i, input  += 8)  
  526.             {  
  527.                 Xor(input  +  8 * (i  +  1), output, 8, byTemp);  
  528.                 RunDes(byTemp, 0, &bySubKey[0], output);  
  529.   
  530.                 memcpy(byInitVec, output, 8);           //将输出设定为扭转变量  
  531.             }  
  532.             Xor(input  +  8 * i, output, 8, output);  
  533.             RunDes(output, 2, &bySubKey[0], output);  
  534.             RunDes(output, 1, &bySubKey[1], output);  
  535.             RunDes(output, 0, &bySubKey[0], output);  
  536.         }  
  537.         strings::AscToHex(output, _Length , OutData);  
  538.     }  
  539.   
  540.     //input中不要自己填补80 + 00....       初始向量固定为8字节的0  
  541.     void DoGPMac(string intText, string KEK, string &OutData)  
  542.     {  
  543.         BYTE byInData[256];  //密钥,输入数据  
  544.         BYTE byEnter[256];  
  545.         BYTE byResult[256];  //算法模式,算法操作,输入,结果  
  546.         int nInLen;  
  547.         int nKeyLen;  
  548.         BYTE Output[512]={0};  
  549.         BYTE Input[512]={0};  
  550.         BYTE Key[512]={0};  
  551.         BYTE *input=Input;  
  552.         BYTE *key=Key;  
  553.         BYTE *output=Output;  
  554.   
  555.         nInLen=intText.length()/2;  
  556.         nKeyLen=KEK.length()/2;  
  557.   
  558.         strings::HexToAsc(intText, input);  
  559.         strings::HexToAsc(KEK, key);  
  560.   
  561.         memset(byInData, 0x00, sizeof(byInData));  
  562.         memcpy(byInData, input, nInLen);  
  563.         byInData[nInLen] = 0x80;  
  564.         nInLen ++ ;  
  565.         nInLen  += (8 - nInLen % 8);  //80  +  (nInLen % 8)个00  
  566.   
  567.         int j = 0;  
  568.         memset(byResult, 0x00, sizeof(byResult));  
  569.         for (int i = 0; i < nInLen / 8; i ++ )  
  570.         {  
  571.             memset(byEnter, 0x00, sizeof(byEnter));  
  572.             for (j = 0; j < 8; j ++ )  
  573.             {  
  574.                 byEnter[j  +  8] = byResult[j] ^ byInData[8 * i  +  j];  //byEnter的前8字节(全0)为初始向量)  
  575.             }  
  576. //          DoSSMac(byEnter, 16, key, nKeyLen, byResult);     //特别注意  
  577.         }  
  578.   
  579.         memcpy(output, byResult, 8);  
  580.         strings::AscToHex(Output,strlen((char*)Output) , OutData);  
  581.     }  
  582.   
  583.   
  584.   
  585.     //ANSI X9.9 MAC  
  586.     void DoDesMac(const BYTE* input, int nInLen, const BYTE* key, BYTE nKeyLen, BYTE* output, const BYTE* init_Vector)  
  587.     {  
  588.         BYTE byVector[8];  
  589.         BYTE byTemp[8];  
  590.         BYTE byData[128];  
  591.   
  592.         memset(byVector, 0x00, sizeof(byVector));  
  593.         memset(byTemp,   0x00, sizeof(byTemp));  
  594.         memset(byData,   0x00, sizeof(byData));  
  595.   
  596.         BYTE bySubKey[3][16][48];       //秘钥  
  597.   
  598.         memset(bySubKey, 0x01, sizeof(bySubKey));  
  599.   
  600.         //构造并生成SubKeys  
  601.         BYTE nKey = (nKeyLen >> 3) > 3 ? 3 : (nKeyLen >> 3);  
  602.         for (int i = 0; i < nKey; i++)  
  603.         {  
  604.             SetSubKey(&bySubKey[i], &key[i << 3]);  
  605.         }  
  606.   
  607.         int j = nInLen >> 3;  
  608.   
  609.         if (init_Vector != NULL)  
  610.         {  
  611.             memcpy(byVector, init_Vector, 8);  
  612.         }  
  613.   
  614.         if (1 == nKey)  //单倍长Key(8字节)  
  615.         {  
  616.             for (int i = 0; i < j; ++i, input += 8)  
  617.             {  
  618.                 Xor(input, byVector, 8, byTemp);  
  619.                 RunDes(byTemp, ENCRYPT, &bySubKey[0], output);  
  620.   
  621.                 memcpy(byVector, output, 8);            //将输出设定为扭转变量  
  622.             }  
  623.         }  
  624.         else if (2 == nKey) //双倍长Key(16字节)  
  625.         {  
  626.             for (int i = 0; i < j; ++i, input += 8)  
  627.             {  
  628.                 Xor(input, byVector, 8, byTemp);  
  629.                 RunDes(byTemp, ENCRYPT, &bySubKey[0], output);  
  630.                 RunDes(output, DECRYPT, &bySubKey[1], output);  
  631.                 RunDes(output, ENCRYPT, &bySubKey[0], output);  
  632.   
  633.                 memcpy(byVector, output, 8);            //将输出设定为扭转变量  
  634.             }  
  635.         }  
  636.         else  //三倍长Key(24字节)    尚未验证  
  637.         {  
  638.             for (int i = 0; i < j; ++i, input += 8)  
  639.             {  
  640.                 Xor(input, byVector, 8, byTemp);  
  641.                 RunDes(byTemp, ENCRYPT, &bySubKey[0], output);  
  642.                 RunDes(output, DECRYPT, &bySubKey[1], output);  
  643.                 RunDes(output, ENCRYPT, &bySubKey[2], output);  
  644.   
  645.                 memcpy(byVector, output, 8);            //将输出设定为扭转变量  
  646.             }  
  647.         }  
  648.     }  
  649.   
  650.     //input中要有80+00.... input的前8字节作为初始向量  
  651.     void DoSSMac(const BYTE* input, int nInLen, const BYTE* key, BYTE nKeyLen, BYTE* output)  
  652.     {  
  653.         BYTE byInitVec[8];   //初始向量  
  654.         BYTE byTemp[8];  
  655.   
  656.         memset(byInitVec, 0x00, sizeof(byInitVec));  
  657.         memset(byTemp,   0x00, sizeof(byTemp));  
  658.   
  659.         memcpy(byInitVec, input, 8);  
  660.   
  661.         BYTE bySubKey[3][16][48];       //秘钥  
  662.   
  663.         memset(bySubKey, 0x01, sizeof(bySubKey));  
  664.   
  665.         int i = 0;  
  666.         int j = (nInLen >> 3);  
  667.   
  668.         //构造并生成SubKeys  
  669.         BYTE nKey = (nKeyLen >> 3) > 3 ? 3 : (nKeyLen >> 3);  
  670.         for (i = 0; i < nKey; i++)  
  671.         {  
  672.             SetSubKey(&bySubKey[i], &key[i << 3]);  
  673.         }  
  674.   
  675.         memcpy(output, input, 8);  
  676.         if (1 == nKey)  //单倍长Key(8字节)  
  677.         {  
  678.             j--;  
  679.             for (int i = 0; i < j; ++i)  
  680.             {  
  681.                 Xor(input + 8 * (i + 1), output, 8, output);  
  682.                 RunDes(output, 0, &bySubKey[0], output);  
  683.   
  684.                 //memcpy(byInitVec, output, 8);         //将输出设定为扭转变量  
  685.             }  
  686.         }  
  687.         else if (2 == nKey) //双倍长Key(16字节)  
  688.         {        
  689.             j -= 2;  
  690.             for (i = 0; i < j; ++i)  
  691.             {  
  692.                 Xor(input + 8 * (i + 1), output, 8, output);  
  693.                 RunDes(output, 0, &bySubKey[0], output);       //将输出设定为扭转变量   
  694.             }  
  695.             Xor(input + 8 * (++i), output, 8, output);        //最后一块数据和上面加密结果异或  
  696.             RunDes(output, 0, &bySubKey[0], output);  
  697.             RunDes(output, 1, &bySubKey[1], output);  
  698.             RunDes(output, 0, &bySubKey[0], output);  
  699.         }  
  700.         else  //三倍长Key(24字节)    尚未验证  
  701.         {  
  702.             //j -= 2;  
  703.             for (i = 0, j = (nInLen >> 3) - 2; i < j; ++i, input += 8)  
  704.             {  
  705.                 Xor(input + 8 * (i + 1), output, 8, byTemp);  
  706.                 RunDes(byTemp, 0, &bySubKey[0], output);  
  707.   
  708.                 memcpy(byInitVec, output, 8);           //将输出设定为扭转变量  
  709.             }  
  710.             Xor(input + 8 * i, output, 8, output);  
  711.             RunDes(output, 2, &bySubKey[0], output);  
  712.             RunDes(output, 1, &bySubKey[1], output);  
  713.             RunDes(output, 0, &bySubKey[0], output);  
  714.         }  
  715.     }  
  716.   
  717.     //input中不要自己填补80+00....       初始向量固定为8字节的0  
  718.     void DoGPMac(const BYTE* input, int nInLen, const BYTE* key, int nKeyLen, BYTE* output)  
  719.     {  
  720.         BYTE byInData[256];  //密钥,输入数据  
  721.         BYTE byEnter[256];  
  722.         BYTE byResult[256];  //算法模式,算法操作,输入,结果  
  723.   
  724.         memset(byInData, 0x00, sizeof(byInData));  
  725.         memcpy(byInData, input, nInLen);  
  726.         byInData[nInLen] = 0x80;  
  727.         nInLen++;  
  728.         nInLen += (8 - nInLen % 8);  //80 + (nInLen % 8)个00  
  729.   
  730.         int j = 0;  
  731.         memset(byResult, 0x00, sizeof(byResult));  
  732.         for (int i = 0; i < nInLen / 8; i++)  
  733.         {  
  734.             memset(byEnter, 0x00, sizeof(byEnter));  
  735.             for (j = 0; j < 8; j++)  
  736.             {  
  737.                 byEnter[j + 8] = byResult[j] ^ byInData[8 * i + j];  //byEnter的前8字节(全0)为初始向量)  
  738.             }  
  739.                     DoSSMac(byEnter, 16, key, (BYTE)nKeyLen, byResult);     //特别注意  
  740.         }  
  741.           
  742.         memcpy(output, byResult, 8);  
  743.     }  
  744.   
  745.     string DesVerify(string Stxt)  
  746.     {  
  747.         string OutPut;  
  748.         DoDes(ECB, ENCRYPT, "0000000000000000", Stxt, OutPut);  
  749.         return OutPut.substr(0, 6);  
  750.     }  
  751. }  


3. DES加解密工具


     Des工具可以实现Des,3Des,Mac,Disp(离散)等功能,支持批量Des计算(需选择File)。对数据不足8的倍数字节实现自动补齐。DES工具下载地址:http://download.csdn.net/detail/yxstars/7728833,软件界面如下:





   


文/yanxin8原创,获取更多信息请移步至yanxin8.com...

0 0