Quellcode durchsuchen

special depart add 29

abiao vor 4 Jahren
Ursprung
Commit
b1f1521775

+ 2 - 2
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
@@ -226,7 +226,7 @@ func (self *PermitController) SaveWxuserInfo() {
 		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

+ 2 - 2
go/gopath/src/fohow.com/apps/controllers/user_controller/binding_wx_phone_controller.go

@@ -64,7 +64,7 @@ func (self *UserController) BindingWxPhone() {
 	//purePhoneNumber	String	没有区号的手机号
 	//countryCode	String	区
 
-	pc := helpers.WxBizDataCrypt{AppId: beego.AppConfig.String("WxFohowXcxAppId"), SessionKey: sessionKey}
+	pc := helpers.WxBizDataCrypt{AppID: beego.AppConfig.String("WxFohowXcxAppId"), SessionKey: sessionKey}
 	result, err := pc.Decrypt(info.EncryptedData, info.Iv, true) //第三个参数解释: 需要返回 JSON 数据类型时 使用 true, 需要返回 map 数据类型时 使用 false
 	if err != nil {
 		beego.BeeLogger.Error("xcx BindingWxPhone descrypt failed, err:%s", err)
@@ -152,7 +152,7 @@ func (self *UserController) BindingWxPhoneNew() {
 	//purePhoneNumber	String	没有区号的手机号
 	//countryCode	String	区
 
-	pc := helpers.WxBizDataCrypt{AppId: beego.AppConfig.String("WxFohowXcxAppId"), SessionKey: sessionKey}
+	pc := helpers.WxBizDataCrypt{AppID: beego.AppConfig.String("WxFohowXcxAppId"), SessionKey: sessionKey}
 	result, err := pc.Decrypt(info.EncryptedData, info.Iv, true) //第三个参数解释: 需要返回 JSON 数据类型时 使用 true, 需要返回 map 数据类型时 使用 false
 	if err != nil {
 		beego.BeeLogger.Error("xcx BindingWxPhone descrypt failed, err:%s", err)

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

@@ -157,7 +157,7 @@ func (self *UserController) UpdateWxUserInfo() {
 
 		if sessionKey != "" {
 			userInfoWithUnionId := &UserInfoWithUnionId{}
-			pc := helpers.WxBizDataCrypt{AppId: beego.AppConfig.String("WxFohowXcxAppId"), SessionKey: sessionKey}
+			pc := helpers.WxBizDataCrypt{AppID: beego.AppConfig.String("WxFohowXcxAppId"), SessionKey: sessionKey}
 			result, err := pc.Decrypt(info.EncryptedData, info.Iv, true) //第三个参数解释: 需要返回 JSON 数据类型时 使用 true, 需要返回 map 数据类型时 使用 false
 			if err != nil {
 				fmt.Println(err)

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

@@ -7,22 +7,19 @@ import (
 	"encoding/json"
 	"errors"
 	"fmt"
-	"regexp"
 	"strings"
 )
 
 var errorCode = map[string]int{
-	"illegalAppId":      -41000,
-	"illegalAesKey":     -41001,
-	"illegalIv":         -41002,
-	"illegalBuffer":     -41003,
-	"decodeBase64Error": -41004,
-	"decodeJsonError":   -41005,
+	"IllegalAesKey":     -41001,
+	"IllegalIv":         -41002,
+	"IllegalBuffer":     -41003,
+	"DecodeBase64Error": -41004,
 }
 
 // WxBizDataCrypt represents an active WxBizDataCrypt object
 type WxBizDataCrypt struct {
-	AppId      string
+	AppID      string
 	SessionKey string
 }
 
@@ -39,52 +36,49 @@ 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) {
-	sessionKey := strings.Replace(strings.TrimSpace(wxCrypt.SessionKey), " ", "+", -1)
-	if len(sessionKey) != 24 {
-		return nil, showError{errorCode["illegalAesKey"], errors.New("sessionKey length is error")}
+	if len(wxCrypt.SessionKey) != 24 {
+		return nil, showError{errorCode["IllegalAesKey"], errors.New("sessionKey length is error")}
 	}
-	aesKey, err := base64.StdEncoding.DecodeString(sessionKey)
+	aesKey, err := base64.StdEncoding.DecodeString(wxCrypt.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{}
-
-	re := regexp.MustCompile(`[^\{]*(\{.*\})[^\}]*`)
-	aesPlantText = []byte(re.ReplaceAllString(string(aesPlantText), "$1"))
-	err = json.Unmarshal(aesPlantText, &decrypted)
+	aesPlantText = []byte(strings.Replace(string(aesPlantText), "\a", "", -1))
+	err = json.Unmarshal([]byte(aesPlantText), &decrypted)
 	if err != nil {
-		return nil, showError{errorCode["decodeJsonError"], err}
+		return nil, showError{errorCode["IllegalBuffer"], 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["IllegalBuffer"], errors.New("appId is not match")}
 	}
 
-	if isJSON {
+	if isJSON == true {
 		return string(aesPlantText), nil
 	}
 
@@ -94,9 +88,9 @@ func (wxCrypt *WxBizDataCrypt) Decrypt(encryptedData string, iv string, isJSON b
 // PKCS7UnPadding return unpadding []Byte plantText
 func PKCS7UnPadding(plantText []byte) []byte {
 	length := len(plantText)
-	if length > 0 {
-		unPadding := int(plantText[length-1])
-		return plantText[:(length - unPadding)]
+	unPadding := int(plantText[length-1])
+	if unPadding < 1 || unPadding > 32 {
+		unPadding = 0
 	}
-	return plantText
+	return plantText[:(length - unPadding)]
 }