From 6380dfa724c5e55b38aec85869c858b4b428d63f Mon Sep 17 00:00:00 2001 From: Xiphin Date: Mon, 13 Jul 2020 20:09:15 +0800 Subject: [PATCH] =?UTF-8?q?Fix:=20=E8=BF=9B=E4=B8=80=E6=AD=A5=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=E5=8F=8A=E5=AE=8C=E5=96=84=E7=A9=BA=E6=A0=BC=E5=B8=A6?= =?UTF-8?q?=E6=9D=A5=E7=9A=84=E9=97=AE=E9=A2=98=20(#4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 +++----- wxbizdatacrypt.go | 47 ++++++++++++++++++++++++----------------------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 986eddf..2dc8094 100644 --- a/README.md +++ b/README.md @@ -32,12 +32,12 @@ import ( ) func main() { - appID := "wx4f4bc4dec97d474b" + appId := "wx4f4bc4dec97d474b" sessionKey := "tiihtNczf5v6AKRyjwEUhQ==" encryptedData := "CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZMQmRzooG2xrDcvSnxIMXFufNstNGTyaGS9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+3hVbJSRgv+4lGOETKUQz6OYStslQ142dNCuabNPGBzlooOmB231qMM85d2/fV6ChevvXvQP8Hkue1poOFtnEtpyxVLW1zAo6/1Xx1COxFvrc2d7UL/lmHInNlxuacJXwu0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn/Hz7saL8xz+W//FRAUid1OksQaQx4CMs8LOddcQhULW4ucetDf96JcR3g0gfRK4PC7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB6Y5anaHqZ9J6nKEBvB4DnNLIVWSgARns/8wR2SiRS7MNACwTyrGvt9ts8p12PKFdlqYTopNHR1Vf7XjfhQlVsAJdNiKdYmYVoKlaRv85IfVunYzO0IKXsyl7JCUjCpoG20f0a04COwfneQAGGwd5oa+T8yO5hzuyDb/XcxxmK01EpqOyuxINew==" iv := "r7BXXKkLb8qrSNn05n0qiA==" - pc := wxbizdatacrypt.WxBizDataCrypt{AppID: appID, SessionKey: sessionKey} + pc := wxbizdatacrypt.WxBizDataCrypt{AppId: appId, SessionKey: sessionKey} result, err := pc.Decrypt(encryptedData, iv, true) //第三个参数解释: 需要返回 JSON 数据类型时 使用 true, 需要返回 map 数据类型时 使用 false if err != nil { fmt.Println(err) @@ -45,6 +45,4 @@ func main() { fmt.Println(result) } } -``` - - +``` \ No newline at end of file diff --git a/wxbizdatacrypt.go b/wxbizdatacrypt.go index 87ab8ab..06c5061 100644 --- a/wxbizdatacrypt.go +++ b/wxbizdatacrypt.go @@ -12,17 +12,17 @@ import ( ) var errorCode = map[string]int{ - "IllegalAppID": -41000, - "IllegalAesKey": -41001, - "IllegalIV": -41002, - "IllegalBuffer": -41003, - "DecodeBase64Error": -41004, - "DecodeJsonError": -41005, + "illegalAppId": -41000, + "illegalAesKey": -41001, + "illegalIv": -41002, + "illegalBuffer": -41003, + "decodeBase64Error": -41004, + "decodeJsonError": -41005, } // WxBizDataCrypt represents an active WxBizDataCrypt object type WxBizDataCrypt struct { - AppID string + AppId string SessionKey string } @@ -39,34 +39,35 @@ func (e showError) Error() string { // If isJSON is true, Decrypt return JSON type. // If isJSON is false, Decrypt return map type. func (wxCrypt *WxBizDataCrypt) Decrypt(encryptedData string, iv string, isJSON bool) (interface{}, error) { - if len(wxCrypt.SessionKey) != 24 { - return nil, showError{errorCode["IllegalAesKey"], errors.New("sessionKey length is error")} + sessionKey := strings.Replace(strings.TrimSpace(wxCrypt.SessionKey), " ", "+", -1) + if len(sessionKey) != 24 { + return nil, showError{errorCode["illegalAesKey"], errors.New("sessionKey length is error")} } - aesKey, err := base64.StdEncoding.DecodeString(wxCrypt.SessionKey) + aesKey, err := base64.StdEncoding.DecodeString(sessionKey) if err != nil { - return nil, showError{errorCode["DecodeBase64Error"], err} + return nil, showError{errorCode["decodeBase64Error"], err} } - + iv = strings.Replace(strings.TrimSpace(iv), " ", "+", -1) if len(iv) != 24 { - return nil, showError{errorCode["IllegalIV"], errors.New("iv length is error")} + return nil, showError{errorCode["illegalIv"], errors.New("iv length is error")} } - aesIV, err := base64.StdEncoding.DecodeString(iv) + aesIv, err := base64.StdEncoding.DecodeString(iv) if err != nil { - return nil, showError{errorCode["DecodeBase64Error"], err} + return nil, showError{errorCode["decodeBase64Error"], err} } encryptedData = strings.Replace(strings.TrimSpace(encryptedData), " ", "+", -1) aesCipherText, err := base64.StdEncoding.DecodeString(encryptedData) if err != nil { - return nil, showError{errorCode["DecodeBase64Error"], err} + return nil, showError{errorCode["decodeBase64Error"], err} } aesPlantText := make([]byte, len(aesCipherText)) aesBlock, err := aes.NewCipher(aesKey) if err != nil { - return nil, showError{errorCode["IllegalBuffer"], err} + return nil, showError{errorCode["illegalBuffer"], err} } - mode := cipher.NewCBCDecrypter(aesBlock, aesIV) + mode := cipher.NewCBCDecrypter(aesBlock, aesIv) mode.CryptBlocks(aesPlantText, aesCipherText) aesPlantText = PKCS7UnPadding(aesPlantText) @@ -76,14 +77,14 @@ func (wxCrypt *WxBizDataCrypt) Decrypt(encryptedData string, iv string, isJSON b aesPlantText = []byte(re.ReplaceAllString(string(aesPlantText), "$1")) err = json.Unmarshal(aesPlantText, &decrypted) if err != nil { - return nil, showError{errorCode["DecodeJsonError"], err} + return nil, showError{errorCode["decodeJsonError"], err} } - if decrypted["watermark"].(map[string]interface{})["appid"] != wxCrypt.AppID { - return nil, showError{errorCode["IllegalAppID"], errors.New("appID is not match")} + if decrypted["watermark"].(map[string]interface{})["appid"] != wxCrypt.AppId { + return nil, showError{errorCode["illegalAppId"], errors.New("appId is not match")} } - if isJSON == true { + if isJSON { return string(aesPlantText), nil } @@ -97,5 +98,5 @@ func PKCS7UnPadding(plantText []byte) []byte { unPadding := int(plantText[length-1]) return plantText[:(length - unPadding)] } - return plantText; + return plantText }