-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMapleAES.cs
112 lines (102 loc) · 3.1 KB
/
MapleAES.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
using System;
using System.IO;
using System.Security.Cryptography;
namespace MapleShark
{
public sealed class MapleDES
{
private static byte[] sSecretKey = new byte[] {
0xC7, 0xD8, 0xC4, 0xBF, 0xB5, 0xE9, 0xC0, 0xFD
};
private static byte[] sDynamicKey = new byte[] {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
private ushort mBuild = 0;
private DES mDES = DES.Create();
private ICryptoTransform mTransformer = null;
public byte[] mIV { get; private set; }
internal MapleDES(ushort pBuild, byte pLocale)
{
mBuild = pBuild;
if ((short)pBuild < 0) { // Second one
pBuild = (ushort)(0xFFFF - pBuild);
}
mDES.Key = sSecretKey;
mDES.Mode = CipherMode.CBC;
mTransformer = mDES.CreateEncryptor();
}
public ushort GetHeaderLength(byte[] pBuffer, int pStart)
{
return BitConverter.ToUInt16(pBuffer, pStart);
}
public byte[] GetIV(byte[] pBuffer)
{
byte[] packetiv;
packetiv = new byte[8];
for (int i = 8; i < 16; i++)
{
packetiv[i-8] = pBuffer[i];
}
for (int i = 0; i < packetiv.Length; i++)
{
Console.Write("0x");
if (packetiv[i] < 16)
{
Console.Write("0");
Console.Write(packetiv[i].ToString("X"));
}
else
{
Console.Write(packetiv[i].ToString("X"));
}
Console.Write(" ");
}
return packetiv;
}
public void SetIV(byte[] iv)
{
mDES.IV = iv;
}
public void SetKey(byte[] pBuffer)
{
for (int i = 49; i < 57; i++)
{
sDynamicKey[i - 49] = pBuffer[i];
}
for (int i = 0; i < sDynamicKey.Length; i++)
{
Console.Write("0x");
if (sSecretKey[i] < 16)
{
Console.Write("0");
Console.Write(sDynamicKey[i].ToString("X"));
}
else
{
Console.Write(sDynamicKey[i].ToString("X"));
}
Console.Write(" ");
}
}
public byte[] Decrypt(byte[] pBuffer, bool firstPacket, bool useStandardIV, byte[] nonStandardIV)
{
if (firstPacket)
{
mDES.Key = sSecretKey;
}
else
{
mDES.Key = sDynamicKey;
}
if (!useStandardIV)
{
mDES.IV = nonStandardIV;
}
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms,
mDES.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(pBuffer, 0, pBuffer.Length-18);
return ms.ToArray();
}
}
}