forked from Ecalose/hbookerlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.go
186 lines (155 loc) · 4.69 KB
/
decode.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package hbookerLib
import (
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"github.com/AlexiaVeronica/hbookerLib/hbookermodel"
"log"
"os"
)
func randString() string {
bytes := make([]byte, 16)
_, err := rand.Read(bytes)
if err != nil {
log.Fatal(err)
}
return hex.EncodeToString(bytes)
}
// SHA256 sha256 编码
func enSha256(data string) []byte {
ret := sha256.Sum256([]byte(data))
return ret[:]
}
func aesDecrypt(contentText string, encryptKey string) ([]byte, error) {
decoded, err := base64.StdEncoding.DecodeString(contentText)
if err != nil {
return nil, err
}
block, ok := aes.NewCipher(enSha256(encryptKey)[:32])
if ok != nil {
return nil, ok
}
iv, err := base64.StdEncoding.DecodeString(ivBase64)
if err != nil {
return nil, err
}
blockModel, plainText := cipher.NewCBCDecrypter(block, iv), make([]byte, len(decoded))
blockModel.CryptBlocks(plainText, decoded)
return plainText[:(len(plainText) - int(plainText[len(plainText)-1]))], nil
}
type RSAUtil struct{}
// GetPublicKeyRefWithKey 从 base64 编码的字符串中获取公钥
func (rsaUtil *RSAUtil) GetPublicKeyRefWithKey(key string) (*rsa.PublicKey, error) {
keyData, err := base64.StdEncoding.DecodeString(key)
if err != nil {
return nil, fmt.Errorf("failed to decode base64 key: %v", err)
}
pub, err := x509.ParsePKIXPublicKey(keyData)
if err != nil {
return nil, fmt.Errorf("failed to parse public key: %v", err)
}
publicKey, ok := pub.(*rsa.PublicKey)
if !ok {
return nil, errors.New("not an RSA public key")
}
return publicKey, nil
}
// Encrypt 使用公钥文件路径加密字符串
func (rsaUtil *RSAUtil) Encrypt(encrypt, keyFilePath string) (string, error) {
var publicKey *rsa.PublicKey
var err error
if keyFilePath == "" {
return "", errors.New("key file path is empty")
}
if len(encrypt) == 0 {
return "", errors.New("encrypt string is empty")
}
if keyFilePath[len(keyFilePath)-4:] == ".pem" {
publicKey, err = rsaUtil.ReadPubKeyFromPem(keyFilePath)
} else {
publicKey, err = rsaUtil.GetPublicKeyRefWithKey(keyFilePath)
}
if err != nil {
return "", fmt.Errorf("failed to get public key: %v", err)
}
encryptedMessage, err := rsaUtil.EncryptString(encrypt, publicKey)
if err != nil {
return "", fmt.Errorf("failed to encrypt string: %v", err)
}
return encryptedMessage, nil
}
// EncryptString 使用公钥加密字符串
func (rsaUtil *RSAUtil) EncryptString(str string, publicKey *rsa.PublicKey) (string, error) {
encryptedData, err := rsaUtil.EncryptData([]byte(str), publicKey)
if err != nil {
return "", fmt.Errorf("failed to encrypt data: %v", err)
}
return base64.StdEncoding.EncodeToString(encryptedData), nil
}
// EncryptData 使用公钥加密数据
func (rsaUtil *RSAUtil) EncryptData(data []byte, publicKey *rsa.PublicKey) ([]byte, error) {
keySize := publicKey.Size()
maxChunkSize := keySize - 11 // PKCS#1 v1.5 填充
var encryptedData []byte
for len(data) > 0 {
chunkSize := len(data)
if chunkSize > maxChunkSize {
chunkSize = maxChunkSize
}
chunk, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, data[:chunkSize])
if err != nil {
return nil, fmt.Errorf("failed to encrypt data: %v", err)
}
encryptedData = append(encryptedData, chunk...)
data = data[chunkSize:]
}
return encryptedData, nil
}
// ReadPubKeyFromPem 从 PEM 文件中读取公钥
func (rsaUtil *RSAUtil) ReadPubKeyFromPem(filePath string) (*rsa.PublicKey, error) {
pemData, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("failed to read PEM file: %v", err)
}
block, _ := pem.Decode(pemData)
if block == nil || block.Type != "PUBLIC KEY" {
return nil, errors.New("failed to decode PEM block containing public key")
}
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse public key: %v", err)
}
publicKey, ok := pub.(*rsa.PublicKey)
if !ok {
return nil, errors.New("not an RSA public key")
}
return publicKey, nil
}
func (rsaUtil *RSAUtil) GetAuthenticate(authenticate *hbookermodel.Authenticate) {
m, _ := json.Marshal(authenticate)
rasUtil := &RSAUtil{}
publicKey, err := rasUtil.Encrypt(string(m), publicIOSKey)
if err != nil {
log.Panicln("Error encrypting Authenticate:", err)
} else {
authenticate.SetP(publicKey)
}
}
func SetNewVersionP(authenticate *hbookermodel.Authenticate) {
authenticate.SetRandStr(randString())
authenticate.SetSignatures(hmacKey + androidSignaturesKey)
h := hmac.New(sha256.New, []byte(hmacKey))
h.Write([]byte(authenticate.GetQueryParams()))
p := h.Sum(nil)
authenticate.SetP(base64.StdEncoding.EncodeToString(p))
}