SA-MP/raknet/SocketDataEncryptor.cpp

131 lines
4.3 KiB
C++

#include <string.h>
#include "SocketDataEncryptor.h"
unsigned short SocketDataEncryptor::session_key = 0;
static const unsigned char S[256] = {
#ifndef SAMPSRV
0x27,0x69,0xFD,0x87,0x60,0x7D,0x83,0x02,0xF2,0x3F,0x71,0x99,0xA3,0x7C,0x1B,0x9D,
0x76,0x30,0x23,0x25,0xC5,0x82,0x9B,0xEB,0x1E,0xFA,0x46,0x4F,0x98,0xC9,0x37,0x88,
0x18,0xA2,0x68,0xD6,0xD7,0x22,0xD1,0x74,0x7A,0x79,0x2E,0xD2,0x6D,0x48,0x0F,0xB1,
0x62,0x97,0xBC,0x8B,0x59,0x7F,0x29,0xB6,0xB9,0x61,0xBE,0xC8,0xC1,0xC6,0x40,0xEF,
0x11,0x6A,0xA5,0xC7,0x3A,0xF4,0x4C,0x13,0x6C,0x2B,0x1C,0x54,0x56,0x55,0x53,0xA8,
0xDC,0x9C,0x9A,0x16,0xDD,0xB0,0xF5,0x2D,0xFF,0xDE,0x8A,0x90,0xFC,0x95,0xEC,0x31,
0x85,0xC2,0x01,0x06,0xDB,0x28,0xD8,0xEA,0xA0,0xDA,0x10,0x0E,0xF0,0x2A,0x6B,0x21,
0xF1,0x86,0xFB,0x65,0xE1,0x6F,0xF6,0x26,0x33,0x39,0xAE,0xBF,0xD4,0xE4,0xE9,0x44,
0x75,0x3D,0x63,0xBD,0xC0,0x7B,0x9E,0xA6,0x5C,0x1F,0xB2,0xA4,0xC4,0x8D,0xB3,0xFE,
0x8F,0x19,0x8C,0x4D,0x5E,0x34,0xCC,0xF9,0xB5,0xF3,0xF8,0xA1,0x50,0x04,0x93,0x73,
0xE0,0xBA,0xCB,0x45,0x35,0x1A,0x49,0x47,0x6E,0x2F,0x51,0x12,0xE2,0x4A,0x72,0x05,
0x66,0x70,0xB8,0xCD,0x00,0xE5,0xBB,0x24,0x58,0xEE,0xB4,0x80,0x81,0x36,0xA9,0x67,
0x5A,0x4B,0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x0F,0x86,0xC5,
0x68,0x9B,0x21,0x0C,0x8B,0x42,0x87,0xFF,0x4F,0xBE,0xC8,0xE8,0xC7,0xD4,0x7A,0xE0,
0x55,0x2F,0x8A,0x8E,0xBA,0x98,0x37,0xE4,0xB2,0x38,0xA1,0xB6,0x32,0x83,0x3A,0x7B,
0x84,0x3C,0x61,0xFB,0x8C,0x14,0x3D,0x43,0x3B,0x1D,0xC3,0xA2,0x96,0xB3,0xF8,0xC4,
0xF2,0x26,0x2B,0xD8,0x7C,0xFC,0x23,0x24,0x66,0xEF,0x69,0x64,0x50,0x54,0x59,0xF1,
0xA0,0x74,0xAC,0xC6,0x7D,0xB5,0xE6,0xE2,0xC2,0x7E,0x67,0x17,0x5E,0xE1,0xB9,0x3F,
0x6C,0x70,0x08,0x99,0x45,0x56,0x76,0xF9,0x9A,0x97,0x19,0x72,0x5C,0x02,0x8F,0x58,
#endif
};
//----------------------------------------------------
unsigned char SocketDataEncryptor::GetChecksum(unsigned char *data, int length)
{
unsigned char sum = 0;
for(int i = 0; i != length; i++)
{
sum ^= data[i] & 0xAA;
}
return sum;
}
//----------------------------------------------------
void SocketDataEncryptor::Process(unsigned char key1,
unsigned char key2,
unsigned char *data,
int length)
{
int counter = 0;
for(int i = 0; i != length; i++)
{
#ifndef SAMPSRV
data[i] = S[data[i]];
#endif
if(counter == 0)
{
data[i] ^= key1;
counter = 1;
}
else
{
data[i] ^= key2;
counter--;
}
#ifdef SAMPSRV
data[i] = S[data[i]];
#endif
}
}
//----------------------------------------------------
#ifndef SAMPSRV
void SocketDataEncryptor::Encrypt(unsigned char *destination,
unsigned char *source,
int *length)
{
unsigned char key = session_key;
*destination = GetChecksum(source, *length);
memcpy(destination + 1, source, *length);
Process(0, key, destination + 1, *length);
++*length;
}
#else
int SocketDataEncryptor::Decrypt(unsigned char *destination,
unsigned char *source,
int *length)
{
unsigned char key = session_key;
unsigned char sum = source[0];
*length--;
memcpy(destination, source + 1, *length);
Process(0, key, destination, *length);
return GetChecksum(destination, *length) == sum;
}
#endif
//----------------------------------------------------
void SocketDataEncryptor::SetKey(int key)
{
session_key = key ^ 0xCCCC;
}
//----------------------------------------------------
// EOF