Need help with decoding RSA

V0RT4C

New Member
Joined
May 3, 2019
Messages
2
Best answers
0
Reaction score
2
Hello,
I'm trying to code a login server in nodejs for the Nostalrius 7.7 server. However I've been struggling a couple of days now with decoding the XTEA part of the request from the client.
Im using the node-rsa package to do this and I'm pretty sure I've got the correct private RSA key.
The error that I get says that i probably have the wrong key, but I've double checked and I think its something else.
The error looks like this: Error: Error during decryption (probably incorrect key). Original error: Error: error:04099079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:eek:aep decoding error

And I get it as soon as I send the request from OTClient.
So I'm pretty stuck here and cant get any further before I've got this decoding issue solved. I dont want to use any existing login server, I'm doing this because I want to learn. If anyone can help me with this I would be really glad. I can pay you with Bitcoins or however you prefer. I just need this issue sorted because it's really driving me crazy. Thank you :)

This is my code:
JavaScript:
let net = require('net');
let NodeRSA = require('node-rsa');
let pkey = `-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCbZGkDtFsHrJVlaNhzU71xZROd15QHA7A+bdB5OZZhtKg3qmBWHXzLlFL6AIBZ
SQmIKrW8pYoaGzX4sQWbcrEhJhHGFSrT27PPvuetwUKnXT11lxUJwyHFwkpb1R/UYPAbThW+sN4Z
MFKKXT8VwePL9cQB1nd+EKyqsz2+jVt/9QIDAQABAoGAQovTtTRtr3GnYRBvcaQxAvjIV9ZUnFRm
C7Y3i1KwJhOZ3ozmSLrEEOLqTgoc7R+sJ1YzEiDKbbete11EC3gohlhW56ptj0WDf+7ptKOgqiEy
Kh4qt1sYJeeGz4GiiooJoeKFGdtk/5uvMR6FDCv6H7ewigVswzf330Q3Ya7+jYECQQERBxsga6+5
x6IofXyNF6QuMqvuiN/pUgaStUOdlnWBf/T4yUpKvNS1+I4iDzqGWOOSR6RsaYPYVhj9iRABoKyx
AkEAkbNzB6vhLAWht4dUdGzaREF3p4SwNcu5bJRa/9wCLSHaS9JaTq4lljgVPp1zyXyJCSCWpFnl
0WvK3Qf6nVBIhQJBANS7rK8+ONWQbxENdZaZ7Rrx8HUTwSOS/fwhsGWBbl1Qzhdq/6/sIfEHkfeH
1hoH+IlpuPuf21MdAqvJt+cMwoECQF1LyBOYduYGcSgg6u5mKVldhm3pJCA+ZGxnjuGZEnet3qeA
eb05++112fyvO85ABUun524z9lokKNFh45NKLjUCQGshzV43P+RioiBhtEpB/QFzijiS4L2HKNu1
tdhudnUjWkaf6jJmQS/ppln0hhRMHlk9Vus/bPx7LtuDuo6VQDo=
-----END RSA PRIVATE KEY-----`;


const key = new NodeRSA();
key.setOptions({ encryptionScheme: 'pkcs1_oaep', environment: 'node'});
key.importKey(pkey);


let server = new net.createServer(function(socket){
    socket.on('data', function(data){
        let packetLength = data.readUInt16LE(0);
        let command = data.readUInt8(2);
        let os = data.readUInt16LE(3);
        let version = data.readUInt16LE(5);
        let spr = data.readInt32LE(7);
        let dat = data.readInt32LE(11);
        let pic = data.readInt32LE(15);
        let encryptedBuffer = data.slice(19, data.length);
        console.log(encryptedBuffer.length); // 128
        console.log(key.decrypt(encryptedBuffer, 'buffer'));
    });
});

server.listen(7171, '127.0.0.1');
 

Qbazzz

Pół człowiek, pół litra
Joined
Jul 3, 2009
Messages
88
Best answers
1
Reaction score
49
Location
inside of the box
Hello.
You are building packet without some important things. Check my very old code from vanilla client dll.
Here is code for building a packet:
C++:
    XTEA xtea;
    char* sizedPacket = xtea.addPacketSize(buf, &len);
    char* packetDivisible = xtea.makeItDivisibleBy8(sizedPacket, &len);
    xtea.encrypt((uint8_t*)packetDivisible, len);
    char* sendablePacket = xtea.finalizePacket(packetDivisible, &len);
    MySendFunction(tibiaSocket, sendablePacket, len, 0);
Functions:
C++:
char* XTEA::addPacketSize(char* buffer, int* len)
{
    int sizedPacketSize = *len + 2;
    char* sizedPacket = new char[sizedPacketSize];
    memcpy(sizedPacket, len, 2);
    memcpy(sizedPacket+2, buffer, *len);
    *len = sizedPacketSize;
    return sizedPacket;
}
C++:
char* XTEA::makeItDivisibleBy8(char* buffer, int* len){
    int packetLenght = *len;
    if ((packetLenght % 8) != 0)
    {
        int newMultiple = (packetLenght/8) + 1;
        int newPacketLenght = newMultiple * 8;
        char* newPacket = new char[newPacketLenght];
        for (int i = 0; i<newPacketLenght; i++)
        {
            if (i < packetLenght) newPacket[i] = buffer[i];
            else newPacket[i] = rand() % 255;
        }
        *len = newPacketLenght;
        return newPacket;
    }

    return buffer;
}
C++:
char* XTEA::finalizePacket(char* packet, int* len)
{
    int finalPacketLenght = *len;
    char* finalPacket = new char[finalPacketLenght+2];
    memcpy(finalPacket, &finalPacketLenght, 2);
    memcpy(finalPacket+2, packet, *len);
    *len = finalPacketLenght + 2;
    return finalPacket;
}
C++:
void encipher(unsigned int num_rounds, uint32_t v[2], uint32_t const key[4]) {
    unsigned int i;
    uint32_t v0=v[0], v1=v[1], sum=0, delta=0x9E3779B9;
    for (i=0; i < num_rounds; i++) {
        v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
        sum += delta;
        v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
    }
    v[0]=v0; v[1]=v1;
}

void decipher(unsigned int num_rounds, uint32_t v[2], uint32_t const key[4]) {
    unsigned int i;
    uint32_t v0=v[0], v1=v[1], delta=0x9E3779B9, sum=delta*num_rounds;
    for (i=0; i < num_rounds; i++) {
        v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
        sum -= delta;
        v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
    }
    v[0]=v0; v[1]=v1;
}
void XTEA::setKey(const uint32_t* key)
{
    memcpy(&m_xkey, key, 16);
}
void XTEA::encrypt(uint8_t* buffer, uint32_t size)
{
    for (uint32_t i = 0; i < size; i += 8)
    {
        //encipher(32, (uint32_t*)&buffer[i], m_xkey);
        encipher(32, reinterpret_cast<uint32_t*> (&buffer[i]), m_xkey);
    }
}
void XTEA::decrypt(uint8_t* buffer, uint32_t size)
{
    for (uint32_t i = 0; i < size; i += 8)
    {
        //decipher (32, (uint32_t*)&buffer[i], m_xkey);
        decipher (32, reinterpret_cast<uint32_t*> (&buffer[i]), m_xkey);
    }
}
Using of xtea you should find somewhere in the internet for your coding lang, rest you need to translate xd
 
Top