-
Notifications
You must be signed in to change notification settings - Fork 3
/
MapleStream.cs
116 lines (101 loc) · 3.89 KB
/
MapleStream.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
namespace MapleShark
{
public enum TransformLocale
{
SPECIAL,
AES,
AES_MCRYPTO,
MCRYPTO,
OLDEST_MCRYPTO,
NONE,
}
public sealed class MapleStream
{
private const int DEFAULT_SIZE = 4096;
private bool mOutbound = false;
private MapleDES mDES = null;
private byte[] mBuffer = new byte[DEFAULT_SIZE];
private int mCursor = 0;
public MapleStream(bool pOutbound, ushort pBuild, byte pLocale, byte[] pIV) {
mOutbound = pOutbound;
mDES = new MapleDES(pBuild, pLocale);
}
public void Append(byte[] packetBuffer, ref byte[] dataBuffer)
{
byte[] temp = new byte[dataBuffer.Length + packetBuffer.Length];
dataBuffer.CopyTo(temp, 0);
packetBuffer.CopyTo(temp, dataBuffer.Length);
dataBuffer = new byte[temp.Length];
temp.CopyTo(dataBuffer, 0);
Console.WriteLine("APPENDED: " + dataBuffer.Length);
}
public MaplePacket Read(DateTime pTransmitted, ushort pBuild, byte pLocale, ref bool firstPacket, ref byte[] dataBuffer, ref byte[] curIV)
{
Console.WriteLine(dataBuffer.Length);
Console.WriteLine("BUFFER DATA:\n--------");
for (int i = 0; i < dataBuffer.Length; i++)
{
Console.Write("0x");
if (dataBuffer[i] < 16)
{
Console.Write("0");
Console.Write(dataBuffer[i].ToString("X"));
}
else
{
Console.Write(dataBuffer[i].ToString("X"));
}
Console.Write(" ");
}
Console.WriteLine("-------");
if (dataBuffer.Length == 0)
{
return null;
}
ushort packetSize = mDES.GetHeaderLength(dataBuffer, 0);
if (packetSize > dataBuffer.Length)
{
Console.WriteLine("INCOMPLETE PACKET:" + packetSize + "Buffer Size: " + dataBuffer.Length);
for (int i = 0; i < dataBuffer.Length; i++)
{
Console.Write("0x");
if (dataBuffer[i] < 16)
{
Console.Write("0");
Console.Write(dataBuffer[i].ToString("X"));
}
else
{
Console.Write(dataBuffer[i].ToString("X"));
}
Console.Write(" ");
}
return null;
}
curIV = mDES.GetIV(dataBuffer);
mDES.SetIV(curIV);
byte[] packetBuffer = new byte[packetSize];
Buffer.BlockCopy(dataBuffer, 16, packetBuffer, 0, packetSize - 26); // last two bytes not needed(18) + 8
//bool byteheader = false;
byte[] decryptedBuffer = mDES.Decrypt(packetBuffer, firstPacket, true, null);
if (firstPacket)
{
Console.Write("\nCHANGING KEY");
mDES.SetKey(decryptedBuffer);
firstPacket = false;
}
byte[] temp = new byte[dataBuffer.Length - packetSize];
Buffer.BlockCopy(dataBuffer, packetSize, temp, 0, dataBuffer.Length - packetSize);
dataBuffer = new byte[temp.Length];
temp.CopyTo(dataBuffer, 0);
ushort opcode=1;
Definition definition = Config.Instance.GetDefinition(pBuild, pLocale, mOutbound, opcode);
return new MaplePacket(pTransmitted, mOutbound, pBuild, pLocale, opcode, definition == null ? "" : definition.Name, decryptedBuffer);
}
}
}