当前位置:编程学习 > 网站相关 >>

DES算法代码

这是之前的一篇文章,今天才发现之前没有把内容放进来。真是晕头了。幸好看了看评论,有人指出来了。实在是不好意思。现在把代码补上来;声明,不是我自己完全原创的。也是从网上找到一个别人的东西,然后改造了一下。
#ifndef CDES_H_CAESAR__DEF 
#define CDES_H_CAESAR__DEF 
 
/*! /Brief CDES类说明 
 *
 * 该类是DES和3DES算法类 
 */ 
class CDES   

public: 
    CDES(); 
    virtual ~CDES(); 
 
    //加密解密 
    enum     
    { 
        ENCRYPT = 0,    //! 加密 
        DECRYPT,            //! 解密 
    }; 
 
    //DES算法的模式 
    enum 
    { 
        ECB     =   0,  //! ECB模式 
        CBC             //! CBC模式 
    }; 
 
    typedef bool    (*PSubKey)[16][48]; 
 
    //Pad填充的模式 
    enum 
    { 
        PAD_ISO_1 = 0,  //! ISO_1填充:数据长度不足8比特的倍数,以0x00补足,如果为8比特的倍数,补8个0x00 
        PAD_ISO_2,      //! ISO_2填充:数据长度不足8比特的倍数,以0x80,0x00..补足,如果为8比特的倍数,补0x80,0x00..0x00 
        PAD_PKCS_7      //! PKCS7填充:数据长度除8余数为n,以(8-n)补足为8的倍数 
    }; 
 
/* /Brief 补足8位数据 
 *
 * Description: 根据协议对加密前的数据进行填充
 * @param nType :类型:PAD类型
 * @param In :数据串指针
 * @param Out :填充输出串指针
 * @param datalen :数据的长度
 * @param padlen :(in,out)输出buffer的长度,填充后的长度
 * @return true--成功;false--失败; 
 */ 
    static bool RunPad(int nType,const unsigned char* In, 
        unsigned datalen,unsigned char* Out,unsigned& padlen); 
 
/* /Brief 执行DES算法对文本加解密
 *
 * Description :执行DES算法对文本加解密
 * @param bType : 类型:加密ENCRYPT,解密DECRYPT
 * @param bMode : 模式:ECB,CBC
 * @param In :待加密串指针
 * @param Out :待输出串指针
 * @param datalen :待加密串的长度,同时Out的缓冲区大小应大于或者等于datalen
 * @param Key :密钥(可为8位,16位,24位)支持3密钥
 * @param keylen : 密钥长度,多出24位部分将被自动裁减
 * @return true--成功;false--失败;  www.zzzyk.com
 */ 
    static bool RunDes(bool bType,bool bMode,const unsigned char* In, 
        unsigned char* Out,unsigned datalen,const unsigned char* Key,unsigned keylen); 
 
protected: 
    //计算并填充子密钥到SubKey数据中 
    static void SetSubKey(PSubKey pSubKey, const unsigned char Key[8]); 
     
    //DES单元运算 
    static void DES(unsigned char Out[8], const unsigned char In[8], 
         const PSubKey pSubKey, bool Type); 
}; 
 
#endif//CDES_H_CAESAR__DEF 

 

实现代码如下:
// DES.cpp: implementation of the CDES class. 
// 
////////////////////////////////////////////////////////////////////// 
#include "DES.h" 
#include "memory.h" 
#include <iostream> 
using namespace std;  
 
//////////////////////////////////////////////////////////////////////// 
// initial permutation IP 
const char IP_Table[64] = { 
    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 
}; 
// final permutation IP^-1  
const char IPR_Table[64] = { 
    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 
}; 
// expansion operation matrix 
const char E_Table[48] = { 
    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 
}; 
// 32-bit permutation function P used on the output of the S-boxes  
const char P_Table[32] = { 
    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 
}; 
// permuted choice table (key)  
const char PC1_Table[56] = { 
    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 
}; 
// permuted choice key (table)  
const char PC2_Table[48] = { 
    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 
}; 
// number left rotations of pc1  
const char LOOP_Table[16] = { 
    1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1 
}; 
// The (in)famous S-boxes  
const char S_Box[8][4][16] = { 
  &n

补充:综合编程 , 安全编程 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,