Quellcode durchsuchen

special depart add 29

abiao vor 4 Jahren
Ursprung
Commit
0c14abf0b9

+ 1 - 1
go/gopath/src/fohow.com/apps/controllers/permit_controller/permit_controller.go

@@ -92,7 +92,7 @@ func (self *PermitController) XcxAuthorize() {
 		self.ReturnError(403, apps.UserNeedLogin, "", nil)
 	}
 
-	pc := helpers.WxBizDataCrypt{AppID: beego.AppConfig.String("WxFohowXcxAppId"), SessionKey: sessionKey}
+	pc := helpers.WxBizDataCrypt{AppId: beego.AppConfig.String("WxFohowXcxAppId"), SessionKey: sessionKey}
 	beego.BeeLogger.Warn("EncryptedData:%s", info.EncryptedData)
 	beego.BeeLogger.Warn("Iv:%s", info.Iv)
 	result, err := pc.Decrypt(info.EncryptedData, info.Iv, true) //第三个参数解释: 需要返回 JSON 数据类型时 使用 true, 需要返回 map 数据类型时 使用 false

+ 33 - 27
go/gopath/src/fohow.com/apps/helpers/wxbizdatacrypt.go

@@ -7,19 +7,22 @@ import (
 	"encoding/json"
 	"errors"
 	"fmt"
+	"regexp"
 	"strings"
 )
 
 var errorCode = map[string]int{
-	"IllegalAesKey":     -41001,
-	"IllegalIv":         -41002,
-	"IllegalBuffer":     -41003,
-	"DecodeBase64Error": -41004,
+	"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
 }
 
@@ -36,49 +39,52 @@ 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)
 
 	var decrypted map[string]interface{}
-	aesPlantText = []byte(strings.Replace(string(aesPlantText), "\a", "", -1))
-	err = json.Unmarshal([]byte(aesPlantText), &decrypted)
+
+	re := regexp.MustCompile(`[^\{]*(\{.*\})[^\}]*`)
+	aesPlantText = []byte(re.ReplaceAllString(string(aesPlantText), "$1"))
+	err = json.Unmarshal(aesPlantText, &decrypted)
 	if err != nil {
-		return nil, showError{errorCode["IllegalBuffer"], err}
+		return nil, showError{errorCode["decodeJsonError"], err}
 	}
 
-	if decrypted["watermark"].(map[string]interface{})["appid"] != wxCrypt.AppID {
-		return nil, showError{errorCode["IllegalBuffer"], 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
 	}
 
@@ -88,9 +94,9 @@ func (wxCrypt *WxBizDataCrypt) Decrypt(encryptedData string, iv string, isJSON b
 // PKCS7UnPadding return unpadding []Byte plantText
 func PKCS7UnPadding(plantText []byte) []byte {
 	length := len(plantText)
-	unPadding := int(plantText[length-1])
-	if unPadding < 1 || unPadding > 32 {
-		unPadding = 0
+	if length > 0 {
+		unPadding := int(plantText[length-1])
+		return plantText[:(length - unPadding)]
 	}
-	return plantText[:(length - unPadding)]
+	return plantText
 }