-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathencryption.go
69 lines (58 loc) · 1.38 KB
/
encryption.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
package wz
import (
//"bytes"
"strings"
)
type Encryption struct {
encryptedStrings []string
aesIV []byte
aesKey []byte
xorKey []byte
}
const VariantGMS = byte(1)
const VariantSEA = byte(2)
func NewEncryption(variant byte) *Encryption {
m := new(Encryption)
m.setWZVariant(variant)
return m
}
func (m *Encryption) IsEncrypted(uol string) bool {
for _, str := range m.encryptedStrings {
if strings.Index(uol, str) == 0 {
return true
}
}
return false
}
func (m *Encryption) TransformBuffer(buffer []byte) {
m.tryExpandXorKey(len(buffer))
for i := 0; i < len(buffer); i++ {
buffer[i] ^= m.xorKey[i]
}
}
func (m *Encryption) setWZVariant(variant byte) {
switch variant {
case VariantGMS:
m.aesIV = GMS_WZ_IV
m.aesKey = WZ_AES_KEY
break
case VariantSEA:
m.aesIV = SEA_WZ_IV
m.aesKey = WZ_AES_KEY
break
default:
// When the WZ key is set to this, do not expect good results
// There has been no version that used this key yet.
m.aesIV = DEFAULT_WZ_IV
m.aesKey = WZ_AES_KEY
}
m.xorKey = []byte{}
m.tryExpandXorKey(400) // Pre-built a WZ key for 400 characters. Should be enough for most of the simple data/strings.
}
func (m *Encryption) tryExpandXorKey(length int) {
// Check if we already have enough data
if len(m.xorKey) < length {
return
}
m.aesIV, m.xorKey = expandXorKey(m.aesIV, m.aesKey, m.xorKey, length)
}