-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxb.go
129 lines (115 loc) · 2.86 KB
/
xb.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
package xb
import (
"crypto/md5"
"crypto/rc4"
"encoding/base64"
"encoding/binary"
"errors"
)
var b64 = base64.NewEncoding("Dkdpgh4ZKsQB80/Mfvw36XI1R25-WUAlEi7NLboqYTOPuzmFjJnryx9HVGcaStCe")
func Encode(params, postData string, userAgent string, timestamp uint32) string {
uaKey := []byte{0, 1, 14}
listKey := []byte{255}
fixed := uint32(3845494467)
md5Params := stdMd5Enc(stdMd5Enc([]byte(params)))
md5PostData := stdMd5Enc(stdMd5Enc([]byte(postData)))
md5UA := stdMd5Enc([]byte(base64.StdEncoding.EncodeToString(stdRc4Enc(uaKey, []byte(userAgent)))))
// 等待加密列表
list := make([]byte, 0, 19)
list = append(list, byte(64))
list = append(list, uaKey...)
list = append(list, md5Params[14:16]...)
list = append(list, md5PostData[14:16]...)
list = append(list, md5UA[14:16]...)
list = binary.BigEndian.AppendUint32(list, timestamp)
list = binary.BigEndian.AppendUint32(list, fixed)
list = append(list, xorKey(list))
enc := make([]byte, 0, 21)
enc = append(enc, byte(2))
enc = append(enc, listKey...)
enc = append(enc, stdRc4Enc(listKey, list)...)
return b64.EncodeToString(enc)
}
type Info struct {
Logo byte
Key []byte
ParamsHash []byte
DataHash []byte
UAHash []byte
Ts uint32
Fixed uint32
XorHash byte
}
func Decode(xb string) (*Info, error) {
dec, err := b64.DecodeString(xb)
if err != nil {
return nil, err
}
if len(dec) != 21 {
return nil, errors.New("xb no 21")
}
dec = dec[2:]
dec = stdRc4Enc([]byte{255}, dec)
return &Info{
Logo: dec[0],
Key: dec[1:4],
ParamsHash: dec[4:6],
DataHash: dec[6:8],
UAHash: dec[8:10],
Ts: binary.BigEndian.Uint32(dec[10:14]),
Fixed: binary.BigEndian.Uint32(dec[14:18]),
XorHash: dec[18],
}, nil
}
func stdMd5Enc(data []byte) []byte {
hash := md5.New()
hash.Write(data)
return hash.Sum(nil)
}
func stdRc4Enc(key []byte, plainText []byte) []byte {
cipher, err := rc4.NewCipher(key)
if err != nil {
return nil
}
res := make([]byte, len(plainText))
cipher.XORKeyStream(res, plainText)
return res
}
func rc4Enc(key []byte, plainText []byte) []byte {
sBox := make([]byte, 256)
for i := 0; i < 256; i++ {
sBox[i] = byte(i)
}
index := byte(0)
for i := 0; i < 256; i++ {
k := key[i%len(key)]
index = byte(uint32(index+sBox[i]+k) % 256)
temp := sBox[i]
sBox[i] = sBox[index]
sBox[index] = temp
}
index = byte(0)
i := 0
cipherText := make([]byte, 0)
for _, ch := range plainText {
i = (i + 1) % 256
index = byte(uint32(index+sBox[i]) % 256)
sBox[i], sBox[index] = sBox[index], sBox[i]
keyStream := sBox[byte(uint32(sBox[i]+sBox[index])%256)]
cipherText = append(cipherText, ch^keyStream)
}
return cipherText
}
func xorKey(list []byte) byte {
if len(list) == 0 {
return 0
}
if len(list) == 1 {
return list[0]
}
key := list[0]
for i := 1; i < len(list); i++ {
key ^= list[i]
}
return key
}