您好,登錄后才能下訂單哦!
接口如果涉及敏感數(shù)據(jù)(如wx.getUserInfo當中的 openId 和 unionId),接口的明文內容將不包含這些敏感數(shù)據(jù)。開發(fā)者如需要獲取敏感數(shù)據(jù),需要對接口返回的加密數(shù)據(jù)(encryptedData) 進行對稱解密。 解密算法如下:
package wechat
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/json"
"errors"
)
var (
ErrAppIDNotMatch = errors.New("app id not match")
ErrInvalidBlockSize = errors.New("invalid block size")
ErrInvalidPKCS7Data = errors.New("invalid PKCS7 data")
ErrInvalidPKCS7Padding = errors.New("invalid padding on input")
)
type WxUserInfo struct {
OpenID string `json:"openId"`
UnionID string `json:"unionId"`
NickName string `json:"nickName"`
Gender int `json:"gender"`
City string `json:"city"`
Province string `json:"province"`
Country string `json:"country"`
AvatarURL string `json:"avatarUrl"`
Language string `json:"language"`
Watermark struct {
Timestamp int64 `json:"timestamp"`
AppID string `json:"appid"`
} `json:"watermark"`
}
type WXUserDataCrypt struct {
appID, sessionKey string
}
func NewWXUserDataCrypt(appID, sessionKey string) *WXUserDataCrypt {
return &WXUserDataCrypt{
appID: appID,
sessionKey: sessionKey,
}
}
// pkcs7Unpad returns slice of the original data without padding
func pkcs7Unpad(data []byte, blockSize int) ([]byte, error) {
if blockSize <= 0 {
return nil, ErrInvalidBlockSize
}
if len(data)%blockSize != 0 || len(data) == 0 {
return nil, ErrInvalidPKCS7Data
}
c := data[len(data)-1]
n := int(c)
if n == 0 || n > len(data) {
return nil, ErrInvalidPKCS7Padding
}
for i := 0; i < n; i++ {
if data[len(data)-n+i] != c {
return nil, ErrInvalidPKCS7Padding
}
}
return data[:len(data)-n], nil
}
func (w *WXUserDataCrypt) Decrypt(encryptedData, iv string) (*WxUserInfo, error) {
aesKey, err := base64.StdEncoding.DecodeString(w.sessionKey)
if err != nil {
return nil, err
}
cipherText, err := base64.StdEncoding.DecodeString(encryptedData)
if err != nil {
return nil, err
}
ivBytes, err := base64.StdEncoding.DecodeString(iv)
if err != nil {
return nil, err
}
block, err := aes.NewCipher(aesKey)
if err != nil {
return nil, err
}
mode := cipher.NewCBCDecrypter(block, ivBytes)
mode.CryptBlocks(cipherText, cipherText)
cipherText, err = pkcs7Unpad(cipherText, block.BlockSize())
if err != nil {
return nil, err
}
var userInfo WxUserInfo
err = json.Unmarshal(cipherText, &userInfo)
if err != nil {
return nil, err
}
if userInfo.Watermark.AppID != w.appID {
return nil, ErrAppIDNotMatch
}
return &userInfo, nil
}
package main
import (
"fmt"
"test/wxbizdatacrypt"
)
func main() {
appID := "wx33f640141e02040e"
sessionKey := `SE/BLocg+sMlvcKmxm8vQA==`
encryptedData :="7SfFtStsHqKZYhbIkke3BH2bCRzGD15T0jEiUtuksrl9lDeHm9LsPmswJymBXuinPCiXkZhd/uq7s7pACTvbWuvvoKEwz5fAJ6Vr9bTx79XVxiIN4r+Fwm6QHO9DjPkFrxTGAZvMYLyH6IOyOV/nmmlMoBM3G4peSnBi1qCYukwlyCMNp67lb93wSiPAoI7eRhYYw8ayPTsZ/MAJ9CBBUiCwM5aFOUWrMKNTikeq7YVjNCv7KCz0LJTrMKda0YMS0J/034L8x9vJ1OnIkxlWVMQEy/f55IfWVHI1I1fSKd5azzyVKXCbWDpU0PLJnU8XM/l4L7ZUlDOcRMR5KQVGhB9rIjVkykdXUPQK87v8lpnitslK06XceOJqDjK6mRkhJWOYpFUozZa6idFV6xmLZX8bkBsLxczzp1h/satEH7rIz3nKbxd3O1c+3dI2soSt8qFtaumcGdwhenTm+at0gxccAp8JD8PZiB5ZDLTofZIQ4RmI004SIExYUDZUje9mZO+3aC8McVwzrEyK7NKD/NZ5/dYPgDRwzBl1Vm99niY="
iv := "z3tGYrgMcbLzd0qXqZuduQ=="
pc := wxbizdatacrypt.NewWXBizDataCrypt(appID, sessionKey)
userInfo, err := pc.Decrypt(encryptedData, iv)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Printf("userData:%+v", userInfo)
}
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。