~ruther/NosSmooth

ref: ed76c27b262982f8550d615a3ed83f669c72f2d5 NosSmooth/Core/NosSmooth.Cryptography/ServerWorldCryptography.cs -rw-r--r-- 7.5 KiB
ed76c27b — Rutherther docs: fix MateWalkCommand header 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//
//  ServerWorldCryptography.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Text;
using NosSmooth.Cryptography.Extensions;

namespace NosSmooth.Cryptography;

/// <summary>
/// A cryptography used on world server, has to have a session id (encryption key) set from the world.
/// </summary>
public class ServerWorldCryptography : ICryptography
{
    /// <summary>
    /// Initializes a new instance of the <see cref="ServerWorldCryptography"/> class.
    /// </summary>
    /// <param name="encryptionKey">The encryption key.</param>
    public ServerWorldCryptography(int encryptionKey)
    {
        EncryptionKey = encryptionKey;
    }

    /// <summary>
    /// Gets or sets the encryption key.
    /// </summary>
    public int EncryptionKey { get; set; }

    /// <inheritdoc />
    public string Decrypt(in ReadOnlySpan<byte> str, Encoding encoding)
    {
        if (EncryptionKey == 0)
        {
            return DecryptUnauthed(str);
        }

        return DecryptAuthed(str, EncryptionKey, encoding);
    }

    /// <inheritdoc />
    public byte[] Encrypt(string packet, Encoding encoding)
    {
        byte[] strBytes = encoding.GetBytes(packet);
        int bytesLength = strBytes.Length;

        byte[] encryptedData = new byte[bytesLength + (int)Math.Ceiling((decimal)bytesLength / 0x7E) + 1];

        int ii = 0;
        for (int i = 0; i < bytesLength; i++)
        {
            if (i % 0x7E == 0)
            {
                encryptedData[i + ii] = (byte)(bytesLength - i > 0x7E ? 0x7E : bytesLength - i);
                ii++;
            }
            encryptedData[i + ii] = (byte)~strBytes[i];
        }
        encryptedData[encryptedData.Length - 1] = 0xFF;

        return encryptedData;
    }

    private static string DecryptAuthed(in ReadOnlySpan<byte> str, int encryptionKey, Encoding encoding)
    {
        var encryptedString = new StringBuilder(str.Length);

        int sessionKey = encryptionKey & 0xFF;
        byte sessionNumber = unchecked((byte)(encryptionKey >> 6));
        sessionNumber &= 0xFF;
        sessionNumber &= 3;

        byte firstbyte = unchecked((byte)(sessionKey + 0x40));

        switch (sessionNumber)
        {
            case 0:
            case 1:
                foreach (byte character in str)
                {
                    byte highbyte = unchecked((byte)(character + firstbyte));
                    encryptedString.Append((char)highbyte);
                }

                break;
            case 2:
            case 3:
                foreach (byte character in str)
                {
                    byte highbyte = unchecked((byte)(character + firstbyte ^ 0xC3));
                    encryptedString.Append((char)highbyte);
                }

                break;
            default:
                encryptedString.Append((char)0xF);
                break;
        }

        var temp = encryptedString.ToString().SplitAllocationless((char)0xFF);
        var save = new StringBuilder(encryptedString.Length);

        foreach (var packet in temp)
        {
            save.Append(DecryptPrivate(packet, encoding));
            save.Append((char)'\n');
        }

        return save.ToString();
    }

    private static string DecryptPrivate(in ReadOnlySpan<char> str, Encoding encoding)
    {
        using var receiveData = new MemoryStream();
        char[] table = { ' ', '-', '.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\n' };
        for (int count = 0; count < str.Length; count++)
        {
            if (str[count] <= 0x7A)
            {
                int len = str[count];

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

                    try
                    {
                        receiveData.WriteByte(unchecked((byte)(str[count] ^ 0xFF)));
                    }
                    catch
                    {
                        receiveData.WriteByte(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.WriteByte(unchecked((byte)table[highbyte - 1]));
                        i++;
                    }

                    if (lowbyte != 0x0 && lowbyte != 0xF)
                    {
                        receiveData.WriteByte(unchecked((byte)table[lowbyte - 1]));
                    }
                }
            }
        }

        byte[] tmp = Encoding.Convert(encoding, Encoding.UTF8, receiveData.ToArray());
        return Encoding.UTF8.GetString(tmp);
    }

    private static string DecryptUnauthed(in ReadOnlySpan<byte> str)
    {
        try
        {
            var encryptedStringBuilder = new StringBuilder(str.Length);
            for (int i = 1; i < str.Length; i++)
            {
                if (Convert.ToChar(str[i]) == 0xE)
                {
                    return encryptedStringBuilder.ToString();
                }

                int firstbyte = Convert.ToInt32(str[i] - 0xF);
                int secondbyte = firstbyte;
                secondbyte &= 240;
                firstbyte = Convert.ToInt32(firstbyte - secondbyte);
                secondbyte >>= 4;

                switch (secondbyte)
                {
                    case 0:
                    case 1:
                        encryptedStringBuilder.Append(' ');
                        break;

                    case 2:
                        encryptedStringBuilder.Append('-');
                        break;

                    case 3:
                        encryptedStringBuilder.Append('.');
                        break;

                    default:
                        secondbyte += 0x2C;
                        encryptedStringBuilder.Append(Convert.ToChar(secondbyte));
                        break;
                }

                switch (firstbyte)
                {
                    case 0:
                    case 1:
                        encryptedStringBuilder.Append(' ');
                        break;

                    case 2:
                        encryptedStringBuilder.Append('-');
                        break;

                    case 3:
                        encryptedStringBuilder.Append('.');
                        break;

                    default:
                        firstbyte += 0x2C;
                        encryptedStringBuilder.Append(Convert.ToChar(firstbyte));
                        break;
                }
            }

            return encryptedStringBuilder.ToString();
        }
        catch (OverflowException)
        {
            return string.Empty;
        }
    }
}
Do not follow this link