PDA

View Full Version : Gameserver packet encryption


aralyon
14.03.2006, 17:55
Can anyone explain me, or rather post code to encryption/decryption gameserver packets?
I looked at l2j, but it works fine only when I'm connecting to emulator. When I connect to normal server, some bytes are ok, some unfortunately not.

testdude
14.03.2006, 18:01
this is from l2packet framework source...
C++
Decoder.cpp
#include "Decoder.h"
#include <stdio.h>
#include <malloc.h>
#include <string.h>

Decoder::Decoder()
{
}

Decoder::~Decoder()
{
}

void Decoder::setKey(char *newkey, int size)
{
key = (char*)malloc(size);
memcpy(key, newkey, size);
printf("key was issued by the server\n");
}

long Decoder::decode(char *input, int size)
{
if(key == NULL)
return 0L;
int i = 0;
int j = 0;
for(int k=0; k < size; k++)
{
int i1 = input[k] & 0xFF;
input[k] = (char)(i1 ^ key[j++] & 0xFF ^i);
i = i1;
if(j>7)
j=0;
}
long l = key[0] & 0xFF;
l |= key[1] << 8 & 0xFF00;
l |= key[2] << 16 & 0xFF0000;
l |= key[3] << 24 & 0xFF000000;
l += size;
key[0] = (char)(l & 255L);
key[1] = (char)(l >> 8 & 255L);
key[2] = (char)(l >> 16 & 255L);
key[3] = (char)(l >> 24 & 255L);

return l;
}
Decoder.h
#ifndef __Decoder_h__
#define __Decoder_h__

class Decoder
{
private:

public:
char *key;
Decoder();
~Decoder();
void setKey(char *newkey, int size);
long decode(char *input, int size);
};

#endif
hope it helps... if you search you will find the whole source posted by digital...

EDIT: also, the 0x00 packet aparently also contains the key of which is used for the decryption of the server packets...
PacketProcessor.cpp
if(sizeGamePacket == 0x0c && newbuffer[2] == 0x00 && decode == 0)
{
char key[8];
key[0] = newbuffer[4];
key[1] = newbuffer[5];
key[2] = newbuffer[6];
key[3] = newbuffer[7];
key[4] = (char)0xA1;
key[5] = (char)0x6C;
key[6] = (char)0x54;
key[7] = (char)0x87;

decoder->setKey(key, sizeof(key));

size = 0;
decode = 1;
continue;
}

thats also from l2pf

aralyon
14.03.2006, 18:12
I'm using this java code (extracted from l2j and compiled under .net as J#)
public long decrypt(ubyte[] raw)
{
if (_key == null)
{
return 0;
}

int temp = 0;
int j = 0;
for (int i = 0; i < raw.length; i++)
{
int temp2 = raw[i] & 0xff;
raw[i] = (ubyte)(temp2 ^ (_key[j++] & 0xff) ^ temp);
temp = temp2;
if (j > 7)
{
j = 0;
}
}

long old = _key[0] & 0xff;
old |= _key[1] << 8 & 0xff00;
old |= _key[2] << 0x10 & 0xff0000;
old |= _key[3] << 0x18 & 0xff000000;

old += raw.length;

_key[0] = (ubyte)(old & 0xff);
_key[1] = (ubyte)(old >> 0x08 & 0xff);
_key[2] = (ubyte)(old >> 0x10 & 0xff);
_key[3] = (ubyte)(old >> 0x18 & 0xff);

return 1;
}

It looks same as that C++ code :?
As I wrote, it's ok with l2j, but there are some bad bytes (just some, most are ok) if I use it with normal server

aralyon
14.03.2006, 18:14
And where is the source of l2packet framework? If I search PP or google for "l2packet" I got only rubbish :(

testdude
14.03.2006, 18:23
check pm

Shawnmb
14.03.2006, 18:25
Or maybe where to find the program itself :D

vex
14.03.2006, 21:11
please pm me too

Shawnmb
15.03.2006, 00:09
And me >.>

testdude
15.03.2006, 10:04
-.- just search for "packet" posted by digital -.-