收藏
回答

go小程序登录解密encryptedData报错 ,但是写死的官方参数又可以解密

package main

import(
"fmt"
  "crypto/aes"
  "crypto/cipher"
  "encoding/base64"
  "errors"
  "log"
  "encoding/json"
)



type  WXBizDataCrypt struct {
appId string
sessionKey string
}


type WaterMark struct {
TimeStamp string `json:"timestamp"`
  AppID string `json:"appid"`
}


type EncryptedDataUserInfo struct {
OpenID string `json:"openId"`
  NickName string `json:"nickName"`
  Gender int `json:"gender"`
  Language string `json:"language"`
  City string `json:"city"`
  Province string `json:"province"`
  Country string `json:"country"`
  AvatarURL string `json:"avatarUrl"`
  UnionID string `json:"unionId"`
  Watermark struct {
Timestamp int `json:"timestamp"`
     Appid string `json:"appid"`
  } `json:"watermark"`
}

func checkError(err error, id string) {
if err!= nil {
log.Fatal("Wrong position is " + id, err)
}
}


func (wx *WXBizDataCrypt) decrypt(encryptedData string, iv string) string {

// The sessionkey & encryptedData & iv are base64 encode
  // So we need to use base64 to decode it first
  sessionKey,err := base64.StdEncoding.DecodeString(wx.sessionKey)
checkError(err, "1")
encryptedDataByte,err := base64.StdEncoding.DecodeString(encryptedData)
checkError(err, "2")
ivByte,err := base64.StdEncoding.DecodeString(iv)
checkError(err, "3")

block,err := aes.NewCipher(sessionKey)
checkError(err, "4")

if len(encryptedDataByte) < aes.BlockSize {
err = errors.New("Ciphertext block size is too short!")
checkError(err, "5")
}

//The wechat biz data use mode CBC, which use block
  mode := cipher.NewCBCDecrypter(block, ivByte)
mode.CryptBlocks(encryptedDataByte, encryptedDataByte)

var dat EncryptedDataUserInfo
fmt.Println(string(encryptedDataByte))
json.Unmarshal(encryptedDataByte, &dat)
//fmt.Println(dat.UnionID)
   return dat.UnionID
/*openId,err := jsonparser.GetString(encryptedDataByte,"openId")
  checkError(err, "6")
  return openId*/
}


func main(){
ha := WXBizDataCrypt{
appId: "wx4f4bc4dec97d474b",
sessionKey : "tiihtNczf5v6AKRyjwEUhQ=="}
encryptedData := "CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZMQmRzooG2xrDcvSnxIMXFufNstNGTyaGS9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+3hVbJSRgv+4lGOETKUQz6OYStslQ142dNCuabNPGBzlooOmB231qMM85d2/fV6ChevvXvQP8Hkue1poOFtnEtpyxVLW1zAo6/1Xx1COxFvrc2d7UL/lmHInNlxuacJXwu0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn/Hz7saL8xz+W//FRAUid1OksQaQx4CMs8LOddcQhULW4ucetDf96JcR3g0gfRK4PC7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB6Y5anaHqZ9J6nKEBvB4DnNLIVWSgARns/8wR2SiRS7MNACwTyrGvt9ts8p12PKFdlqYTopNHR1Vf7XjfhQlVsAJdNiKdYmYVoKlaRv85IfVunYzO0IKXsyl7JCUjCpoG20f0a04COwfneQAGGwd5oa+T8yO5hzuyDb/XcxxmK01EpqOyuxINew=="
  iv := "r7BXXKkLb8qrSNn05n0qiA=="

/*  ha := WXBizDataCrypt{
      appId: "wxb8776b4df5c6ef6a",
      sessionKey : "ezcAawD6urUDuNetdZulOg=="}
   encryptedData := "BoG3R4blaQ5PwtODlnvbMowmmN37KI44bc3UPGa/dBpI4LelPkJ5C2vJ2xjEC6HTTT5VvyBKeQu6O+vBA7yc+D/0gSJRXhCUk0m1MoR1d2bqpxCQewnsAAj47f2ieo3B+bVI5Dayw06izx5EgMokZear7ZxT5VXAAoppTRximeht0MwZyH6XR9lakwPwrZfZPcd3hgGd81NPUdvsVyTHXiXnr4TRRPhw9z+cwTTy7HgVI6JmDjD1SgtyP2o4huVcc66UWzve5yAgCT2Q7DJvH8b+XZEFmRgYjlZfaspS1oK/kOEgbzJu+1m/MsZfMUGsh1TE9JkwrH6EjLhjuhhJNxAe7nO9mN7W8COs8wfvxWfhS3uwUjmg3dRiQpxIrmfewpcJQQr0mw68vl6KneXeY26LgmCyAzYPzUu946pYSJrhbLvEds4gMGJb7R4XipwMLZ4tcY1rgs4Md8noBAicWLUjNsNLNeoxvUOVEVvA1HcPo4fowg2G+ibGRiJP/AQORzigimbLAux1b/nRunx3KQ=="
   iv := "FYmK+E8nT6SLJUP9ytvSvQ=="*/


  res := ha.decrypt(encryptedData, iv)


fmt.Println(res)
}


最后一次编辑于  03-20  (未经腾讯允许,不得转载)
邀请回答
复制链接收藏投诉关注问题回答