求 把C++代码装换成java代码

白玫瑰 发布于 2017/08/11 16:04
阅读 92
收藏 0

#include <stdio.h>
#include <malloc.h>
#include <fcntl.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <math.h>
#include <memory.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

#define FLINE    __FILE__, __LINE__

#ifndef DES_ENC
    #define DES_ENC 0  /*加密*/
    #define DES_DEC 1  /*解密*/
#endif    

/* 两个密钥分量 -- 测试 */

#define DESKEY_1 "fe60769259b39a6d4c13c8d7fd6b86a1"
#define DESKEY_2 "3d24d0dca83067380e0487fb2fd76f0a"


/*********************************************/

int pow2( unsigned int  pexp );
void Encrypt( char *input, char *key, char *output );
void Decrypt( char *input, char *key, char *output );
void init_data( char oldstr[16],char newstr[8] );
void getkey(char *keya,char *keyb,unsigned char *key );

/*3des加密解密函数,flg=0表示加密,flg=1表示解密*/
void tripedes( char pinput[16],char pkey[32],char poutput[16],int flg );

/*********************************************/

char sbox1[] =
{
   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
};


char sbox2[] =
{
   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
};


char sbox3[] =
{
   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
};


char sbox4[] =

    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
};


char sbox5[] =

    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
};


char sbox6[] =
{
   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
};

char sbox7[] =

    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
};


char sbox8[] =
{
   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
};

char *sbox[] =

   sbox1, 
   sbox2, 
   sbox3, 
   sbox4,
   sbox5,
   sbox6,
   sbox7,
   sbox8
};


/* 
**   for initial permutation
*/
char table1[] = 
{
   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
};

/* 
**   for permutation choice 1
*/
char table2[] =
{
   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
};

/* 
**   for expansion function
*/
char table3[] = 
{
   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
};

/* 
**   for permutation choice 2
*/
char table4[] = 
{
   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
};


/* 
**   for inverse of initial permutation
*/
char table5[] = 
{
   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
};


/* 
**   for permutation function P
*/
char table6[] = 
{
   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
};


/* 
**   for selection function
*/
char table7[] = 

    1, 6, 2, 1, 7,12, 8, 5,13,18,14, 9,19,24,20,13,
   25,30,26,17,31,36,32,21,37,42,38,25,43,48,44,29
};

/* 
**   for shift left 1
*/
char table8[] = 

    2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,
   18,19,20,21,22,23,24,25,26,27,28, 1,30,31,32,33,
   34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,
   50,51,52,53,54,55,56,29
};


/* 
**   for shift left 2
*/
char table9[] = 
{    
    3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,
   19,20,21,22,23,24,25,26,27,28, 1, 2,31,32,33,34,
   35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,
   51,52,53,54,55,56,29,30
};

/* 
**   for shift right 1
*/
char table10[] = 
{
   28, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,
   16,17,18,19,20,21,22,23,24,25,26,27,56,29,30,31,
   32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
   48,49,50,51,52,53,54,55
};


/* 
**   for shift right 2
*/
char table11[] = 
{
   27,28, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
   15,16,17,18,19,20,21,22,23,24,25,26,55,56,29,30,
   31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,
   47,48,49,50,51,52,53,54
};

/* 
**   content is table index,total count
*/
char *table[] =
{
   table1,table2,table3,table4, table5,table6,
   table7,table8,table9,table10,table11
};

unsigned char initial_permutation[] = { 0, 64 };
unsigned char permutation_c1[] = { 1, 56 };
unsigned char expansion_function[] = { 2, 48 };
unsigned char permutation_c2[] = { 3, 48 };
unsigned char inverse_ip[] = { 4, 64 };
unsigned char permutation_fp[] = { 5, 32 };
unsigned char shift_left1[] = { 7, 56 };
unsigned char shift_left2[] = { 8, 56 };
unsigned char shift_right1[] = { 9, 56 };
unsigned char shift_right2[] = { 10, 56 };
unsigned char x64[8],i64[8],k64[8],k56[7],k48[6],l32[4],r48[6],r32[4];


void main( int argc,char *argv[] )
{
    char desstr[17];

    if (atoi(argv[2])==DES_ENC)
        encrypt_3des(argv[1],desstr);
    else
        decrypt_3des(argv[1],desstr);

    printf("\n结果:%s\n",desstr );

}

void tripedes( char pinput[16],char pkey[32],char poutput[16],int flg )
{
    int i,j=0,mw[16];
    int inputtmp[16];
    unsigned char input[8],key1[8],key2[8],tmpkey1[16],tmpkey2[16];
    char output1[100],output2[100],desstr[17];

    memcpy( tmpkey1,pkey,16 );
    memcpy( tmpkey2,pkey+16,16 );

    init_data( pinput,input );
    init_data( tmpkey1,key1 );
    init_data( tmpkey2,key2 );


    if( flg==0 ) /*加密*/
    {
        Encrypt( input, key1, output1 );
        Decrypt( output1, key2, output2 );
        Encrypt( output2, key1, output1 );
    }
    else /*解密*/
    {
        Decrypt( input, key1, output1 );
        Encrypt( output1, key2, output2 );
        Decrypt( output2, key1, output1 );
    }


    for (i=0;i<8;i++)  {
        mw[2*i]=8*((output1[i]>>7)%2)+4*((output1[i]>>6)%2)+2*((output1[i]>>5)%2)+((output1[i]>>4)%2);
        mw[2*i+1]=8*((output1[i]>>3)%2)+4*((output1[i]>>2)%2)+2*((output1[i]>>1)%2)+((output1[i])%2);
    }
    for (i=0;i<16;i++)  {
        if( mw[i]>=0 && mw[i]<=9 )
            desstr[i]=mw[i]+'0';
        else
            desstr[i]=mw[i]+'A'-10;
    }
    desstr[16]=0;
    strcpy( poutput,desstr );

}

void getkey(char *keya,char *keyb,unsigned char *key )
{
    int tmpkey1[32],tmpkey2[32];
    int i,j,k;
    int tkey[128],mw[32];
    for(i=0;i<32;i++)
    {
        tmpkey1[i]=keya[i]>'9'?keya[i]-'a'+10:keya[i]-'0';
        tmpkey2[i]=keyb[i]>'9'?keyb[i]-'a'+10:keyb[i]-'0';
    }
    for(i=0;i<128;i++)
    {
        tkey[i]=((tmpkey1[i/4]>>(3-i%4))%2+(tmpkey2[i/4]>>(3-i%4))%2)%2;
    }
    for (i=0;i<32;i++)  {
        mw[i]=8*tkey[4*i]+4*tkey[4*i+1]+2*tkey[4*i+2]+tkey[4*i+3];
    }
    for (i=0;i<32;i++)  {
        if( mw[i]>=0 && mw[i]<=9 )
            key[i]=mw[i]+'0';
        else
            key[i]=mw[i]+'a'-10;
    }

}

void init_data( char oldstr[16],char newstr[8] )
{
    unsigned int inputtmp[16],i,j;
    unsigned char desstr[17],input[8];

    for( i=0;i<16;i++ )
    {
        inputtmp[i]=oldstr[i]>'9'? oldstr[i]-'a'+10: oldstr[i]-'0';
    }
    for( i=0;i<8;i++ )
        input[i]=0;

    for( i=0;i<16;i++  )
        for( j=0;j<4;j++  )
        {
            input[i/2]|=(inputtmp[i]&pow2(3-j))*(1*(i%2)+16*((i+1)%2));
        }
    strcpy( newstr,input );
}

void transdata( input,
                output,
                function )
unsigned char *input;
unsigned char *output;
unsigned char *function;
{
   int i;
   unsigned char out[8];

   /* 
   **   initial value with 0
   */
   for ( i=0; i<function[1]>>3; i++ )
   {
       out[i]=0;
   }

   for ( i=0; i<(int)function[1]; i++ )
   {
       out[i>>3] |= ( ( input[table[function[0]][i]-1 >> 3]
                      & ( 0x80 >> ( table[function[0]][i]-1 & 0x7 ) ) )
                      << ( table[function[0]][i]-1 & 0x7 ) ) >> ( i & 0x7 );
   }

   /* 
   **   initial value with 0
   */
   for ( i=0; i<function[1]>>3; i++ )
   {
       output[i]=out[i];
   }
}

void DesSel( input, output )
unsigned char *input;
unsigned char *output;
{
   int idx,i,j,k,cnt;

   /* 
   **   initial value with 0
   */
   for ( i=0; i<4; i++ )
   {
       output[i] = 0;
   }

   cnt = 0;
   for ( i=0; i<8; i++ )
   {
       for ( j=5, k=0, idx=0; j>=0; j-- )
       {
           idx+=((input[table7[cnt]-1+k>>3] & (0x80>>(table7[cnt]-1+k & 0x7)))
                ? 1 <<j : 0);

           if ( j>3 )
           {
              cnt++;
           }
           else
           {
              k++;
           }
       }

       cnt++;
       for ( j=0; j<4; j++ )
       {
           output[table7[cnt]+j-1 >> 3] |= ((sbox[i][idx] & (0x08 >> j)) << 4+j)
                                        >> (table7[cnt]+j-1 & 0x7);
       }
       cnt++;
   }
}

/* 
**   io->r32 
**   input->k56 
*/
void fnct( io, input )
unsigned char *io;
unsigned char *input;
{
   unsigned char k48[6],r48[6],s32[4];
   int i;

   /* 
   **   28+28 to 48
   */
   transdata( input, k48, permutation_c2 ); 

   /* 
   **   32 to 48
   */
   transdata( io, r48, expansion_function ); 

   /* 
   **   expn xor key
   */
   for ( i=0; i<6; i++ )
   {
       r48[i] ^= k48[i];
   }

   DesSel( r48, s32 );

   transdata( s32, io, permutation_fp );
}

/* 
**   input->x64 
**   key->k64 
**   output->i64 
*/
void Encrypt( input, 
              key, 
              output )
unsigned char *input; 
unsigned char *key; 
unsigned char *output;
{
   unsigned char i64[8],k56[7],t32[4];
   int i,j;

   transdata( input, i64, initial_permutation );

   transdata( key, k56, permutation_c1 );

   /*
   **   shift left 1
   */
   transdata( k56, k56, shift_left1 );

   t32[0] = i64[4]; 
   t32[1] = i64[5]; 
   t32[2] = i64[6]; 
   t32[3] = i64[7];

   fnct( i64+4, k56 );

   i64[4] ^= i64[0]; 
   i64[5] ^= i64[1]; 
   i64[6] ^= i64[2]; 
   i64[7] ^= i64[3];
   i64[0]  = t32[0]; 
   i64[1]  = t32[1]; 
   i64[2]  = t32[2]; 
   i64[3]  = t32[3];

   /*
   **   shift left 1
   */
   transdata( k56, k56, shift_left1 );

   t32[0] = i64[4]; 
   t32[1] = i64[5]; 
   t32[2] = i64[6]; 
   t32[3] = i64[7];

   fnct( i64+4, k56 );

   i64[4] ^= i64[0]; 
   i64[5] ^= i64[1]; 
   i64[6] ^= i64[2]; 
   i64[7] ^= i64[3];
   i64[0]  = t32[0]; 
   i64[1]  = t32[1]; 
   i64[2]  = t32[2]; 
   i64[3]  = t32[3];

   /*
   **   shift left 2
   */
   transdata(k56,k56,shift_left2);

   t32[0] = i64[4]; 
   t32[1] = i64[5]; 
   t32[2] = i64[6]; 
   t32[3] = i64[7];

   fnct( i64+4, k56 );

   i64[4] ^= i64[0]; 
   i64[5] ^= i64[1]; 
   i64[6] ^= i64[2]; 
   i64[7] ^= i64[3];
   i64[0]  = t32[0]; 
   i64[1]  = t32[1]; 
   i64[2]  = t32[2]; 
   i64[3]  = t32[3];

   /*
   **   shift left 2
   */
   transdata( k56, k56, shift_left2 );

   t32[0] = i64[4]; 
   t32[1] = i64[5]; 
   t32[2] = i64[6]; 
   t32[3] = i64[7];

   fnct(i64+4,k56);

   i64[4] ^= i64[0]; 
   i64[5] ^= i64[1]; 
   i64[6] ^= i64[2]; 
   i64[7] ^= i64[3];
   i64[0]  = t32[0]; 
   i64[1]  = t32[1]; 
   i64[2]  = t32[2]; 
   i64[3]  = t32[3];

   /*
   **   shift left 2
   */
   transdata( k56, k56, shift_left2 );

   t32[0] = i64[4]; 
   t32[1] = i64[5]; 
   t32[2] = i64[6]; 
   t32[3] = i64[7];

   fnct( i64+4, k56 );

   i64[4] ^= i64[0]; 
   i64[5] ^= i64[1]; 
   i64[6] ^= i64[2]; 
   i64[7] ^= i64[3];
   i64[0]  = t32[0]; 
   i64[1]  = t32[1]; 
   i64[2]  = t32[2]; 
   i64[3]  = t32[3];

   /*
   **   shift left 2 
   */
   transdata( k56, k56, shift_left2 );

   t32[0] = i64[4]; 
   t32[1] = i64[5];
   t32[2] = i64[6];
   t32[3] = i64[7];

   fnct( i64+4, k56 );

   i64[4] ^= i64[0]; 
   i64[5] ^= i64[1]; 
   i64[6] ^= i64[2]; 
   i64[7] ^= i64[3];
   i64[0]  = t32[0]; 
   i64[1]  = t32[1]; 
   i64[2]  = t32[2]; 
   i64[3]  = t32[3];

   /*
   **   shift left 2 
   */
   transdata( k56, k56, shift_left2 );

   t32[0] = i64[4];
   t32[1] = i64[5];
   t32[2] = i64[6];
   t32[3] = i64[7];

   fnct(i64+4,k56);

   i64[4] ^= i64[0];
   i64[5] ^= i64[1];
   i64[6] ^= i64[2];
   i64[7] ^= i64[3];
   i64[0]  = t32[0];
   i64[1]  = t32[1];
   i64[2]  = t32[2];
   i64[3]  = t32[3];

   /*
   **   shift left 2 
   */
   transdata( k56, k56, shift_left2 );

   t32[0] = i64[4]; 
   t32[1] = i64[5]; 
   t32[2] = i64[6]; 
   t32[3] = i64[7];

   fnct( i64+4, k56 );

   i64[4] ^= i64[0]; 
   i64[5] ^= i64[1]; 
   i64[6] ^= i64[2]; 
   i64[7] ^= i64[3];
   i64[0]  = t32[0]; 
   i64[1]  = t32[1]; 
   i64[2]  = t32[2]; 
   i64[3]  = t32[3];

   /*
   **   shift left 1 
   */
   transdata( k56, k56, shift_left1 );

   t32[0] = i64[4];
   t32[1] = i64[5];
   t32[2] = i64[6];
   t32[3] = i64[7];

   fnct( i64+4, k56 );

   i64[4] ^= i64[0];
   i64[5] ^= i64[1];
   i64[6] ^= i64[2];
   i64[7] ^= i64[3];
   i64[0]  = t32[0];
   i64[1]  = t32[1];
   i64[2]  = t32[2];
   i64[3]  = t32[3];

   /*
   **   shift left 2 
   */
   transdata( k56, k56, shift_left2 );

   t32[0] = i64[4];
   t32[1] = i64[5];
   t32[2] = i64[6];
   t32[3] = i64[7];

   fnct( i64+4, k56 );

   i64[4] ^= i64[0];
   i64[5] ^= i64[1];
   i64[6] ^= i64[2];
   i64[7] ^= i64[3];
   i64[0]  = t32[0];
   i64[1]  = t32[1];
   i64[2]  = t32[2];
   i64[3]  = t32[3];

   /*
   **   shift left 2
   */
   transdata( k56, k56, shift_left2 );

   t32[0] = i64[4];
   t32[1] = i64[5];
   t32[2] = i64[6];
   t32[3] = i64[7];

   fnct( i64+4, k56 );

   i64[4] ^= i64[0];
   i64[5] ^= i64[1];
   i64[6] ^= i64[2];
   i64[7] ^= i64[3];
   i64[0]  = t32[0];
   i64[1]  = t32[1];
   i64[2]  = t32[2];
   i64[3]  = t32[3];

   /*
   **   shift left 2
   */
   transdata( k56, k56, shift_left2 );

   t32[0] = i64[4];
   t32[1] = i64[5];
   t32[2] = i64[6];
   t32[3] = i64[7];

   fnct( i64+4, k56 );

   i64[4] ^= i64[0];
   i64[5] ^= i64[1];
   i64[6] ^= i64[2];
   i64[7] ^= i64[3];
   i64[0]  = t32[0];
   i64[1]  = t32[1];
   i64[2]  = t32[2];
   i64[3]  = t32[3];

   /*
   **   shift left 2
   */
   transdata( k56, k56, shift_left2 );

   t32[0] = i64[4];
   t32[1] = i64[5];
   t32[2] = i64[6];
   t32[3] = i64[7];

   fnct( i64+4, k56 );

   i64[4] ^= i64[0];
   i64[5] ^= i64[1];
   i64[6] ^= i64[2];
   i64[7] ^= i64[3];
   i64[0]  = t32[0];
   i64[1]  = t32[1];
   i64[2]  = t32[2];
   i64[3]  = t32[3];

   /*
   **   shift left 2
   */
   transdata( k56, k56, shift_left2 );

   t32[0] = i64[4];
   t32[1] = i64[5];
   t32[2] = i64[6];
   t32[3] = i64[7];

   fnct( i64+4, k56 );

   i64[4] ^= i64[0];
   i64[5] ^= i64[1];
   i64[6] ^= i64[2];
   i64[7] ^= i64[3];
   i64[0]  = t32[0];
   i64[1]  = t32[1];
   i64[2]  = t32[2];
   i64[3]  = t32[3];

   /*
   **   shift left 2
   */
   transdata( k56, k56, shift_left2 );

   t32[0] = i64[4];
   t32[1] = i64[5];
   t32[2] = i64[6];
   t32[3] = i64[7];

   fnct( i64+4, k56 );

   i64[4] ^= i64[0];
   i64[5] ^= i64[1];
   i64[6] ^= i64[2];
   i64[7] ^= i64[3];
   i64[0]  = t32[0];
   i64[1]  = t32[1];
   i64[2]  = t32[2];
   i64[3]  = t32[3];

   /*
   **   shift left 1
   */
   transdata( k56, k56, shift_left1 );

   t32[0] = i64[4];
   t32[1] = i64[5];
   t32[2] = i64[6];
   t32[3] = i64[7];

   fnct( i64+4, k56 );

   i64[4] ^= i64[0];
   i64[5] ^= i64[1];
   i64[6] ^= i64[2];
   i64[7] ^= i64[3];
   i64[0]  = t32[0];
   i64[1]  = t32[1];
   i64[2]  = t32[2];
   i64[3]  = t32[3];

   /*
   **   substitute
   */
   t32[0] = i64[4];
   t32[1] = i64[5];
   t32[2] = i64[6];
   t32[3] = i64[7];

   i64[4] = i64[0];
   i64[5] = i64[1];
   i64[6] = i64[2];
   i64[7] = i64[3];

   i64[0] = t32[0];
   i64[1] = t32[1];
   i64[2] = t32[2];
   i64[3] = t32[3];

   transdata( i64, output, inverse_ip );
}


/*
**   input->x64 
**     key->k64
**  output->i64
*/
void Decrypt( input,
              key,
              output )
unsigned char *input;
unsigned char *key;
unsigned char *output;
{
   unsigned char i64[8],k56[7],t32[4];

   transdata( input, i64, initial_permutation );

   /*
   **   substitute
   */
   t32[0] = i64[4];
   t32[1] = i64[5];
   t32[2] = i64[6];
   t32[3] = i64[7];

   i64[4] = i64[0];
   i64[5] = i64[1];
   i64[6] = i64[2];
   i64[7] = i64[3];

   i64[0] = t32[0];
   i64[1] = t32[1];
   i64[2] = t32[2];
   i64[3] = t32[3];

   transdata( key, k56, permutation_c1 );

   /*
   **   shift right 1
   */
   t32[0] = i64[0];
   t32[1] = i64[1];
   t32[2] = i64[2];
   t32[3] = i64[3];

   fnct( i64, k56 );

   i64[0] ^= i64[4];
   i64[1] ^= i64[5];
   i64[2] ^= i64[6];
   i64[3] ^= i64[7];
   i64[4]  = t32[0];
   i64[5]  = t32[1];
   i64[6]  = t32[2];
   i64[7]  = t32[3];

   transdata( k56, k56, shift_right1 );

   /*
   **   shift right 2
   */
   t32[0] = i64[0];
   t32[1] = i64[1];
   t32[2] = i64[2];
   t32[3] = i64[3];

   fnct( i64, k56 );

   i64[0] ^= i64[4];
   i64[1] ^= i64[5];
   i64[2] ^= i64[6];
   i64[3] ^= i64[7];
   i64[4]  = t32[0];
   i64[5]  = t32[1];
   i64[6]  = t32[2];
   i64[7]  = t32[3];

   transdata( k56, k56, shift_right2 );

   /*
   **   shift right 2
   */
   t32[0] = i64[0];
   t32[1] = i64[1];
   t32[2] = i64[2];
   t32[3] = i64[3];

   fnct( i64, k56 );

   i64[0] ^= i64[4];
   i64[1] ^= i64[5];
   i64[2] ^= i64[6];
   i64[3] ^= i64[7];
   i64[4]  = t32[0];
   i64[5]  = t32[1];
   i64[6]  = t32[2];
   i64[7]  = t32[3];

   transdata( k56, k56, shift_right2 );

   /*
   **   shift right 2
   */
   t32[0] = i64[0];
   t32[1] = i64[1];
   t32[2] = i64[2];
   t32[3] = i64[3];

   fnct( i64, k56 );

   i64[0] ^= i64[4];
   i64[1] ^= i64[5];
   i64[2] ^= i64[6];
   i64[3] ^= i64[7];
   i64[4]  = t32[0];
   i64[5]  = t32[1];
   i64[6]  = t32[2];
   i64[7]  = t32[3];

   transdata( k56, k56, shift_right2 );

   /*
   **   shift right 2
   */
   t32[0] = i64[0];
   t32[1] = i64[1];
   t32[2] = i64[2];
   t32[3] = i64[3];

   fnct( i64, k56 );

   i64[0] ^= i64[4];
   i64[1] ^= i64[5];
   i64[2] ^= i64[6];
   i64[3] ^= i64[7];
   i64[4]  = t32[0];
   i64[5]  = t32[1];
   i64[6]  = t32[2];
   i64[7]  = t32[3];

   transdata( k56, k56, shift_right2 );

   /*
   **   shift right 2 
   */
   t32[0] = i64[0];
   t32[1] = i64[1];
   t32[2] = i64[2];
   t32[3] = i64[3];

   fnct( i64, k56 );

   i64[0] ^= i64[4];
   i64[1] ^= i64[5];
   i64[2] ^= i64[6];
   i64[3] ^= i64[7];
   i64[4]  = t32[0];
   i64[5]  = t32[1];
   i64[6]  = t32[2];
   i64[7]  = t32[3];

   transdata( k56, k56, shift_right2 );

   /*
   **   shift right 2
   */
   t32[0] = i64[0];
   t32[1] = i64[1];
   t32[2] = i64[2];
   t32[3] = i64[3];

   fnct( i64, k56 );

   i64[0] ^= i64[4];
   i64[1] ^= i64[5];
   i64[2] ^= i64[6];
   i64[3] ^= i64[7];
   i64[4]  = t32[0];
   i64[5]  = t32[1];
   i64[6]  = t32[2];
   i64[7]  = t32[3];

   transdata( k56, k56, shift_right2 );

   /*
   **   shift right 1
   */
   t32[0] = i64[0];
   t32[1] = i64[1];
   t32[2] = i64[2];
   t32[3] = i64[3];

   fnct(i64,k56);

   i64[0] ^= i64[4];
   i64[1] ^= i64[5];
   i64[2] ^= i64[6];
   i64[3] ^= i64[7];
   i64[4]  = t32[0];
   i64[5]  = t32[1];
   i64[6]  = t32[2];
   i64[7]  = t32[3];

   transdata( k56, k56, shift_right1 );

   /*
   **   shift right 2
   */
   t32[0] = i64[0];
   t32[1] = i64[1];
   t32[2] = i64[2];
   t32[3] = i64[3];

   fnct( i64, k56 );

   i64[0] ^= i64[4];
   i64[1] ^= i64[5];
   i64[2] ^= i64[6];
   i64[3] ^= i64[7];
   i64[4]  = t32[0];
   i64[5]  = t32[1];
   i64[6]  = t32[2];
   i64[7]  = t32[3];

   transdata( k56, k56, shift_right2 );

   /*
   **   shift right 2
   */
   t32[0] = i64[0];
   t32[1] = i64[1];
   t32[2] = i64[2];
   t32[3] = i64[3];

   fnct( i64, k56 );

   i64[0] ^= i64[4];
   i64[1] ^= i64[5];
   i64[2] ^= i64[6];
   i64[3] ^= i64[7];
   i64[4]  = t32[0];
   i64[5]  = t32[1];
   i64[6]  = t32[2];
   i64[7]  = t32[3];

   transdata( k56, k56, shift_right2 );

   /*
   **   shift right 2
   */
   t32[0] = i64[0];
   t32[1] = i64[1];
   t32[2] = i64[2];
   t32[3] = i64[3];

   fnct( i64, k56 );

   i64[0] ^= i64[4];
   i64[1] ^= i64[5];
   i64[2] ^= i64[6];
   i64[3] ^= i64[7];
   i64[4]  = t32[0];
   i64[5]  = t32[1];
   i64[6]  = t32[2];
   i64[7]  = t32[3];

   transdata( k56, k56, shift_right2 );

   /*
   **   shift right 2
   */
   t32[0] = i64[0];
   t32[1] = i64[1];
   t32[2] = i64[2];
   t32[3] = i64[3];

   fnct( i64, k56 );

   i64[0] ^= i64[4];
   i64[1] ^= i64[5];
   i64[2] ^= i64[6];
   i64[3] ^= i64[7];
   i64[4]  = t32[0];
   i64[5]  = t32[1];
   i64[6]  = t32[2];
   i64[7]  = t32[3];

   transdata( k56, k56, shift_right2 );

   /*
   **   shift right 2
   */
   t32[0] = i64[0];
   t32[1] = i64[1];
   t32[2] = i64[2];
   t32[3] = i64[3];

   fnct( i64, k56 );

   i64[0] ^= i64[4];
   i64[1] ^= i64[5];
   i64[2] ^= i64[6];
   i64[3] ^= i64[7];
   i64[4]  = t32[0];
   i64[5]  = t32[1];
   i64[6]  = t32[2];
   i64[7]  = t32[3];

   transdata( k56, k56, shift_right2 );

   /*
   **   shift right 2
   */
   t32[0] = i64[0];
   t32[1] = i64[1];
   t32[2] = i64[2];
   t32[3] = i64[3];

   fnct( i64, k56 );

   i64[0] ^= i64[4];
   i64[1] ^= i64[5];
   i64[2] ^= i64[6];
   i64[3] ^= i64[7];
   i64[4]  = t32[0];
   i64[5]  = t32[1];
   i64[6]  = t32[2];
   i64[7]  = t32[3];

   transdata( k56, k56, shift_right2 );

   /*
   **   shift right 1
   */
   t32[0] = i64[0];
   t32[1] = i64[1];
   t32[2] = i64[2];
   t32[3] = i64[3];

   fnct( i64, k56 );

   i64[0] ^= i64[4];
   i64[1] ^= i64[5];
   i64[2] ^= i64[6];
   i64[3] ^= i64[7];
   i64[4]  = t32[0];
   i64[5]  = t32[1];
   i64[6]  = t32[2];
   i64[7]  = t32[3];

   transdata( k56, k56, shift_right1 );

   /*
   **   shift right 1 
   */
   t32[0] = i64[0];
   t32[1] = i64[1];
   t32[2] = i64[2];
   t32[3] = i64[3];

   fnct( i64, k56 );

   i64[0] ^= i64[4];
   i64[1] ^= i64[5];
   i64[2] ^= i64[6];
   i64[3] ^= i64[7];
   i64[4]  = t32[0];
   i64[5]  = t32[1];
   i64[6]  = t32[2];
   i64[7]  = t32[3];

   transdata( k56, k56, shift_right1 );

   /*
   **   substitute
   */
   transdata( i64, output, inverse_ip );

}

int pow2( unsigned int  pexp )
{
    int i,r;
    r=1;
    for( i=0;i<pexp;i++ )    
        r*=2;
    return r;
}

/* 加密 */
int encrypt_3des(char *pin, char *pout)
{
    return TripleDES(pin, pout, DES_ENC);
}

/* 解密 */
int decrypt_3des(char *pin, char *pout)
{
    return TripleDES(pin, pout, DES_DEC);
}

int TripleDES(char *pin, char *pout, int flg)
{
    unsigned char   key[32];
    char            in1[17], desstr[17], desstr2[17];
    char            keya[33], keyb[33];
    char            tmpstr[32];
    int             len;

    memset(in1, '\0', sizeof(in1));
    memset(desstr, '\0', sizeof(desstr));

    strcpy(keya, DESKEY_1);  /* 分量1 */
    strcpy(keyb, DESKEY_2);  /* 分量2 */

    if ( flg != DES_ENC && flg != DES_DEC )
    {
        printf("加/解密标志错[%d]", flg);
        return -1;
    }

    len = strlen(pin);
    if ( len > 14 && flg == DES_ENC )
    {
        printf("待加密的明文长度不能大于14");
        return -1;
    }

    if ( len != 16 && flg == DES_DEC )
    {
        printf("待解密的密文长度必须等于16");
        return -1;
    }

    if ( flg == DES_ENC ) /* 加密 */
    {
        memset(in1, 'F', 16);
        sprintf(tmpstr, "%02d%s", len, pin);
        memcpy(in1, tmpstr, strlen(tmpstr));
    } else {
        strcpy(in1, pin);
    }

    getkey(keya, keyb, key);

    tripedes(in1, key, desstr, flg);

    if (flg==DES_ENC)
    {
        strcpy(pout, desstr);
    }
    else
    {
        memset(tmpstr,0x00,sizeof(tmpstr));
        strncpy(tmpstr,desstr,2);
        if (atoi(tmpstr)>14)
        {
            printf("被解密的密码明文长度超过14位");
            return -1;
        }
        strncpy(pout,desstr+2,atoi(tmpstr));
    }    

    return 0;
}
 

加载中
返回顶部
顶部