-
Notifications
You must be signed in to change notification settings - Fork 7
/
crypto.go
81 lines (68 loc) · 2.07 KB
/
crypto.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
package utils
import (
"crypto/aes"
)
const eapiKey = "e82ckenh8dichen8"
const cacheKey = ")(13daqP@ssw0rd~"
const markerKey = "#14ljk_!\\]&0U<'("
func generateKey(key []byte) (genKey []byte) {
genKey = make([]byte, 16)
copy(genKey, key)
for i := 16; i < len(key); {
for j := 0; j < 16 && i < len(key); j, i = j+1, i+1 {
genKey[j] ^= key[i]
}
}
return genKey
}
func encryptECB(data, keyStr string) (encrypted []byte) {
origData := []byte(data)
key := []byte(keyStr)
cipher, _ := aes.NewCipher(generateKey(key))
length := (len(origData) + aes.BlockSize) / aes.BlockSize
plain := make([]byte, length*aes.BlockSize)
copy(plain, origData)
pad := byte(len(plain) - len(origData))
for i := len(origData); i < len(plain); i++ {
plain[i] = pad
}
encrypted = make([]byte, len(plain))
// 分组分块加密
for bs, be := 0, cipher.BlockSize(); bs <= len(origData); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
cipher.Encrypt(encrypted[bs:be], plain[bs:be])
}
return encrypted
}
func decryptECB(encrypted []byte, keyStr string) (decrypted []byte) {
key := []byte(keyStr)
cipher, _ := aes.NewCipher(generateKey(key))
decrypted = make([]byte, len(encrypted))
for bs, be := 0, cipher.BlockSize(); bs < len(encrypted); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
cipher.Decrypt(decrypted[bs:be], encrypted[bs:be])
}
trim := 0
if len(decrypted) > 0 {
trim = len(decrypted) - int(decrypted[len(decrypted)-1])
}
return decrypted[:trim]
}
// EapiEncrypt eapi 加密
func EapiEncrypt(data string) (encrypted []byte) {
return encryptECB(data, eapiKey)
}
// MarkerEncrypt 163 key 加密
func MarkerEncrypt(data string) (encrypted []byte) {
return encryptECB(data, markerKey)
}
// CacheKeyEncrypt cache_key 加密
func CacheKeyEncrypt(data string) (encrypted []byte) {
return encryptECB(data, cacheKey)
}
// EapiDecrypt eapi 解密
func EapiDecrypt(encrypted []byte) (decrypted []byte) {
return decryptECB(encrypted, eapiKey)
}
// MarkerDecrypt 163 key 解密
func MarkerDecrypt(encrypted []byte) (decrypted []byte) {
return decryptECB(encrypted, markerKey)
}