Nostale World Encryption

07/08/2018 12:01 PainToTheWorld#1
Hi
ich versuche das:
Code:
public static string Decrypt2(string str)
        {
            List<byte> receiveData = new List<byte>();
            char[] table = { ' ', '-', '.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'n' };
            int count;
            for (count = 0; count < str.Length; count++)
            {
                if (str[count] <= 0x7A)
                {
                    int len = str[count];

                    for (int i = 0; i < len; i++)
                    {
                        count++;

                        try
                        {
                            receiveData.Add(unchecked((byte)(str[count] ^ 0xFF)));
                        }
                        catch
                        {
                            receiveData.Add(255);
                        }
                    }
                }
                else
                {
                    int len = str[count];
                    len &= 0x7F;

                    for (int i = 0; i < len; i++)
                    {
                        count++;
                        int highbyte;
                        try
                        {
                            highbyte = str[count];
                        }
                        catch
                        {
                            highbyte = 0;
                        }
                        highbyte &= 0xF0;
                        highbyte >>= 0x4;

                        int lowbyte;
                        try
                        {
                            lowbyte = str[count];
                        }
                        catch
                        {
                            lowbyte = 0;
                        }
                        lowbyte &= 0x0F;

                        if (highbyte != 0x0 && highbyte != 0xF)
                        {
                            receiveData.Add(unchecked((byte)table[highbyte - 1]));
                            i++;
                        }

                        if (lowbyte != 0x0 && lowbyte != 0xF)
                        {
                            receiveData.Add(unchecked((byte)table[lowbyte - 1]));
                        }
                    }
                }
            }
            return Encoding.UTF8.GetString(Encoding.Convert(Encoding.Default, Encoding.UTF8, receiveData.ToArray()));
}

public override string Decrypt(byte[] str, int sessionId = 0)
        {
            string encrypted_string = "";
            int session_key = sessionId & 0xFF;
            byte session_number = unchecked((byte)(sessionId >> 6));
            session_number &= 0xFF;
            session_number &= unchecked((byte)0x80000003);

            switch (session_number)
            {
                case 0:
                    foreach (byte character in str)
                    {
                        byte firstbyte = unchecked((byte)(session_key + 0x40));
                        byte highbyte = unchecked((byte)(character - firstbyte));
                        encrypted_string += (char)highbyte;
                    }
                    break;

                case 1:
                    foreach (byte character in str)
                    {
                        byte firstbyte = unchecked((byte)(session_key + 0x40));
                        byte highbyte = unchecked((byte)(character + firstbyte));
                        encrypted_string += (char)highbyte;
                    }
                    break;

                case 2:
                    foreach (byte character in str)
                    {
                        byte firstbyte = unchecked((byte)(session_key + 0x40));
                        byte highbyte = unchecked((byte)(character - firstbyte ^ 0xC3));
                        encrypted_string += (char)highbyte;
                    }
                    break;

                case 3:
                    foreach (byte character in str)
                    {
                        byte firstbyte = unchecked((byte)(session_key + 0x40));
                        byte highbyte = unchecked((byte)(character + firstbyte ^ 0xC3));
                        encrypted_string += (char)highbyte;
                    }
                    break;

                default:
                    encrypted_string += (char)0xF;
                    break;
            }

            string[] temp = encrypted_string.Split((char)0xFF);
            string save = "";

            for (int i = 0; i < temp.Length; i++)
            {
                save += Decrypt2(temp[i]);
                if (i < temp.Length - 2)
                {
                    save += (char)0xFF;
                }
            }

            return save;
}
in Python3 zu übersetzen, die ersten Zahlen werden richtig dargestellt der rest jedoch nicht.

Das ist meine Function:
Code:
def decrypt2(_packet):
    decrypted_string = ''
    table = [' ', '-', '.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'n']

    count = 0
    while count < len(_packet):
        if ord(_packet[count]) <= 0x7A:
            _len = ord(_packet[count])
            for i in range(1, _len):
                count += 1
                decrypted_string += chr(ord(_packet[count]) ^ 0xFF)
        else:
            _len = ord(_packet[count])
            _len &= 0x7F

            i = 0
            while i < _len:
                count += 1

                try:
                    highbyte = ord(_packet[count])
                    highbyte &= 0xF0
                    highbyte >>= 0x4
                except IndexError:
                    highbyte = 0

                try:
                    lowbyte = ord(_packet[count])
                    lowbyte &= 0x0F
                except IndexError:
                    lowbyte = 0

                if highbyte != 0x0 and highbyte != 0xF:
                    decrypted_string += table[highbyte - 1]

                if lowbyte != 0x0 and lowbyte != 0xF:
                    decrypted_string += table[lowbyte - 1]

                i += 1
            count += 1
    print('Decrypted:', decrypted_string)
    return decrypted_string

def decrypt(_packet, session_id):
    encrypted_string = ""
    session_key = session_id & 0xFF
    session_number = session_id >> 6
    session_number &= 0xFF
    session_number &= 0x80000003


    if session_number == 0:
        for i in range(0, len(_packet)):
            firstbyte = session_key + 0x40
            highbyte = (int(_packet[i]) - firstbyte)
            encrypted_string += chr(highbyte)
    elif session_number == 1:
        for i in range(0, len(_packet)):
            firstbyte = session_key + 0x40
            highbyte = (int(_packet[i]) + firstbyte)
            encrypted_string += chr(highbyte)
    elif session_number == 2:
        for i in range(0, len(_packet)):
            firstbyte = session_key + 0x40
            highbyte = (int(_packet[i]) - firstbyte) ^ 0xC3
            encrypted_string += chr(highbyte)
    elif session_number == 3:
        for i in range(0, len(_packet)):
            firstbyte = session_key + 0x40
            highbyte = (int(_packet[i]) + firstbyte) ^ 0xC3
            encrypted_string += chr(highbyte)
    else:
        encrypted_string += chr(0xF)

    temp = encrypted_string.split(chr(0xFF))
    save = ""

    for i in range(0, len(temp)):
        save += decrypt2(temp[i])
        if i < len(temp[i]) -2:
            save += chr(0xFF)

    return save
Kennt sich jemand damit aus und sieht was ich falsch mache?
07/08/2018 12:08 Voria#2
@[Only registered and activated users can see links. Click Here To Register...] Was möchtest du damit bezwecken?
was waren denn deine Hintergründe warum du diesen Code geschrieben hast?
und was soll das jetzt genau sein?
Diese fragen stelle ich damit ich dir besser Helfen kann.
07/08/2018 12:54 Pumba98#3
Quote:
Originally Posted by Voria View Post
@[Only registered and activated users can see links. Click Here To Register...] Was möchtest du damit bezwecken?
was waren denn deine Hintergründe warum du diesen Code geschrieben hast?
und was soll das jetzt genau sein?
Diese fragen stelle ich damit ich dir besser Helfen kann.
Er will den Code von c# nach Python übersetzen?!
Ist die OpenNos World Encryption, aber wüsste nicht wie das beim lösen des Problems hilft.



Ich kann zwar kein/kaum Python & C#, aber wie kommst du von
for (int i = 0; i < len; i++)
auf
for i in range(1, _len)

ansonsten ist die Frage ob du sowas wie
unchecked((byte)IRGENDWAS)
korrekt umgesetzt hast, kann ich nicht wirklich was zu sagen.


Ich würde einfach mal beides parallel mit der selben Eingabe debuggen und schauen wo Unterschiede auftreten

Ansonsten kann man dir im [Only registered and activated users can see links. Click Here To Register...] vielleicht auch kompetenter helfen
07/09/2018 09:32 Ethnical#4
@[Only registered and activated users can see links. Click Here To Register...] Do you have the (encrypt function) for the world in python ?