-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypt.go
97 lines (90 loc) · 1.55 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
package xwis
const (
preHeaderLength = 4
headerLength = 8
fullHeaderLength = preHeaderLength + headerLength
)
func encryptTo(out, data []byte) []byte {
ind := 0
cnt := 0
loc := 0
loc2 := 0
acc := 0
for i := 0; i <= len(data)-10; i++ {
for j := 0; j <= 7; j++ {
if loc == 8 {
ind++
loc = 0
}
if loc2 == 7 {
acc ^= 1 << 7
out[cnt] = byte(acc)
cnt++
acc = 0
loc2 = 0
}
var v6 byte
if ind < len(data) {
v6 = data[ind]
}
v5 := 1 << loc
v4 := int(v6) & v5
v3 := v4 >> loc
acc ^= (v3 << loc2) & 0xFF
loc++
loc2++
}
}
return out
}
func decrypt(data []byte) {
ind := 0
cnt := 0
loc := 0
for i := 0; i <= len(data)-10; i++ {
var acc byte
for j := 0; j <= 7; j++ {
if cnt == 7 {
cnt = 0
loc++
}
if loc == 8 {
data[ind] = 0
ind++
loc = 0
}
v6 := byte(0)
if ind < len(data) {
v6 = data[ind]
}
v5 := 1 << loc
v4 := int(v6) & v5
v3 := v4 >> loc
acc ^= byte((v3 << j) & 0xFF)
loc++
cnt++
}
data[i] = acc
}
}
func decryptAndDecode(data []byte) (*GameInfo, error) {
data = data[fullHeaderLength:]
decrypt(data)
var g GameInfo
err := g.UnmarshalBinary(data)
if err != nil {
return nil, err
}
return &g, nil
}
var header = []byte{':', 'G', '1', 'P', '3', 0x9a, 0x03, 0x01}
func encodeAndEncrypt(g *GameInfo) ([]byte, error) {
gdata, err := g.MarshalBinary()
if err != nil {
return nil, err
}
data := make([]byte, headerLength+len(gdata))
copy(data, header)
encryptTo(data[headerLength:], gdata)
return data, nil
}