-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclient.go
49 lines (36 loc) · 1.01 KB
/
client.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
package pangcrypt
import "encoding/binary"
// ClientDecrypt decrypts PangYa client packets.
func ClientDecrypt(data []byte, key byte) ([]byte, error) {
if key >= 0x10 {
return nil, KeyOutOfRangeError{key}
}
if len(data) < 5 {
return nil, BufferTooSmallError{len(data), 5}
}
index := int(key)<<8 + int(data[0])
buffer := append(data[:0:0], data...)
buffer[4] = cryptTable[1][index]
for i := 8; i < len(data); i++ {
buffer[i] ^= buffer[i-4]
}
return buffer[5:], nil
}
// ClientEncrypt encrypts PangYa client packets.
func ClientEncrypt(data []byte, key byte, salt byte) ([]byte, error) {
if key >= 0x10 {
return nil, KeyOutOfRangeError{key}
}
buffer := make([]byte, len(data)+5)
copy(buffer[5:], data)
index := int(key)<<8 + int(salt)
buffer[0] = salt
buffer[3] = 0
buffer[4] = cryptTable[1][index]
binary.LittleEndian.PutUint16(buffer[1:3], uint16(len(buffer)-4))
for i := len(buffer) - 1; i >= 8; i-- {
buffer[i] ^= buffer[i-4]
}
buffer[4] ^= cryptTable[0][index]
return buffer, nil
}