最近在做mac值3des加密解密算法,对方提供的是C语言的算法,我编译后得出来的值为什么和用php自带的一些方法算出来的值不同,小弟很苦恼,请各位高手帮忙看一下,最好是能把这个C语言方法转化成php版本,小弟不胜感激。。C语言版本:
#include <stdio.h>
#include <string.h>
#define IP_TABLE 0
#define IP_1_TABLE 1
#define E_TABLE 2
#define P_TABLE 3
#define PC1_TABLE 4
#define PC2_TABLE 5
typedef struct
{
BYTE L[32];
BYTE R[32];
}LRStruct;
typedef struct
{
BYTE C[28];
BYTE D[28];
}CDStruct;
typedef struct
{
BYTE b0 : 1;
BYTE b1 : 1;
BYTE b2 : 1;
BYTE b3 : 1;
BYTE b4 : 1;
BYTE b5 : 1;
BYTE b6 : 1;
BYTE b7 : 1;
}BYTEStruct;
char* Trim(char *In);
void ByteToBit(BYTE *src, BYTE *dst, int n);
void BitToByte(BYTE *src, BYTE *dst, int n);
int HexStrToBytes(char *strSou, BYTE *BytDes, int bytCount);
int BytesToHexStr(char *strDes, BYTE *bytSou, int bytCount);
void DesAlgo(BYTE *key, BYTE *src, BYTE *dst, BYTE bEncrypt);
void Shift(CDStruct *pCD, int iCircle, BYTE bEncrypt);
void RShift(BYTE *buf28, int nShift);
void LShift(BYTE *buf28, int nShift);
void fFunction(BYTE *R, BYTE*K, BYTE *result);
void S_Change(BYTE *src, BYTE *result);
void SelectExchangeFromTable(int nTable, BYTE *src, BYTE *dst);
//STD DES
////////////////////////////////////////////////////////
// g_nOut: number of output bits from each table
int g_nOut[6] = {64,64,48,32,56,48};
int TABLE_shift[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
                       
////////////////////////////////////////////////////////
// TABLE_ip: INITIAL PERMUTATION
int TABLE_ip[64] = {
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,
56,48,40,32,24,16, 8, 0,58,50,42,34,26,18,10, 2,
60,52,44,36,28,20,12, 4,62,54,46,38,30,22,14, 6};
////////////////////////////////////////////////////////
// TABLE_ip_1: INVERSE INITIAL PERMUTATION
int TABLE_ip_1[64] = {
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,32, 0,40, 8,48,16,56,24} ;
////////////////////////////////////////////////////////
// p: Permutation Function P
int TABLE_p[32] = {
15, 6,19,20,28,11,27,16, 0,14,22,25, 4,17,30, 9,
 1, 7,23,13,31,26, 2, 8,18,12,29, 5,21,10, 3,24};
////////////////////////////////////////////////////////
// pc_1: Permuted Choice1
int TABLE_pc_1[56] = {
56,48,40,32,24,16, 8, 0,57,49,41,33,25,17, 9, 1,
58,50,42,34,26,18,10, 2,59,51,43,35,62,54,46,38,
30,22,14, 6,61,53,45,37,29,21,13, 5,60,52,44,36,
28,20,12, 4,27,19,11, 3};
////////////////////////////////////////////////////////
// TABLE_e: Expansion Function
int TABLE_ePro[48] = {
31, 4, 0, 1, 2, 3, 3, 8, 4, 5, 6, 7, 7,12, 8, 9,
10,11,11,16,12,13,14,15,15,20,16,17,18,19,19,24,
20,21,22,23,23,28,24,25,26,27,27, 0,28,29,30,31} ;
////////////////////////////////////////////////////////
// pc_2: Permuted Choice2
int TABLE_pc_2Pro[48] = {
13, 4,16,10,23, 0, 2, 9,27,14, 5,20,22, 7,18,11,
 3,25,15, 1, 6,26,19,12,40,54,51,30,36,46,29,47,
39,50,44,32,43,52,48,38,55,33,45,31,41,49,35,28} ;
////////////////////////////////////////////////////////
// s: Selection Function (s1...s8)
BYTE TABLE_s[8][64] = 
{
{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},
{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},
{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},
{ 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},
{ 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},
{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},
{ 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},
{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}
};
////////////////////////////////////////////////////////
// TABLE_shift: Left SHifts of C and D blocks
void BitXor(BYTE *Data1, BYTE *Data2, int Len, BYTE *Dest)
{
int i;
for (i = 0; i < Len; i++) Dest[i] = Data1[i] ^ Data2[i];
}
void StrBitXor(char* Data1, char* Data2, char* Dest)
{
int i;
int len1;
int len2;
int nCount;
BYTE byX = 0;
BYTE byY = 0;
BYTE byR = 0;
len1 = strlen(Data1);
len2 = strlen(Data2);
if (len1 % 2 != 0 || len2 % 2 != 0)
{
return;
}
if (len1 >= len2)
{
nCount = len2/2;
}
else
{
nCount = len1/2;
}
for (i=0;i<nCount;i++)
{
HexStrToBytes(&Data1[i*2], &byX, 1);
HexStrToBytes(&Data2[i*2], &byY, 1);
BitXor(&byX, &byY, 1, &byR);
BytesToHexStr(&Dest[i*2], &byR, 1);
}
}
void BitToByte(BYTE *src, BYTE *dst, int n)
{
int i;
BYTEStruct  *BS;
for (i=0; i<n; i++)
{
BS = (BYTEStruct *)(&src[i]);
dst[i*8+7] = BS->b0;
dst[i*8+6] = BS->b1;
dst[i*8+5] = BS->b2;
dst[i*8+4] = BS->b3;
dst[i*8+3] = BS->b4;
dst[i*8+2] = BS->b5;
dst[i*8+1] = BS->b6;
dst[i*8+0] = BS->b7;
}
}
void ByteToBit(BYTE *src, BYTE *dst, int n)
{
int i;
BYTEStruct  *BS;
for (i=0; i<n; i++)
{
BS = (BYTEStruct *)(&dst[i]);
BS->b0 = src[i*8+7];
BS->b1 = src[i*8+6];
BS->b2 = src[i*8+5];
BS->b3 = src[i*8+4];
BS->b4 = src[i*8+3];
BS->b5 = src[i*8+2];
BS->b6 = src[i*8+1];
BS->b7 = src[i*8+0];
}
}
char* Trim(char *In)
{
int i,len;
len = strlen(In);
for (i=len-1; i>=0; i--)
{
if( In[i]!=0x20 && In[i]!=0x00 ) break;
}
for (i++; i<len; i++) In[i] = 0x00;
return In;
}
int HexStrToBytes(char *strSou, BYTE *BytDes, int bytCount)
{
int i;
int len;
BYTE HighByte,LowByte;
len = strlen(Trim(strSou));
if (bytCount * 2 < len) len = bytCount * 2;
if (len % 2 == 0)
{
for (i = 0; i < len; i += 2)
{
HighByte = strSou[i];
LowByte  = strSou[i + 1];
if  (HighByte >= 0x61 && HighByte <= 0x66)
HighByte -= 0x57;
else if (HighByte >= 0x41 && HighByte <= 0x46)
HighByte -= 0x37;
else if (HighByte >= 0x30 && HighByte <= 0x39)
HighByte -= 0x30;
if  (LowByte >= 0x61 && LowByte <= 0x66)
LowByte -= 0x57;
else if (LowByte >= 0x41 && LowByte <= 0x46)
LowByte -= 0x37;
else if (LowByte >= 0x30 && LowByte <= 0x39)
LowByte -= 0x30;
BytDes[i / 2] = (HighByte << 4) | LowByte;
}
for (; i < bytCount * 2; i += 2)
{
BytDes[i / 2] = 0;
}
return (len / 2);
}
else
return 0;
}
int BytesToHexStr(char *strDes, BYTE *bytSou, int bytCount)
{
int i;
int len;
BYTE HighByte, LowByte;
for (i = 0; i < bytCount; i++)
{
HighByte = bytSou[i] >> 4;
LowByte  = bytSou[i] & 0x0F;
HighByte += 0x30;
if (HighByte > 0x39) strDes[i * 2] = HighByte + 0x07;
else strDes[i * 2] = HighByte;
LowByte += 0x30;
if (LowByte > 0x39)  strDes[i * 2 + 1] = LowByte + 0x07;
else strDes[i * 2 + 1] = LowByte;
}
len = strlen(strDes);
return len;
}
void DesAlgo(BYTE *key, BYTE *src, BYTE *dst, BYTE bEncrypt)
{
int  i, j, k;
BYTE input[64]; // store input data
BYTE output[64]; // store output data
BYTE KEY[64]; // store key
BYTE tempA[7];
BYTE tempB[64];
BYTE tempC[8];
BYTE tempD[32];
BYTE K[48];
BYTE fResult[32];
LRStruct LR; // store L[i],R[i] --- temp data after every circle
CDStruct CD; // store C[i],D[i] --- temp Result of Shift()
BitToByte(src, input, 8); // src (64 bits) ==>input (64 Bytes)
SelectExchangeFromTable(IP_TABLE,input,(BYTE*)&LR);  // INPUT ==> IP ==> L0,R0
BitToByte(key, KEY, 8); // key (64 bits) ==>KEY (64 Bytes)
SelectExchangeFromTable(PC1_TABLE,KEY,(BYTE*)&CD);  // KEY ==> PC_1 ==> C0,D0
for (i=0; i<16; i++)
{
// get K(i+1)
Shift(&CD, i, bEncrypt); // C[i],D[i] ==> shifting ==> C[i+1],D[i+1]
ByteToBit((BYTE*)&CD,tempA,7);
SelectExchangeFromTable(PC2_TABLE,(BYTE*)&CD, K);  //  C[i+1],D[i+1] ==> PC_2 ==> K[i+1]
for(k=0; k<8; k++)
{
tempB[k*8+0]=0;
tempB[k*8+1]=0;
for (j=0; j<6; j++) tempB[k*8+(j+2)]=K[k*6+j];
}
ByteToBit(tempA,tempC,8);
// fResult[i] <== f( R[i], K[i+1] )
fFunction(LR.R, K, fResult);
// fResult[i] <== L[i] .XOR. f[i]
BitXor (LR.L, fResult, 32, fResult);
// L(i+1) <== R[i]
memcpy(LR.L, LR.R, sizeof(BYTE)*32);
// R(i+1) <== fResult[i]
memcpy(LR.R, fResult, sizeof(BYTE)*32);
}
// LR <== RL
memcpy(tempD, LR.L,sizeof(BYTE)*32);
memcpy(LR.L, LR.R,sizeof(BYTE)*32);
memcpy(LR.R, tempD,sizeof(BYTE)*32);
SelectExchangeFromTable(IP_1_TABLE, (BYTE*)&LR, output);  // L16R16 ==> IP_1 ==> output
ByteToBit(output, dst, 8);// dst (64 bits) <==  output (64 Bytes)
}
 
void SelectExchangeFromTable(int nTable, BYTE *src, BYTE *dst)
{
static int * table[6] = {TABLE_ip, TABLE_ip_1, TABLE_ePro, TABLE_p, TABLE_pc_1, TABLE_pc_2Pro};
int i, nOut;
int *pTable;
nOut = g_nOut[nTable];
pTable = table[nTable];
for (i=0; i< nOut; i++) dst[i] = src[pTable[i]];
}
void Shift(CDStruct *pCD, int iCircle, BYTE bEncrypt)
{
if ( 0 == bEncrypt)
{
if ( iCircle != 0)
{
RShift(pCD->C, TABLE_shift[iCircle]);
RShift(pCD->D, TABLE_shift[iCircle]);
}
}
else
{
LShift(pCD->C, TABLE_shift[iCircle]);
LShift(pCD->D, TABLE_shift[iCircle]);
}
}
未完待续。。

解决方案 »

  1.   

    // 28 bytes Right shift nShift time(s)
    void RShift(BYTE *buf28, int nShift)
    {
    int i;
    BYTE temp[2];
    for (i=0; i<nShift; i++) temp[(nShift-1)-i] = buf28[27-i];
    for (i=27; i>=nShift; i--) buf28[i] = buf28[i-nShift];
    for (i=0; i<nShift; i++) buf28[i] = temp[i];
    }
    // 28 bytes Left shift nShift time(s)
    void LShift(BYTE *buf28, int nShift)
    {
    int i;
    BYTE temp[2];
    for (i=0; i<nShift; i++) temp[i] = buf28[i];
    for (i=nShift; i<28; i++) buf28[i-nShift] = buf28[i];
    for (i=0; i<nShift; i++) buf28[27-(nShift-1)+i] = temp[i];
    }
    // result <== f( R, K )
    void fFunction(BYTE *R, BYTE *K, BYTE *result)
    {
    BYTE temp[48]; // store temp data 
    BYTE SResult[32]; // store result of S_Change
    // R ==> E_TABLE ==> temp
    SelectExchangeFromTable(E_TABLE, R, temp);
    // temp .XOR. K ==> temp
    BitXor(K,temp,48,temp);
    // temp ==> S_Change ==> SResult
    S_Change(temp, SResult);
    // SResult ==> P_TABLE ==> result
    SelectExchangeFromTable(P_TABLE, SResult, result);
    }
    // src ==> S_Change ==> src
    void S_Change(BYTE *src, BYTE *result)
    {
    BYTE  sTemp;
    BYTE  nPos;
    int  i;
    BYTEStruct *rBSPos;
    for (i=0; i<8; i++)
    {
    rBSPos = (BYTEStruct *)(&nPos) ;
    nPos = 0;
    rBSPos->b0 = src[i*6+5];
    rBSPos->b1 = src[i*6+4];
    rBSPos->b2 = src[i*6+3];
    rBSPos->b3 = src[i*6+2];
    rBSPos->b4 = src[i*6+1];
    rBSPos->b5 = src[i*6+0];
    if (i%2==0) sTemp= TABLE_s[i][nPos]*16; // S Table Result ==> High 4 bits of sTemp
    else
    { // S Table Result ==> Low 4 bits of sTemp
    sTemp += TABLE_s[i][nPos];
    // sTemp Expand to 8 bytes
    BitToByte(&sTemp, &result[i/2*8], 1);
    }
    }
    }
    void EnDES(BYTE *pbyKey, BYTE *pbySrc, BYTE *pbyDst)
    {
    DesAlgo(pbyKey, pbySrc, pbyDst, 1);
    }
    void DeDES(BYTE *pbyKey, BYTE *pbySrc, BYTE *pbyDst)
    {
    DesAlgo(pbyKey, pbySrc, pbyDst, 0);
    }
    void En3DES(BYTE *pbyKey, BYTE *pbySrc, BYTE *pbyDst)
    {
    BYTE LKey[8];
    BYTE RKey[8];
    BYTE TmpDest[8];
    memcpy(LKey,pbyKey,8);
    memcpy(RKey,pbyKey+8,8);
    EnDES(LKey,pbySrc,pbyDst);
    DeDES(RKey,pbyDst,TmpDest);
    EnDES(LKey,TmpDest,pbyDst);
    }
    void De3DES(BYTE *pbyKey, BYTE *pbySrc, BYTE *pbyDst)
    {
    BYTE LKey[8];
    BYTE RKey[8];
    BYTE TmpDest[8];
    memcpy(LKey,pbyKey,8);
    memcpy(RKey,pbyKey+8,8);
    DeDES(LKey,pbySrc,pbyDst);
    EnDES(RKey,pbyDst,TmpDest);
    DeDES(LKey,TmpDest,pbyDst);
    }
    void DefuseKey(BYTE *key, BYTE *factor, BYTE *DefusedKey)
    {
    BYTE xornum[8]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
    BYTE XorFactor[8];
    En3DES(key,factor,DefusedKey);
    BitXor(factor,xornum,8,XorFactor);
    En3DES(key,XorFactor,DefusedKey+8);
    }
    void MAC(BYTE *pbyKey, BYTE *pbySrc, int iSrcLen, BYTE *pbyInit, BYTE *pbyDst)

    BYTE pbySrcTemp[8];
    BYTE pbyInitData[8];
    BYTE pbyDeaSrc[8];
    int i, j, n, iAppend;
    n = iSrcLen / 8 + 1;
    iAppend = 8 - ( n * 8 - iSrcLen);
    memcpy (pbyInitData,pbyInit,8);
    for(i = 0; i < n; i++)
    {
    memset(pbySrcTemp, 0x00, 8);
    if(i==(n-1))
    {
    memcpy(pbySrcTemp, &pbySrc[i*8], iAppend); 
    pbySrcTemp[iAppend] = 0x80;
    for (j = iAppend + 1; j < 8; j++) pbySrcTemp[j] = 0x00;
    }
    else memcpy(pbySrcTemp, &pbySrc[i*8], 8); 
    BitXor(pbySrcTemp, pbyInitData, 8, pbyDeaSrc);
    memset(pbyInitData, 0x00, 8);
    EnDES(pbyKey, pbyDeaSrc, pbyInitData);
    }
    memcpy(pbyDst, pbyInitData, 4);
    }
    void MAC16(BYTE *pbyKey, BYTE *pbySrc, int iSrcLen, BYTE *pbyInit, BYTE *pbyDst)

    BYTE pbySrcTemp[8];
    BYTE pbyInitData[8];
    BYTE pbyDeaSrc[8];
    BYTE KeyA[8];
    BYTE KeyB[8];
    int i, j, n, iAppend;
    n = iSrcLen / 8 + 1;
    iAppend = 8 - ( n * 8 - iSrcLen);
    memcpy (pbyInitData,pbyInit,8);
    memcpy (KeyA,pbyKey,8);
    memcpy (KeyB,&pbyKey[8],8);
    for(i = 0; i < n; i++)
    {
    memset(pbySrcTemp, 0x00, 8);
    if(i==(n-1))
    {
    memcpy(pbySrcTemp, &pbySrc[i*8], iAppend); 
    pbySrcTemp[iAppend] = 0x80;
    for (j = iAppend + 1; j < 8; j++) pbySrcTemp[j] = 0x00;
    }
    else memcpy(pbySrcTemp, &pbySrc[i*8], 8); 
    BitXor(pbySrcTemp, pbyInitData, 8, pbyDeaSrc);
    memset(pbyInitData, 0x00, 8);
    EnDES(KeyA, pbyDeaSrc, pbyInitData);
    }
    DeDES(KeyB, pbyInitData, pbyDeaSrc);
    EnDES(KeyA, pbyDeaSrc, pbyInitData);
    memcpy(pbyDst, pbyInitData, 4);
    }
    void StrEnDES(char *Key, char *Src, char *Dst)
    {
    BYTE bytKey[8];
    BYTE bytSrc[8];
    BYTE bytDst[8];
    HexStrToBytes(Key,bytKey,8);
    HexStrToBytes(Src,bytSrc,8);
    DesAlgo(bytKey, bytSrc, bytDst, 1);
    BytesToHexStr(Dst,bytDst,8);
    }void StrDeDES(char *Key, char *Src, char *Dst)
    {
    BYTE bytKey[8];
    BYTE bytSrc[8];
    BYTE bytDst[8]; HexStrToBytes(Key,bytKey,8);
    HexStrToBytes(Src,bytSrc,8);
    DesAlgo(bytKey, bytSrc, bytDst, 0);
    BytesToHexStr(Dst,bytDst,8);
    }void StrEn3DES(char *Key, char *Src, char *Dst)
    {
    BYTE LKey[8];
    BYTE RKey[8];
    BYTE bytSrc[8];
    BYTE bytDst[8];
    HexStrToBytes(Key,     LKey,  8);
    HexStrToBytes(&Key[16],RKey,  8);
    HexStrToBytes(Src,     bytSrc,8);
    EnDES(LKey,bytSrc,bytDst);
    DeDES(RKey,bytDst,bytSrc);
    EnDES(LKey,bytSrc,bytDst);
    BytesToHexStr(Dst,bytDst,8);
    }
    void StrDe3DES(char *Key, char *Src, char *Dst)
    {
    BYTE LKey[8];
    BYTE RKey[8];
    BYTE bytSrc[8];
    BYTE bytDst[8];
    HexStrToBytes(Key,     LKey,  8);
    HexStrToBytes(&Key[16],RKey,  8);
    HexStrToBytes(Src,     bytSrc,8);
    DeDES(LKey,bytSrc,bytDst);
    EnDES(RKey,bytDst,bytSrc);
    DeDES(LKey,bytSrc,bytDst);
    BytesToHexStr(Dst,bytDst,8);
    }
    void StrEn3DES_ECB(char *Key, char *Src, char *Dst)
    {
    BYTE bytKey[16] = {0};
    BYTE bytSrc[8] = {0};
    BYTE bytDst[8] = {0};
    int len;
    int ipending;
    int iGrp;
    len = strlen(Src);
    if (len % 2 != 0)
    {
    return;
    }
    ipending = ((16 - len % 16) / 2) % 8;
    HexStrToBytes(Key, bytKey, 16);
    for (iGrp=0; iGrp<len/16; iGrp++)
    {
    memset(bytSrc, 0, 8);
    HexStrToBytes(&Src[iGrp*16], bytSrc, 8);
    En3DES(bytKey, bytSrc, bytDst);
    BytesToHexStr(&Dst[iGrp*16], bytDst, 8);
    }
    if (ipending > 0)
    {
    memset(&bytSrc, 0, 8);
    HexStrToBytes(&Src[iGrp*16], bytSrc, 8-ipending);
    En3DES(bytKey, bytSrc, bytDst);
    BytesToHexStr(&Dst[iGrp*16], bytDst, 8);
    }
    }
    void StrEn3DES_CBC(char *Key, char *Src, char *Dst)
    {
    BYTE bytKey[16] = {0};
    BYTE bytSrc[8] = {0};
    BYTE bytDst[8] = {0};
    BYTE bytTmp[8] = {0};
    BYTE bytPending = 0;
    int len;
    int iGrp;
    len = strlen(Src);
    if (len % 2 != 0)
    {
    return;
    }
    bytPending = (16 - len % 16) / 2;
    HexStrToBytes(Key, bytKey, 16);
    for (iGrp=0; iGrp<len/16; iGrp++)
    {
    memset(bytSrc, 0, 8);
    HexStrToBytes(&Src[iGrp*16], bytTmp, 8);
    BitXor(bytTmp, bytDst, 8, bytSrc);
    En3DES(bytKey, bytSrc, bytDst);
    BytesToHexStr(&Dst[iGrp*16], bytDst, 8);
    }
    memset(bytTmp, bytPending, 8);
    HexStrToBytes(&Src[iGrp*16], bytTmp, 8-bytPending);
    BitXor(bytTmp, bytDst, 8, bytSrc);
    En3DES(bytKey, bytSrc, bytDst);
    BytesToHexStr(&Dst[iGrp*16], bytDst, 8);
    }
    void StrDe3DES_ECB(char *Key, char *Src, char *Dst)
    {
    BYTE bytKey[16] = {0};
    BYTE bytSrc[8] = {0};
    BYTE bytDst[8] = {0};
    int len;
    int iGrp;
    len = strlen(Src);
    if (len % 16 != 0)
    {
    return;
    }
    HexStrToBytes(Key, bytKey, 16);
    for (iGrp=0; iGrp<len/16; iGrp++)
    {
    HexStrToBytes(&Src[iGrp*16], bytSrc, 8);
    De3DES(bytKey, bytSrc, bytDst);
    BytesToHexStr(&Dst[iGrp*16], bytDst, 8);
    }
    }
    void StrDe3DES_CBC(char *Key, char *Src, char *Dst)
    {
    BYTE bytKey[16] = {0};
    BYTE bytSrc[8] = {0};
    BYTE bytDst[8] = {0};
    BYTE bytTmp[8] = {0};
    BYTE bytTmp1[8] = {0};
    BYTE bytPending = 0;
    int len;
    int iGrp;
    len = strlen(Src);
    if (len % 16 != 0)
    {
    return;
    }
    iGrp = len / 16;
    HexStrToBytes(Key, bytKey, 16);
    for (iGrp=0; iGrp<len/16-1; iGrp++)
    {
    HexStrToBytes(&Src[iGrp*16], bytSrc, 8);
    De3DES(bytKey, bytSrc, bytTmp);
    BitXor(bytTmp, bytTmp1, 8, bytDst);
    HexStrToBytes(&Src[iGrp*16], bytTmp1, 8);
    BytesToHexStr(&Dst[iGrp*16], bytDst, 8);
    }
    HexStrToBytes(&Src[iGrp*16], bytSrc, 8);
    De3DES(bytKey, bytSrc, bytTmp);
    BitXor(bytTmp, bytTmp1, 8, bytDst);
    BytesToHexStr(&Dst[iGrp*16], bytDst, 8-bytDst[7]);
    }
    void StrMAC(char *Key, char *Src, char* InitData, char *Dst)
    {
    BYTE bytKey[8] = {0};
    BYTE bytSrc[8192] = {0};
    BYTE bytDst[4] = {0};
    BYTE bytInit[8] = {0};
    int len;
    len = strlen(Src);
    if (len % 2 != 0)
    {
    return;
    }
    len = len / 2;
    HexStrToBytes(Key,     bytKey,  8);
    HexStrToBytes(InitData,bytInit, 8);
    HexStrToBytes(Src,     bytSrc,  len);
    MAC(bytKey, bytSrc, len, bytInit, bytDst);
    BytesToHexStr(Dst,bytDst,4);
    }
    void StrMAC16(char *Key, char *Src, char* InitData, char *Dst)
    {
    BYTE bytKey[16] = {0};
    BYTE bytSrc[8192] = {0};
    BYTE bytDst[4] = {0};
    BYTE bytInit[8] = {0};
    int len;
    len = strlen(Src);
    if (len % 2 != 0)
    {
    return;
    }
    len = len / 2;
    HexStrToBytes(Key,     bytKey,  16);
    HexStrToBytes(InitData,bytInit, 8);
    HexStrToBytes(Src,     bytSrc,  len);
    MAC16(bytKey, bytSrc, len, bytInit, bytDst);
    BytesToHexStr(Dst,bytDst,4);
    }
    void StrDefuseKey(char *Key, char *factor, char *DefusedKey)
    {
    BYTE bytKey[16];
    BYTE bytFactor[8];
    BYTE bytDefusedKey[16];
    HexStrToBytes(Key,bytKey,16);
    HexStrToBytes(factor,bytFactor,8);
    DefuseKey(bytKey, bytFactor, bytDefusedKey);
    BytesToHexStr(DefusedKey,bytDefusedKey,16);
    }
    int main()
    {
    char keystr[33],srcstr[33],initstr[17],deststr[17];
    memset(keystr,0,sizeof(keystr));
    memset(srcstr,0,sizeof(srcstr));
    memset(initstr,0,sizeof(initstr));
    memset(deststr,0,sizeof(deststr));
    /*设置MAC Key, 双倍长密钥 必须16字节(扩展后32字符)*/
    memcpy(keystr,"11223344556677889900aabbccddeeff",32);
    /*设置待加密的源串,程序会自动补齐*/
    memcpy(srcstr,"11223344556677889900aa",22);
    /*初始因子必须为8字节(扩展后16字符)*/
    memcpy(initstr,"0000000000000000",16);
    /*调用计算MAC函数,返回4字节(8字符)的MAC值*/
    StrMAC16(keystr,srcstr,initstr,deststr);
    printf("desstr==%s\n",deststr);
    return 0;
    }