Code: Show/Hide //Conversion routine
void CSSConnection::WriteUint32to8(Uint32 data,Uint8 *target,int len){ for(Uint32 cnt=0;cnt<sizeof(Uint32) && cnt<len;cnt++) target[cnt]=(Uint8)((data >> (cnt * 8)) & 0xFF); } //Encryption routines - by Snrrrup void CSSConnection::InitEncryption(Uint32 key){ Log("InitEncryption"); Sint32 temp = 0; encCypherKey = key; for(int cnt = 0; cnt < 520; cnt += 2) // Each "block" is 2 bytes and the keystream size is 520 bytes { temp = (Uint32)((Uint64)((Uint64)key * (Uint64)0x834E0B5F) >> 48); temp += (temp >> 31); key = ((key % 0x1F31D) * 16807) - (temp * 2836) + 123; if((Sint32)key < 0) key += 0x7FFFFFFF; *((Uint16 *)(encKeyStream + cnt)) = (Uint16)key; } encryptionEnabled=true; } void CSSConnection::Encrypt(char *data,int len){ Log("Encrypt"); if(!encryptionEnabled) return; int StartPos = 1; Uint32 Encrypted = encCypherKey; if(!data[0]) //If Byte at Offset 0 is 0x00 StartPos++; for(Uint32 Count = StartPos; Count < len; Count += 4) { Encrypted ^= *(Uint32 *)(encKeyStream + (Count - StartPos)) ^ (Uint32)(data+Count); WriteUint32to8(Encrypted,(Uint8*)(data+Count),len-Count); } } void CSSConnection::Decrypt(char *data,int len){ Log("Decrypt"); if(!encryptionEnabled) return; Uint32 Decrypted = encCypherKey; int StartPos = 1; if(!data[0]) //If Byte at Offset 0 is 0x00 StartPos++; for(Uint32 Count = StartPos; Count < len; Count += 4) { Uint32 Encrypted = (Uint32)(data+Count); Decrypted ^= *(Uint32 *)(encKeyStream + (Count - StartPos)) ^ Encrypted; WriteUint32to8(Decrypted,(Uint8*)(data+Count),len-Count); Decrypted = Encrypted; } } |
myke wrote: |
you haven't described your problem so people really can't help you too much...is it a problem compiling? a problem with the encryption not encrypting or decrypting? |
Code: Show/Hide //Encryption
Uint32 encClientKey,encCypherKey; Uint8 encKeyStream[520]; bool encryptionEnabled; //Encryption routines - by Snrrrup void InitEncryption(Uint32 key);//FIXME void Encrypt(char *data,int len);//FIXME void Decrypt(char *data,int len);//FIXME |