-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcrypt.go
278 lines (245 loc) · 7.9 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package wechataes
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/xml"
"fmt"
mathRand "math/rand"
"time"
)
type WechatCryptor struct {
appId string
token string
aesKey []byte
}
// 构造函数
// appId 公众平台appid
// token 公众平台上,开发者设置的token
// aesKey 公众平台上,开发者设置的EncodingAESKey
func NewWechatCryptor(appId, token, encodingAesKey string) (*WechatCryptor, error) {
cryptor := new(WechatCryptor)
if 43 != len(encodingAesKey) {
return cryptor, &WechatCryptorError{Code: IllegalAesKey}
}
cryptor.appId = appId
cryptor.token = token
cryptor.aesKey, _ = base64.StdEncoding.DecodeString(encodingAesKey + "=")
return cryptor, nil
}
func (cryptor *WechatCryptor) String() string {
return "AppId(" + cryptor.appId +
") Token(" + cryptor.token +
") AES_KEY(" + base64.StdEncoding.EncodeToString(cryptor.aesKey) + ")"
}
// 随机生成16位字符串
func WechatCryptorRandomStr() string {
r := mathRand.New(mathRand.NewSource(time.Now().UnixNano()))
bytes := []byte("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
var result []byte
for i := 0; i < 16; i++ {
result = append(result, bytes[r.Intn(len(bytes))])
}
return string(result)
}
const WechatCryptorEncryptMsgFormat = `
<xml>
<Encrypt><![CDATA[%s]]></Encrypt>
<MsgSignature><![CDATA[%s]]></MsgSignature>
<TimeStamp>%s</TimeStamp>
<Nonce><![CDATA[%s]]></Nonce>
</xml>
`
func (cryptor *WechatCryptor) EncryptMsg(msg, timeStamp, nonce string) (string, error) {
encrypt, sign, timeStamp, nonce, err := cryptor.EncryptMsgContent(msg, timeStamp, nonce)
if nil != err {
return "", err
}
return fmt.Sprintf(WechatCryptorEncryptMsgFormat, encrypt, sign, timeStamp, nonce), nil
}
type WechatCryptorPostBody struct {
XMLName xml.Name `xml:"xml"`
ToUserName string `xml:"ToUserName"`
AppId string `xml:"AppId"`
Encrypt string `xml:"Encrypt"`
}
func (cryptor *WechatCryptor) DecryptMsg(msgSign, timeStamp, nonce, postData string) (string, error) {
postBody := WechatCryptorPostBody{}
err := xml.Unmarshal([]byte(postData), &postBody)
if nil != err || 0 == len(postBody.Encrypt) {
return "", &WechatCryptorError{Code: ParseXmlError}
}
return cryptor.DecryptMsgContent(msgSign, timeStamp, nonce, postBody.Encrypt)
}
func (cryptor *WechatCryptor) EncryptMsgContent(msg, timeStamp, nonce string) (string, string, string, string, error) {
encrypt, err := cryptor.Encrypt(WechatCryptorRandomStr(), msg)
if nil != err {
return "", "", "", "", err
}
if 0 == len(timeStamp) {
timeStamp = fmt.Sprint(time.Now().Unix())
}
sign := SHA1(cryptor.token, timeStamp, nonce, encrypt)
return encrypt, sign, timeStamp, nonce, nil
}
func (cryptor *WechatCryptor) DecryptMsgContent(msgSign, timeStamp, nonce, encrypt string) (string, error) {
sign := SHA1(cryptor.token, timeStamp, nonce, encrypt)
if msgSign != sign {
return "", &WechatCryptorError{Code: ValidateSignatureError}
}
return cryptor.Decrypt(encrypt)
}
// 对明文进行加密
func (cryptor *WechatCryptor) Encrypt(randomStr, text string) (string, error) {
randomBytes := []byte(randomStr)
textBytes := []byte(text)
networkBytes := wechatCryptorBuildNetworkBytesOrder(len(textBytes))
appIdBytes := []byte(cryptor.appId)
var unencrypted []byte
unencrypted = append(unencrypted, randomBytes...)
unencrypted = append(unencrypted, networkBytes...)
unencrypted = append(unencrypted, textBytes...)
unencrypted = append(unencrypted, appIdBytes...)
encrypted, err := wechatCryptorEncrypt(unencrypted, cryptor.aesKey)
if nil != err {
return "", &WechatCryptorError{Code: EncryptAESError}
}
return encrypted, nil
}
// 对密文进行解密
func (cryptor *WechatCryptor) Decrypt(text string) (string, error) {
original, err := wechatCryptorDecrypt(text, cryptor.aesKey)
if nil != err {
return "", &WechatCryptorError{Code: DecryptAESError}
}
networkBytes := original[16:20]
textLen := wechatCryptorRecoverNetworkBytesOrder(networkBytes)
textBytes := original[20 : 20+textLen]
appIdBytes := original[20+textLen:]
if cryptor.appId != string(appIdBytes) {
return "", &WechatCryptorError{Code: ValidateAppidError}
}
return string(textBytes), nil
}
func wechatCryptorEncrypt(rawData, key []byte) (string, error) {
data, err := wechatCryptorAesCBCEncrypt(rawData, key)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(data), nil
}
func wechatCryptorDecrypt(rawData string, key []byte) ([]byte, error) {
data, err := base64.StdEncoding.DecodeString(rawData)
if err != nil {
return nil, err
}
dnData, err := wechatCryptorAesCBCDecrypt(data, key)
if err != nil {
return nil, err
}
return dnData, nil
}
// aes加密,填充秘钥key的16位
func wechatCryptorAesCBCEncrypt(rawData, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
// 填充原文
rawData = wechatCryptorPKCS7Padding(rawData)
cipherText := make([]byte, len(rawData))
// 初始向量IV
iv := key[:16]
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(cipherText, rawData)
return cipherText, nil
}
// aes解密
func wechatCryptorAesCBCDecrypt(encryptData, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
// 初始向量IV
iv := key[:16]
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(encryptData, encryptData)
// 解填充
encryptData = wechatCryptorPKCS7UnPadding(encryptData)
return encryptData, nil
}
func wechatCryptorPKCS7Padding(ciphertext []byte) []byte {
amountToPad := 32 - (len(ciphertext) % 32)
if 0 == amountToPad {
amountToPad = 32
}
padChr := (byte)(amountToPad & 0xFF)
result := make([]byte, len(ciphertext), len(ciphertext)+amountToPad)
copy(result, ciphertext)
for i := 0; i < amountToPad; i++ {
result = append(result, padChr)
}
return result
}
func wechatCryptorPKCS7UnPadding(origData []byte) []byte {
pad := (int)(origData[len(origData)-1])
if pad < 1 || pad > 32 {
pad = 0
}
return origData[:len(origData)-pad]
}
// 生成4个字节的网络字节序
func wechatCryptorBuildNetworkBytesOrder(number int) []byte {
return []byte{
(byte)(number >> 24 & 0xFF),
(byte)(number >> 16 & 0xF),
(byte)(number >> 8 & 0xFF),
(byte)(number & 0xFF),
}
}
// 还原4个字节的网络字节序
func wechatCryptorRecoverNetworkBytesOrder(orderBytes []byte) int {
var number = 0
for i := 0; i < 4; i++ {
number <<= 8
number |= (int)(orderBytes[i] & 0xff)
}
return number
}
/**
异常的错误码和具体的错误信息
*/
type WechatCryptorError struct {
Code int
}
const OK int = 0
const ValidateSignatureError int = -40001
const ParseXmlError int = -40002
const ComputeSignatureError int = -40003
const IllegalAesKey int = -40004
const ValidateAppidError int = -40005
const EncryptAESError int = -40006
const DecryptAESError int = -40007
const IllegalBuffer int = -40008
func (e *WechatCryptorError) Error() string {
switch e.Code {
case ValidateSignatureError:
return "签名验证错误"
case ParseXmlError:
return "xml解析失败"
case ComputeSignatureError:
return "sha加密生成签名失败"
case IllegalAesKey:
return "SymmetricKey非法"
case ValidateAppidError:
return "appid校验失败"
case EncryptAESError:
return "aes加密失败"
case DecryptAESError:
return "aes解密失败"
case IllegalBuffer:
return "解密后得到的buffer非法"
default:
return ""
}
}