最近在做一个项目中遇上了这么一个问题:要求加密算法采用3-DES算法,密钥为192位(以3DES按ECB模式加密算法加密字符串)加密字符串。
由于本人第一次接触,也查看了前辈的思想,但是终不得要领,没办法搞定这个东西。请大家帮帮忙。
如能附源码或者指引地址,不胜感激!

解决方案 »

  1.   

    3-DES加密密钥为24 Byte
    就是192位
    我以前就网上的一段经典DES算法的封装// Des.h: interface for the CDes class.
    //
    //////////////////////////////////////////////////////////////////////#if !defined(AFX_DES_H__8000CEF3_A45A_46F6_B2B3_F2094BDB09EB__INCLUDED_)
    #define AFX_DES_H__8000CEF3_A45A_46F6_B2B3_F2094BDB09EB__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000#pragma warning(disable:4244)class CDes  
    {
    public:
    enum MODE { NoMode = -1, EN0, DE1};
    enum LEVEL { NoLevel = -1, DES1, DES2, DES3};

    MODE Mode;
    LEVEL Level;

    CDes();
    virtual ~CDes(); CDes(LEVEL level, MODE mode, char* password);
    void ResetDes(LEVEL level, MODE mode, char* password);
    int DoDes(unsigned char *pInput, int nLenInput, unsigned char * &pOutput);protected:
    //根据一个c字符串生成对应的密钥
    void makekey(char *aptr /* c_str */, unsigned char *kptr /* unsigned char[8] */);
    void make2key(char *aptr /* c_str */, unsigned char *kptr /* unsigned char[16] */);
    void make3key(char *aptr /* c_str */, unsigned char *kptr /* unsigned char[24] */); //设定对象的密钥和加/解密模式
    void deskey(const unsigned char *key /* hexkey[8] */, short edf/* MODE */);
    void des2key(const unsigned char *key /* hexkey[16] */, short mode/* MODE */);
    void des3key(const unsigned char *key /* hexkey[24] */, short mode/* MODE */); //用来做DES加密,分别是8位,16位,24位
    void des(unsigned char *from /* from[8] */, unsigned char *into /* to[8] */);
    void D2des(unsigned char *from /* from[16] */, unsigned char *into /* from[16] */);
    void D3des(unsigned char *from /* from[24] */, unsigned char *into /* from[24] */);private:
    //三个internal key
    unsigned long KnL[32] ;
    unsigned long KnR[32] ;
    unsigned long Kn3[32] ; //固定不变的常量数据
    const static unsigned char Df_Key[24];
    const static unsigned short bytebit[8];
    const static unsigned long bigbyte[24];
    const static unsigned char pc1[56];
    const static unsigned char totrot[16];
    const static unsigned char pc2[48];
    const static unsigned long SP1[64];
    const static unsigned long SP2[64];
    const static unsigned long SP3[64];
    const static unsigned long SP4[64];
    const static unsigned long SP5[64];
    const static unsigned long SP6[64];
    const static unsigned long SP7[64];
    const static unsigned long SP8[64]; void scrunch(unsigned char *, unsigned long *);
    void unscrun(unsigned long *, unsigned char *);
    void desfunc(unsigned long *, unsigned long *);
    void cookey(register unsigned long *raw1); //用来保存及恢复三个internal key的函数
    void usekey(unsigned long *from /* cookedkey[32] */);
    void use2key(unsigned long *from /* cookedkey[32] */);
    void use3key(unsigned long *from /* cookedkey[32] */);
    void cpkey(unsigned long *into /* cookedkey[32] */);
    void cp2key(unsigned long *into /* cookedkey[32] */);
    void cp3key(unsigned long *into /* cookedkey[32] */); //此函数只被makekey调用,用来根据缺省的16bit的key生成一个8bit的key
    void Ddes(unsigned char *from /* from[8] */, unsigned char *into /* from[8] */);
    };#endif // !defined(AFX_DES_H__8000CEF3_A45A_46F6_B2B3_F2094BDB09EB__INCLUDED_)
      

  2.   

    代码太长了,发不了
    要的话请发mail到 [email protected]
    我会发给你的