现在有两个文件,怎么样利用现有的Des算法给的三个接口去实现,用一个文件中的内容作密钥去加密另外一个文件,然后把加密后的文件写入第三个新文件。
假设a文件为待加密文件
b文件为密钥文件
接口为: void __fastcall SetKey(unsigned char *pInkey); // 初始化密匙
void __fastcall Encrypt(unsigned char *pInData, unsigned char *pOut); // 数据加密
void __fastcall Decrypt(unsigned char *pInData, unsigned char *pOut); // 数据解密

解决方案 »

  1.   

    csdn难道没有高手??何况这个问题不是很难吧~~
      

  2.   


    /* d3des.h -
     *
     * Headers and defines for d3des.c
     * Graven Imagery, 1992.
     *
     * Copyright (c) 1988,1989,1990,1991,1992 by Richard Outerbridge
     * (GEnie : OUTER; CIS : [71755,204])
     */#define D2_DES /* include double-length support */
    #define D3_DES /* include triple-length support */#ifdef D3_DES
    #ifndef D2_DES
    #define D2_DES /* D2_DES is needed for D3_DES */
    #endif
    #endif#define EN0 0 /* MODE == encrypt */
    #define DE1 1 /* MODE == decrypt *//* A useful alias on 68000-ish machines, but NOT USED HERE. */typedef union {
    unsigned long blok[2];
    unsigned short word[4];
    unsigned char byte[8];
    } M68K;extern void deskey(unsigned char *, short);
    /*       hexkey[8]     MODE
     * Sets the internal key register according to the hexadecimal
     * key contained in the 8 bytes of hexkey, according to the DES,
     * for encryption or decryption according to MODE.
     */extern void usekey(unsigned long *);
    /*     cookedkey[32]
     * Loads the internal key register with the data in cookedkey.
     */extern void cpkey(unsigned long *);
    /*    cookedkey[32]
     * Copies the contents of the internal key register into the storage
     * located at &cookedkey[0].
     */extern void des(unsigned char *, unsigned char *);
    /*     from[8]       to[8]
     * Encrypts/Decrypts (according to the key currently loaded in the
     * internal key register) one block of eight bytes at address 'from'
     * into the block at address 'to'.  They can be the same.
     */#ifdef D2_DES#define desDkey(a,b) des2key((a),(b))
    extern void des2key(unsigned char *, short);
    /*       hexkey[16]     MODE
     * Sets the internal key registerS according to the hexadecimal
     * keyS contained in the 16 bytes of hexkey, according to the DES,
     * for DOUBLE encryption or decryption according to MODE.
     * NOTE: this clobbers all three key registers!
     */extern void Ddes(unsigned char *, unsigned char *);
    /*     from[8]       to[8]
     * Encrypts/Decrypts (according to the keyS currently loaded in the
     * internal key registerS) one block of eight bytes at address 'from'
     * into the block at address 'to'.  They can be the same.
     */extern void D2des(unsigned char *, unsigned char *);
    /*     from[16]       to[16]
     * Encrypts/Decrypts (according to the keyS currently loaded in the
     * internal key registerS) one block of SIXTEEN bytes at address 'from'
     * into the block at address 'to'.  They can be the same.
     */extern void makekey(char *, unsigned char *);
    /* *password, single-length key[8]
     * With a double-length default key, this routine hashes a NULL-terminated
     * string into an eight-byte random-looking key, suitable for use with the
     * deskey() routine.
     */#define makeDkey(a,b) make2key((a),(b))
    extern void make2key(char *, unsigned char *);
    /* *password, double-length key[16]
     * With a double-length default key, this routine hashes a NULL-terminated
     * string into a sixteen-byte random-looking key, suitable for use with the
     * des2key() routine.
     */#ifndef D3_DES /* D2_DES only */#define useDkey(a) use2key((a))
    #define cpDkey(a) cp2key((a))extern void use2key(unsigned long *);
    /*     cookedkey[64]
     * Loads the internal key registerS with the data in cookedkey.
     * NOTE: this clobbers all three key registers!
     */extern void cp2key(unsigned long *);
    /*    cookedkey[64]
     * Copies the contents of the internal key registerS into the storage
     * located at &cookedkey[0].
     */#else /* D3_DES too */#define useDkey(a) use3key((a))
    #define cpDkey(a) cp3key((a))extern void des3key(unsigned char *, short);
    /*       hexkey[24]     MODE
     * Sets the internal key registerS according to the hexadecimal
     * keyS contained in the 24 bytes of hexkey, according to the DES,
     * for DOUBLE encryption or decryption according to MODE.
     */extern void use3key(unsigned long *);
    /*     cookedkey[96]
     * Loads the 3 internal key registerS with the data in cookedkey.
     */extern void cp3key(unsigned long *);
    /*    cookedkey[96]
     * Copies the contents of the 3 internal key registerS into the storage
     * located at &cookedkey[0].
     */extern void make3key(char *, unsigned char *);
    /* *password, triple-length key[24]
     * With a triple-length default key, this routine hashes a NULL-terminated
     * string into a twenty-four-byte random-looking key, suitable for use with
     * the des3key() routine.
     */#endif /* D3_DES */
    #endif /* D2_DES *//* d3des.h V5.09 rwo 9208.04 15:06 Graven Imagery
     ********************************************************************/
      

  3.   

    +class Encrypt  
    {
    private:
    unsigned char AskL(void);
    void ShiftR(void);
        void initialize(unsigned char *key);int OLDE;
    int OLDL;
    unsigned char A[24];       
    unsigned char B[8];       
    unsigned char D[8];
    unsigned char Bits[8][3];
    unsigned char ROM[256];
    #define loopshiftr(b,d) ((b>>d)|(b<<(8-d)))public:
    Encrypt();     
        int EN_DE_Code(unsigned char *buf,int size,unsigned char  *key); };
      

  4.   

    Encrypt::Encrypt()
    {}void Encrypt::initialize(unsigned char * key)
    {
    int i,j;OLDE = 1;
    OLDL = 1;A[0]=0x12,  A[1]=0x34,  A[2]=0x56,  A[3]=0x78,   A[4]=0x90;
    A[5]=0x12,  A[6]=0x34,  A[7]=0x56,  A[8]=0x78,   A[9]=0x90;
    A[10]=0x12, A[11]=0x34, A[12]=0x56, A[13]=0x78,  A[14]=0x90;
    A[15]=0x12, A[16]=0x34, A[17]=0x56, A[18]=0x78,  A[19]=0x90;
    A[20]=0x12, A[21]=0x34, A[22]=0x56, A[23]=0x78;B[0]=0x12,B[1]=0x34,B[2]=0x56,B[3]=0x78;
    B[4]=0x90,B[5]=0x12,B[6]=0x34,B[7]=0x56;        Bits[0][0]=1,Bits[0][1]=6,Bits[0][2]=7;
    Bits[1][0]=2,Bits[1][1]=4,Bits[1][2]=6;
    Bits[2][0]=3,Bits[2][1]=2,Bits[2][2]=8;
    Bits[3][0]=4,Bits[3][1]=5,Bits[3][2]=1;
    Bits[4][0]=5,Bits[4][1]=7,Bits[4][2]=3;
    Bits[5][0]=6,Bits[5][1]=3,Bits[5][2]=5;
    Bits[6][0]=7,Bits[6][1]=8,Bits[6][2]=4;
    Bits[7][0]=8,Bits[7][1]=1,Bits[7][2]=2;
    for(j=0,i=0;i<10;i++,j++)
      A[j*2]=key[i];
    for(j=1;i<16;i++,j++)
      B[j]=key[i];
     
    unsigned char InitROM[256] = {  
      0x99, 0xC7, 0x31, 0xB4, 0x89, 0xDC, 0x7C, 0x9C, 0x90, 0x80, 0xE7, 0xE5, 0xF9, 0x13, 0x73, 0x75,
      0x47, 0x9A, 0x65, 0x93, 0x2A, 0xA2, 0xB9, 0x1F, 0xA8, 0x51, 0x14, 0x98, 0x88, 0xD7, 0x70, 0x4D,
      0x6A, 0x17, 0x96, 0x84, 0x59, 0x7B, 0xB2, 0x32, 0x94, 0x34, 0x5D, 0x0A, 0xFA, 0x8D, 0x30, 0x8A,
      0xD1, 0x78, 0xBA, 0xCB, 0x21, 0x07, 0x38, 0x6B, 0xAF, 0xAE, 0xDE, 0x05, 0x82, 0xB1, 0x8C, 0xD0,
      0x86, 0xD2, 0xAC, 0x56, 0x2C, 0x1C, 0xE4, 0x9E, 0x33, 0x48, 0x18, 0x68, 0x6C, 0x11, 0xE3, 0xC0,
      0xE9, 0x7E, 0x2E, 0xC5, 0x3F, 0x61, 0xFC, 0xEF, 0x28, 0xB6, 0x8F, 0x81, 0x3D, 0x5A, 0x4A, 0xA1,
      0x09, 0xB0, 0x6D, 0xEC, 0xC4, 0x76, 0x15, 0x36, 0xEA, 0xBC, 0xD8, 0x69, 0x92, 0x97, 0x27, 0xB7,
      0x04, 0x3E, 0x1D, 0xC3, 0x0B, 0x4C, 0x1A, 0x19, 0x50, 0x4F, 0xC2, 0xCA, 0x8E, 0x83, 0xA5, 0x25,
      0x79, 0xE1, 0xA3, 0x2B, 0xE2, 0x22, 0x5B, 0x0E, 0xDB, 0x0D, 0xFF, 0xDA, 0xA0, 0x62, 0xF1, 0xB8,
      0x63, 0xD5, 0x45, 0x95, 0x8B, 0xC9, 0x49, 0x4B, 0x74, 0x10, 0x64, 0x3C, 0xF3, 0x1B, 0xEE, 0x42,
      0xDD, 0xAA, 0x00, 0x40, 0xCE, 0xE0, 0x5C, 0x54, 0x29, 0x60, 0x9D, 0x5F, 0xAB, 0xBB, 0x4E, 0xBF,
      0x7F, 0x41, 0xDF, 0xC6, 0x44, 0xD3, 0x0C, 0xF2, 0xD9, 0x66, 0xF6, 0x43, 0xFD, 0x39, 0x77, 0xF8,
      0x9F, 0x67, 0x1E, 0xCD, 0xC8, 0x58, 0xED, 0xFE, 0x03, 0x91, 0x01, 0xD6, 0xAD, 0x5E, 0x52, 0xA9,
      0xBE, 0x20, 0xB5, 0xFB, 0x0F, 0x16, 0x85, 0x23, 0x2F, 0xF5, 0x3A, 0x7D, 0xCC, 0x24, 0xA7, 0x9B,
      0xF7, 0x46, 0x53, 0xB3, 0xA4, 0x02, 0x08, 0xA6, 0x87, 0xD4, 0x26, 0x57, 0xC1, 0xF0, 0x12, 0x6E,
      0x06, 0xE8, 0xEB, 0xCF, 0x6F, 0x71, 0x3B, 0xBD, 0x2D, 0x7A, 0xF4, 0x55, 0x72, 0x37, 0x35, 0xE6
    };
      for(i=0;i<256;i++)
      ROM[i]=InitROM[i];}void Encrypt::ShiftR()
    {
    unsigned char c1, b5;
      int i, j1, j2, j3;
      c1 = A[23] + A[17] + A[9] + A[6];
      for(i = 23; i > 0; i--) A[i] = A[i-1];
      A[0] = c1;
      b5 = (B[0] + B[1]) ^ (B[6] + B[7]);
      for(i = 7; i > 0; i--) B[i] = B[i-1];
      B[0] = b5 + c1;
      for(i = 0; i < 8; i++)
      {
        j1 = 8 - Bits[i][0];
        j2 = 8 - Bits[i][1];
        j3 = 8 - Bits[i][2];
        D[i] = (((A[i] >> j1) & 1) << 2) + (((A[i+8] >> j2) & 1) << 1) + ((A[i+8] >> j3) & 1);
      }
      B[i] = loopshiftr(B[i], D[i]);}unsigned char Encrypt::AskL()
    {
     unsigned char C, D, E, R, E1;
      C = (B[2] ^ B[3]) + (B[4] ^ B[5]);
      D = C * ((B[6] + B[7]) ^ OLDL);  if(D == 0) D = 0x80;
      E = (B[0] + B[1]) ^ C;
      E1 = OLDE + E;
      OLDE = E;
      R = ROM[D] + E1;
      OLDL = R;
      ShiftR();
      return(R);
    }int Encrypt::EN_DE_Code(unsigned  char * buf, int size, unsigned char * key)
    {
    int  i;
    initialize(key);
    for(i=0;i<size;i++)
    {
    unsigned char c = buf[i];
    c ^= AskL();
    buf[i]=c;
    }
    return size;
    }