www.pudn.com > wyd753.rar > encdec.cpp, change:2009-01-19,size:6231b


#include "encdec.h" 
 
// Keys para a encryptacao e decryptacao dos pacotes 
const unsigned char EncDec_Keys[512] = { 
	0x84, 0x87, 0x37, 0xD7, 0xEA, 0x79, 0x91, 0x7D, 0x4B, 0x4B, 0x85, 0x7D, 0x87, 0x81, 0x91, 0x7C, 
	0x0F, 0x73, 0x91, 0x91, 0x87, 0x7D, 0x0D, 0x7D, 0x86, 0x8F, 0x73, 0x0F, 0xE1, 0xDD, 0x85, 0x7D, 
	0x05, 0x7D, 0x85, 0x83, 0x87, 0x9C, 0x85, 0x33, 0x0D, 0xE2, 0x87, 0x19, 0x0F, 0x79, 0x85, 0x86, 
	0x37, 0x7D, 0xD7, 0xDD, 0xE9, 0x7D, 0xD7, 0x7D, 0x85, 0x79, 0x05, 0x7D, 0x0F, 0xE1, 0x87, 0x7E, 
	0x23, 0x87, 0xF5, 0x79, 0x5F, 0xE3, 0x4B, 0x83, 0xA3, 0xA2, 0xAE, 0x0E, 0x14, 0x7D, 0xDE, 0x7E, 
	0x85, 0x7A, 0x85, 0xAF, 0xCD, 0x7D, 0x87, 0xA5, 0x87, 0x7D, 0xE1, 0x7D, 0x88, 0x7D, 0x15, 0x91, 
	0x23, 0x7D, 0x87, 0x7C, 0x0D, 0x7A, 0x85, 0x87, 0x17, 0x7C, 0x85, 0x7D, 0xAC, 0x80, 0xBB, 0x79, 
	0x84, 0x9B, 0x5B, 0xA5, 0xD7, 0x8F, 0x05, 0x0F, 0x85, 0x7E, 0x85, 0x80, 0x85, 0x98, 0xF5, 0x9D, 
	0xA3, 0x1A, 0x0D, 0x19, 0x87, 0x7C, 0x85, 0x7D, 0x84, 0x7D, 0x85, 0x7E, 0xE7, 0x97, 0x0D, 0x0F, 
	0x85, 0x7B, 0xEA, 0x7D, 0xAD, 0x80, 0xAD, 0x7D, 0xB7, 0xAF, 0x0D, 0x7D, 0xE9, 0x3D, 0x85, 0x7D, 
	0x87, 0xB7, 0x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, 0xBF, 0x4B, 0x19, 0x85, 0xA5, 0x91, 0x80, 0x87, 0x81, 0x87, 0x7C, 
	0x0F, 0x73, 0x91, 0x91, 0x84, 0x87, 0x37, 0xD7, 0x86, 0x79, 0xE1, 0xDD, 0x85, 0x7A, 0x73, 0x9B, 
	0x05, 0x7D, 0x0D, 0x83, 0x87, 0x9C, 0x85, 0x33, 0x87, 0x7D, 0x85, 0x0F, 0x87, 0x7D, 0x0D, 0x7D, 
	0xF6, 0x7E, 0x87, 0x7D, 0x88, 0x19, 0x89, 0xF5, 0xD1, 0xDD, 0x85, 0x7D, 0x8B, 0xC3, 0xEA, 0x7A, 
	0xD7, 0xB0, 0x0D, 0x7D, 0x87, 0xA5, 0x87, 0x7C, 0x73, 0x7E, 0x7D, 0x86, 0x87, 0x23, 0x85, 0x10, 
	0xD7, 0xDF, 0xED, 0xA5, 0xE1, 0x7A, 0x85, 0x23, 0xEA, 0x7E, 0x85, 0x98, 0xAD, 0x79, 0x86, 0x7D, 
	0x85, 0x7D, 0xD7, 0x7D, 0xE1, 0x7A, 0xF5, 0x7D, 0x85, 0xB0, 0x2B, 0x37, 0xE1, 0x7A, 0x87, 0x79, 
	0x84, 0x7D, 0x73, 0x73, 0x87, 0x7D, 0x23, 0x7D, 0xE9, 0x7D, 0x85, 0x7E, 0x02, 0x7D, 0xDD, 0x2D, 
	0x87, 0x79, 0xE7, 0x79, 0xAD, 0x7C, 0x23, 0xDA, 0x87, 0x0D, 0x0D, 0x7B, 0xE7, 0x79, 0x9B, 0x7D, 
	0xD7, 0x8F, 0x05, 0x7D, 0x0D, 0x34, 0x8F, 0x7D, 0xAD, 0x87, 0xE9, 0x7C, 0x85, 0x80, 0x85, 0x79, 
	0x8A, 0xC3, 0xE7, 0xA5, 0xE8, 0x6B, 0x0D, 0x74, 0x10, 0x73, 0x33, 0x17, 0x0D, 0x37, 0x21, 0x19 
}; 
 
 
// Decrypta o pacote 
// 
// Retorna false se o pacote  invalido 
// Retorna true se o pacote  valido 
bool Decrypt(u8 *Packet, s32 pSize) 
{ 
    u8 iKeyWord, KeyWord, key; 
 
    if(*(u32*)Packet == 0x1F11F311) 
    { /* hello packet; not encrypted */ 
        return false; 
    } 
 
    if(pSize < 12 || pSize > 8192) 
    { /* invalid packet */ 
        return false; 
    } 
 
    /* get Key */ 
    iKeyWord = Packet[2]; 
    KeyWord = EncDec_Keys[iKeyWord * 2]; 
 
    u32 pos = KeyWord; 
    u32 Sum1 = 0, Sum2 = 0; 
    for(s32 i = 4; i < pSize; i++, pos++) 
    { 
        register u32 rEAX; 
 
        Sum2 &= 0xFF; 
        Sum2 += Packet[i]; 
 
        key = EncDec_Keys[((pos & 0xFF) * 2) + 1]; 
 
        rEAX = (i & 0x03); 
        if(rEAX == 0) 
        { 
            key <<= 0x01; 
            Packet[i] -= key; 
        } 
        else if(rEAX == 1) 
        { 
            key >>= 0x03; 
            Packet[i] += key; 
        } 
        else if(rEAX == 2) 
        { 
            key <<= 0x02; 
            Packet[i] -= key; 
        } 
        else /* rEAX = 3 */ 
        { 
            key >>= 0x05; 
            Packet[i] += key; 
        } 
 
        Sum1 &= 0xFF; 
        Sum1 += Packet[i]; 
    } 
 
    Sum2 &= 0xFF; 
    Sum1 &= 0xFF; 
 
    /* check Time Stamp */ 
    if(*(u32*)&Packet[8] == 0) 
        return false; 
 
    /* check Sum */ 
    return (((Sum2 - Sum1) & 0xFF) == Packet[3]); 
} 
 
// Encrypta o pacote 
// 
// Retorna false se o pacote  invalido 
// Retorna true se o pacote  valido 
bool Encrypt(u8 *Packet, s32 pSize) 
{ 
    u8 iKeyWord, KeyWord, key; 
 
    if(*(u32*)Packet == 0x1F11F311) 
    { /* hello packet; not encrypted */ 
        return true; 
    } 
 
    if(pSize < 12 || pSize > 4096) 
    { /* invalid packet */ 
        return false; 
    } 
 
    /* set rand() to get random number */ 
    srand(time(NULL)); 
 
    /* get Key */ 
    iKeyWord = (rand() & 0xFF); 
    KeyWord = EncDec_Keys[iKeyWord * 2]; 
 
    /* write Key */ 
    Packet[2] = iKeyWord; 
 
    /* write Time Stamp */ 
    *(u32*)&Packet[8] = clock(); 
 
    u32 pos = KeyWord; 
    u32 Sum1 = 0, Sum2 = 0; 
    for(s32 i = 4; i < pSize; i++, pos++) 
    { 
        register u32 rEAX; 
 
        Sum1 &= 0xFF; 
        Sum1 += Packet[i]; 
 
        key = EncDec_Keys[((pos & 0xFF) * 2) + 1]; 
 
        rEAX = (i & 0x03); 
        if(rEAX == 0) 
        { 
            key *= 2; 
            Packet[i] += key; 
        } 
        else if(rEAX == 1) 
        { 
            key >>= 3; 
            Packet[i] -= key; 
        } 
        else if(rEAX == 2) 
        { 
            key *= 4; 
            Packet[i] += key; 
        } 
        else /* rEAX = 3 */ 
        { 
            key >>= 5; 
            Packet[i] -= key; 
        } 
 
        Sum2 &= 0xFF; 
        Sum2 += Packet[i]; 
    } 
 
    Sum2 &= 0xFF; 
    Sum1 &= 0xFF; 
 
    /* write Sum */ 
    Packet[3] = ((Sum2 - Sum1) & 0xFF); 
 
    return true; 
}