~ruther/NosSmooth

ref: b1ca4f80bce17293b0e78f73cb01a9e058079544 NosSmooth/Core/NosSmooth.Cryptography/ServerLoginCryptography.cs -rw-r--r-- 1.5 KiB
b1ca4f80 — Rutherther feat(crypto): add cryptography server, client, world, login... 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
//
//  ServerLoginCryptography.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;

namespace NosSmooth.Cryptography;

/// <summary>
/// A cryptography used for logging to NosTale server from the server.
/// </summary>
public class ServerLoginCryptography : ICryptography
{
    /// <inheritdoc />
    public string Decrypt(in ReadOnlySpan<byte> str, Encoding encoding)
    {
        try
        {
            string decryptedPacket = string.Empty;

            foreach (byte character in str)
            {
                if (character > 14)
                {
                    decryptedPacket += Convert.ToChar((character - 15) ^ 195);
                }
                else
                {
                    decryptedPacket += Convert.ToChar((256 - (15 - character)) ^ 195);
                }
            }

            return decryptedPacket;
        }
        catch
        {
            return string.Empty;
        }
    }

    /// <inheritdoc />
    public byte[] Encrypt(string packet, Encoding encoding)
    {
        try
        {
            packet += " ";
            byte[] tmp = Encoding.Default.GetBytes(packet);
            for (int i = 0; i < packet.Length; i++)
            {
                tmp[i] = Convert.ToByte(tmp[i] + 15);
            }
            tmp[tmp.Length - 1] = 25;
            return tmp;
        }
        catch
        {
            return Array.Empty<byte>();
        }
    }
}