forked from defaultmagi/maplelib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypt.go
354 lines (297 loc) · 9.45 KB
/
crypt.go
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
Copyright 2014-2015 Franc[e]sco (lolisamurai@tfwno.gf)
This file is part of maplelib-go.
maplelib-go is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
maplelib-go is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with maplelib-go. If not, see <http://www.gnu.org/licenses/>.
*/
package maplelib
import (
"crypto/aes"
"crypto/cipher"
"fmt"
)
/*
A Crypt is an encryption key for MapleStory packets.
It consists of a 4-byte value repeated four times for a total of 16 bytes.
MapleStory uses two keys: one is used to encrypt sent packets and the other
is used to decrypt received packets. This is valid for both the client and
the server.
The two keys are randomly generated when a client connects and sent
in the unencrypted handshake packet which is the first packet that is sent
to a client that connects to a server.
The MapleStory protocol will shift encryption keys every time a packet is
send or received, so you will have to call .Shuffle() every time this
happens.
*/
type Crypt struct {
mapleVersion uint16
key [16]byte
}
const encryptedHeaderSize = 4
const blocksize = 1460
var aeskey = [32]byte{
0x13, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00,
0x06, 0x00, 0x00, 0x00,
0xB4, 0x00, 0x00, 0x00,
0x1B, 0x00, 0x00, 0x00,
0x0F, 0x00, 0x00, 0x00,
0x33, 0x00, 0x00, 0x00,
0x52, 0x00, 0x00, 0x00,
}
// NewCrypt initializes and returns an encryption key
func NewCrypt(key [4]byte, mapleVersion uint16) Crypt {
var res Crypt
// Repeats the key 4 times
for i := 0; i < 4; i++ {
copy(res.key[4*i:], key[:])
}
res.mapleVersion = encodeMapleVersion(mapleVersion)
return res
}
func (c Crypt) String() string {
return fmt.Sprintf("MapleCrypt[v%d]{% X}", c.MapleVersion(), c.key)
}
// MapleVersion returns the target MapleStory version for this encryption key
func (c *Crypt) MapleVersion() uint16 {
return decodeMapleVersion(c.mapleVersion)
}
// IV returns the current initialization vector of this key (16 bytes)
func (c *Crypt) IV() []byte {
return c.key[:]
}
// Encrypt encrypts the given array of bytes.
// NOTE: the array must have 4 bytes of space at the beginning for the encrypted
// header
func (c *Crypt) Encrypt(buffer []byte) {
c.makeHeader(buffer)
mapleCrypt(buffer[encryptedHeaderSize:])
c.aesCrypt(buffer[encryptedHeaderSize:])
}
// EncryptNoShanda encrypts the given array of bytes only using maple's AES.
// NOTE: the array must have 4 bytes of space at the beginning for the encrypted
// header
func (c *Crypt) EncryptNoShanda(buffer []byte) {
c.makeHeader(buffer)
c.aesCrypt(buffer[encryptedHeaderSize:])
}
// Decrypt decrypts the given array of bytes.
// NOTE: you must omit the first 4 bytes (encrypted header)
func (c *Crypt) Decrypt(buffer []byte) {
c.aesCrypt(buffer[:])
mapleDecrypt(buffer[:])
}
// DecryptNoShanda decrypts the given array of bytes only using maple's AES.
// NOTE: you must omit the first 4 bytes (encrypted header)
func (c *Crypt) DecryptNoShanda(buffer []byte) {
c.aesCrypt(buffer[:])
}
func (c *Crypt) makeHeader(buffer []byte) {
cb := uint16(len(buffer) - encryptedHeaderSize)
// I have no idea what I'm doing
// this encryption shit was reversed from the game itself
iiv := uint16(c.key[3] & 0xFF) // is the & 0xFF even needed?
iiv |= uint16(c.key[2]) << 8 & 0xFF00
iiv ^= c.mapleVersion
mlength := uint16((cb << 8 & 0xFF00) | cb>>8)
xoredIv := iiv ^ mlength
buffer[0] = byte(iiv >> 8 & 0xFF)
buffer[1] = byte(iiv & 0xFF)
buffer[2] = byte(xoredIv >> 8 & 0xFF)
buffer[3] = byte(xoredIv & 0xFF)
}
// GetPacketLength decodes the packet length from the encrypted header.
// NOTE: this size does not include the 4-byte encrypted header.
func GetPacketLength(encryptedHeader []byte) int {
return int((uint16(encryptedHeader[0]) + uint16(encryptedHeader[1])*0x100) ^
(uint16(encryptedHeader[2]) + uint16(encryptedHeader[3])*0x100))
}
func encodeMapleVersion(mapleVersion uint16) (res uint16) {
// I dunno why they do this, looks like some kind of way to encrypt the
// game version
res = 0xFFFF - mapleVersion
res = (res >> 8 & 0xFF) | (res << 8 & 0xFF00)
return
}
func decodeMapleVersion(mapleVersion uint16) (res uint16) {
res = (mapleVersion >> 8 & 0xFF) | (mapleVersion << 8 & 0xFF00)
res = -(res - 0xFFFF)
return
}
// Shuffle shuffles the current key after an encryption or decryption
func (c *Crypt) Shuffle() {
// I have no idea what I'm doing
// this encryption shit was reversed from the game itself
// credits to vana for the key shuffle code
var im12andwh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0x86, 0x9F, 0x73, 0xE6, 0x0A, 0xDE, 0x2B, 0x99, 0x4A, 0x47, 0x9C, 0xDF,
0x09, 0x76, 0x9E, 0x30, 0x0E, 0xE4, 0xB2, 0x94, 0xA0, 0x3B, 0x34, 0x1D,
0x28, 0x0F, 0x36, 0xE3, 0x23, 0xB4, 0x03, 0xD8, 0x90, 0xC8, 0x3C, 0xFE,
0x5E, 0x32, 0x24, 0x50, 0x1F, 0x3A, 0x43, 0x8A, 0x96, 0x41, 0x74, 0xAC,
0x52, 0x33, 0xF0, 0xD9, 0x29, 0x80, 0xB1, 0x16, 0xD3, 0xAB, 0x91, 0xB9,
0x84, 0x7F, 0x61, 0x1E, 0xCF, 0xC5, 0xD1, 0x56, 0x3D, 0xCA, 0xF4, 0x05,
0xC6, 0xE5, 0x08, 0x49,
}
newiv := [4]byte{0xF2, 0x53, 0x50, 0xC6}
var input, valueinput byte
var fulliv, shift uint32
for i := byte(0); i < 4; i++ {
input = c.key[i]
valueinput = im12andwhatisthis[input]
newiv[0] += im12andwhatisthis[newiv[1]] - input
newiv[1] -= newiv[2] ^ valueinput
newiv[2] ^= im12andwhatisthis[newiv[3]] + input
newiv[3] -= newiv[0] - valueinput
fulliv = uint32(newiv[3])<<24 | uint32(newiv[2])<<16 |
uint32(newiv[1])<<8 | uint32(newiv[0])
shift = fulliv>>0x1D | fulliv<<0x03
newiv[0] = byte(shift & uint32(0xFF))
newiv[1] = byte(shift >> 8 & uint32(0xFF))
newiv[2] = byte(shift >> 16 & uint32(0xFF))
newiv[3] = byte(shift >> 24 & uint32(0xFF))
}
for i := byte(0); i < 4; i++ {
copy(c.key[4*i:], newiv[:])
}
}
func ror(val byte, num int) byte {
for i := 0; i < num; i++ {
var lowbit int
if val&1 > 0 {
lowbit = 1
} else {
lowbit = 0
}
val >>= 1
val |= byte(lowbit << 7)
}
return val
}
func rol(val byte, num int) byte {
var highbit int
for i := 0; i < num; i++ {
if val&0x80 > 0 {
highbit = 1
} else {
highbit = 0
}
val <<= 1
val |= byte(highbit)
}
return val
}
func mapleDecrypt(buf []byte) {
// I have no idea what I'm doing
// this encryption shit was reversed from the game itself
var j int32
var a, b, c byte
for i := byte(0); i < 3; i++ {
a = 0
b = 0
for j = int32(len(buf)); j > 0; j-- {
c = buf[j-1]
c = rol(c, 3)
c ^= 0x13
a = c
c ^= b
c = byte(int32(c) - j)
c = ror(c, 4)
b = a
buf[j-1] = c
}
a = 0
b = 0
for j = int32(len(buf)); j > 0; j-- {
c = buf[int32(len(buf))-j]
c -= 0x48
c ^= 0xFF
c = rol(c, int(j))
a = c
c ^= b
c = byte(int32(c) - j)
c = ror(c, 3)
b = a
buf[int32(len(buf))-j] = c
}
}
}
func mapleCrypt(buf []byte) {
// I have no idea what I'm doing
// this encryption shit was reversed from the game itself
var j int32
var a, c byte
for i := byte(0); i < 3; i++ {
a = 0
for j = int32(len(buf)); j > 0; j-- {
c = buf[int32(len(buf))-j]
c = rol(c, 3)
c = byte(int32(c) + j)
c ^= a
a = c
c = ror(a, int(j))
c ^= 0xFF
c += 0x48
buf[int32(len(buf))-j] = c
}
a = 0
for j = int32(len(buf)); j > 0; j-- {
c = buf[j-1]
c = rol(c, 4)
c = byte(int32(c) + j)
c ^= a
a = c
c ^= 0x13
c = ror(c, 3)
buf[j-1] = c
}
}
}
func (c *Crypt) aesCrypt(buf []byte) {
var pos, tpos, cbwrite, cb int32 = 0, 0, 0, int32(len(buf))
var first byte = 1
cb = int32(len(buf))
// I'm not 100% sure what this exactly does but apparently maple
// decrypts packets in blocks of 1460 bytes to work around
// packet limitations or something
for cb > pos {
tpos = blocksize - int32(first*4)
if cb > pos+tpos {
cbwrite = tpos
} else {
cbwrite = cb - pos
}
block, err := aes.NewCipher(aeskey[:])
if err != nil {
panic(err) // cbf to handle this unlikely error
}
stream := cipher.NewOFB(block, c.key[:])
stream.XORKeyStream(buf[pos:pos+cbwrite], buf[pos:pos+cbwrite])
pos += tpos
if first == 1 {
first = 0
}
}
}