扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
接口如果涉及敏感数据(如wx.getUserInfo当中的 openId 和 unionId),接口的明文内容将不包含这些敏感数据。开发者如需要获取敏感数据,需要对接口返回的加密数据(encryptedData) 进行对称解密。 解密算法如下:
创新互联是一家专业提供平安企业网站建设,专注与做网站、成都网站建设、H5场景定制、小程序制作等业务。10年已为平安众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。
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)
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流