⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cast.c

📁 BestCrypt开源加密原代码
💻 C
📖 第 1 页 / 共 2 页
字号:
        DWORD_TO_4BYTES(tmp32bStream[2], tmpBytePtr);        PRE_SUBKEY( tmp32bStream[3],key32bStream[1],5,tb[0xA],tb[0x9],tb[0xB],tb[0x8],kb[0xB]);        DWORD_TO_4BYTES(tmp32bStream[3], tmpBytePtr);        FIND_SUBKEYS3(subKeyNum, tb);        subKeyNum += 4;        /* Define 12-15 subkeys (28-31 on the second round) */        keyBytePtr = keyByteStream;        tmpBytePtr = tmpByteStream;        PRE_SUBKEY( key32bStream[0],tmp32bStream[2],6,tb[0x5],tb[0x7],tb[0x4],tb[0x6],tb[0x0]);        DWORD_TO_4BYTES(key32bStream[0], keyBytePtr);        PRE_SUBKEY( key32bStream[1],tmp32bStream[0],7,kb[0x0],kb[0x2],kb[0x1],kb[0x3],tb[0x2]);        DWORD_TO_4BYTES(key32bStream[1], keyBytePtr);        PRE_SUBKEY( key32bStream[2],tmp32bStream[1],4,kb[0x7],kb[0x6],kb[0x5],kb[0x4],tb[0x1]);        DWORD_TO_4BYTES(key32bStream[2], keyBytePtr);        PRE_SUBKEY( key32bStream[3],tmp32bStream[3],5,kb[0xA],kb[0x9],kb[0xB],kb[0x8],tb[0x3]);        DWORD_TO_4BYTES(key32bStream[3], keyBytePtr);        FIND_SUBKEYS4(subKeyNum, kb);        subKeyNum += 4;    }     /* 32-bit subkeys beginning from the 16-th subkey are used in CAST for 
       rotational operation. This operation performed on 32-bit numbers, 
       hence, the maximum bits we need is 5 bits. Let's mask all other bits
       in the 16 - 31 subkeys
     */    for ( i=16; i<CAST_MAX_NUMBER_OF_ROUNDS*2; i++ )        subKeys[i] &= 0x1f;    return TRUE;}/****************************************************************
 *
 *	CastECB() does encrypting (encrypt==TRUE) or
 *	decrypting (encrypt==FALSE) for a block of data the size of
 *	cipher block (BLOCK_SIZE, given in bytes).
 *
 ****************************************************************/BOOL CastECB( BYTE  *InBlock,          BYTE  *OutBlock,         DWORD *ExtKey,         BOOL  Encrypt){    DWORD  left, righ;    BYTE     *dataPtr;    CastData *castData;    DWORD  *subKeys, temp;    dataPtr  = InBlock;    castData = (CastData *)ExtKey;    subKeys  = castData->subKeys;    BYTES_TO_DWORD(dataPtr, left);    BYTES_TO_DWORD(dataPtr, righ);    if ( Encrypt )    {        ROUND_TYPE1(left, righ, subKeys, 0,  temp );   /* 1  round */        ROUND_TYPE2(righ, left, subKeys, 1,  temp );   /* 2  round */        ROUND_TYPE3(left, righ, subKeys, 2,  temp );   /* 3  round */        ROUND_TYPE1(righ, left, subKeys, 3,  temp );   /* 4  round */        ROUND_TYPE2(left, righ, subKeys, 4,  temp );   /* 5  round */        ROUND_TYPE3(righ, left, subKeys, 5,  temp );   /* 6  round */        ROUND_TYPE1(left, righ, subKeys, 6,  temp );   /* 7  round */        ROUND_TYPE2(righ, left, subKeys, 7,  temp );   /* 8  round */        ROUND_TYPE3(left, righ, subKeys, 8,  temp );   /* 9  round */        ROUND_TYPE1(righ, left, subKeys, 9,  temp );   /* 10 round */        ROUND_TYPE2(left, righ, subKeys, 10, temp );   /* 11 round */        ROUND_TYPE3(righ, left, subKeys, 11, temp );   /* 12 round */        if ( castData->make16rounds )        {            ROUND_TYPE1(left, righ, subKeys, 12, temp ); /* 13 round */            ROUND_TYPE2(righ, left, subKeys, 13, temp ); /* 14 round */            ROUND_TYPE3(left, righ, subKeys, 14, temp ); /* 15 round */            ROUND_TYPE1(righ, left, subKeys, 15, temp ); /* 16 round */        }        temp = left;         left = righ;        righ = temp;    }    else    {        temp = left;         left = righ;        righ = temp;        if ( castData->make16rounds )        {            ROUND_TYPE1(righ, left, subKeys, 15, temp ); /* 16 round */            ROUND_TYPE3(left, righ, subKeys, 14, temp ); /* 15 round */            ROUND_TYPE2(righ, left, subKeys, 13, temp ); /* 14 round */            ROUND_TYPE1(left, righ, subKeys, 12, temp ); /* 13 round */        }        ROUND_TYPE3(righ, left, subKeys, 11, temp );   /* 12 round */        ROUND_TYPE2(left, righ, subKeys, 10, temp );   /* 11 round */        ROUND_TYPE1(righ, left, subKeys, 9,  temp );   /* 10 round */        ROUND_TYPE3(left, righ, subKeys, 8,  temp );   /* 9  round */        ROUND_TYPE2(righ, left, subKeys, 7,  temp );   /* 8  round */        ROUND_TYPE1(left, righ, subKeys, 6,  temp );   /* 7  round */        ROUND_TYPE3(righ, left, subKeys, 5,  temp );   /* 6  round */        ROUND_TYPE2(left, righ, subKeys, 4,  temp );   /* 5  round */        ROUND_TYPE1(righ, left, subKeys, 3,  temp );   /* 4  round */        ROUND_TYPE3(left, righ, subKeys, 2,  temp );   /* 3  round */        ROUND_TYPE2(righ, left, subKeys, 1,  temp );   /* 2  round */        ROUND_TYPE1(left, righ, subKeys, 0,  temp );   /* 1  round */    }    dataPtr  = OutBlock;    DWORD_TO_4BYTES(left, dataPtr);    DWORD_TO_4BYTES(righ, dataPtr);    return TRUE;}//************ CAST CBC mode encryption **************VOIDEncrypt( DWORD *IVector,          DWORD *KeyAddress,         DWORD *SrcBuffer,         DWORD *DstBuffer,         DWORD Length )    // in bytes{    DWORD i;    DWORD left, right;    UCHAR buf[8], *bytePtr;    left  = IVector[0];    right = IVector[1];    for ( i = 0; i < (Length >> 2); i = i+2 )    {        // do EBC encryption of (Initial_Vector XOR Data)        left  = SrcBuffer[i] ^ left;        right = SrcBuffer[i+1] ^ right;        bytePtr = buf;       DWORD_TO_4BYTES(left, bytePtr);        bytePtr = &(buf[4]); DWORD_TO_4BYTES(right,bytePtr);        CastECB( buf, buf, KeyAddress, TRUE );        bytePtr = buf;       BYTES_TO_DWORD(bytePtr,left);        bytePtr = &(buf[4]); BYTES_TO_DWORD(bytePtr,right);        DstBuffer[i]   = left;        DstBuffer[i+1] = right;         }}//************ CAST CBC mode decryption **************VOIDDecrypt( DWORD *IVector,          DWORD *KeyAddress,         DWORD *SrcBuffer,         DWORD *DstBuffer,         DWORD Length )    // in bytes{    DWORD i;    DWORD left, right, ivectorL, ivectorR, oldSrcL, oldSrcR;    UCHAR buf[8], *bytePtr;    ivectorL = IVector[0];    ivectorR = IVector[1];    for ( i = 0; i < (Length >> 2); i = i+2 )    {        left  = oldSrcL = SrcBuffer[i];        right = oldSrcR = SrcBuffer[i+1];        // Encrypted Data -> new IV,         // then do EBC decryption of Encrypted Data,         // then XOR decrypted data with old IV        bytePtr = buf;       DWORD_TO_4BYTES(left, bytePtr);        bytePtr = &(buf[4]); DWORD_TO_4BYTES(right,bytePtr);        CastECB( buf, buf, KeyAddress, FALSE );        bytePtr = buf;       BYTES_TO_DWORD(bytePtr,left);        bytePtr = &(buf[4]); BYTES_TO_DWORD(bytePtr,right);        DstBuffer[i]   = left  ^ ivectorL;        DstBuffer[i+1] = right ^ ivectorR;              ivectorL = oldSrcL;        ivectorR = oldSrcR;    }}char cast_c[]="$Id: cast.c,v 1.4 2003/05/22 05:39:07 crypt Rel-1.6-5 $";

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -