Skip to content

Commit

Permalink
Fix: 进一步修正及完善空格带来的问题 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiphin committed Jul 13, 2020
1 parent dbaa834 commit 6380dfa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,17 @@ 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)
} else {
fmt.Println(result)
}
}
```


```
47 changes: 24 additions & 23 deletions wxbizdatacrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)

Expand All @@ -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
}

Expand All @@ -97,5 +98,5 @@ func PKCS7UnPadding(plantText []byte) []byte {
unPadding := int(plantText[length-1])
return plantText[:(length - unPadding)]
}
return plantText;
return plantText
}

0 comments on commit 6380dfa

Please sign in to comment.