Skip to content

Commit

Permalink
wxbizdatacrypt golang
Browse files Browse the repository at this point in the history
  • Loading branch information
施凯伦 committed Feb 15, 2017
0 parents commit b1c7b56
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#Golang WXBizDataCrypt

微信小程序用户信息解密Golang 版
```
go get github.com/yilee/wx-biz-data-crypt
```

```go
import (
"github.com/yilee/wx-biz-data-crypt"
)
```



```go
appID := "wx4f4bc4dec97d474b"
sessionKey := "tiihtNczf5v6AKRyjwEUhQ=="
encryptedData := "CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZMQmRzooG2xrDcvSnxIMXFufNstNGTyaGS9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+3hVbJSRgv+4lGOETKUQz6OYStslQ142dNCuabNPGBzlooOmB231qMM85d2/fV6ChevvXvQP8Hkue1poOFtnEtpyxVLW1zAo6/1Xx1COxFvrc2d7UL/lmHInNlxuacJXwu0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn/Hz7saL8xz+W//FRAUid1OksQaQx4CMs8LOddcQhULW4ucetDf96JcR3g0gfRK4PC7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB6Y5anaHqZ9J6nKEBvB4DnNLIVWSgARns/8wR2SiRS7MNACwTyrGvt9ts8p12PKFdlqYTopNHR1Vf7XjfhQlVsAJdNiKdYmYVoKlaRv85IfVunYzO0IKXsyl7JCUjCpoG20f0a04COwfneQAGGwd5oa+T8yO5hzuyDb/XcxxmK01EpqOyuxINew=="
iv := "r7BXXKkLb8qrSNn05n0qiA=="
pc := wxbizdatacrypt.NewWXBizDataCrypt(appID, sessionKey)
userInfo, err := pc.Decrypt(encryptedData, iv)
```
70 changes: 70 additions & 0 deletions wxbizdatacrypt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package wxbizdatacrypt

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

var ErrorAppIDNotMatch = errors.New("app id not match")

type UserInfo struct {
OpenID string `json:"openId"`
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 WXBizDataCrypt struct {
appID, sessionKey string
}

func NewWXBizDataCrypt(appID, sessionKey string) *WXBizDataCrypt {
return &WXBizDataCrypt{
appID: appID,
sessionKey: sessionKey,
}
}

func (w *WXBizDataCrypt) Decrypt(encryptedData, iv string) (*UserInfo, 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 = []byte(strings.Replace(string(cipherText), "\f", "", -1))
cipherText = []byte(strings.Replace(string(cipherText), "\a", "", -1))
var userInfo UserInfo
err = json.Unmarshal(cipherText, &userInfo)
if err != nil {
return nil, err
}
if userInfo.Watermark.AppID != w.appID {
return nil, ErrorAppIDNotMatch
}
return &userInfo, nil
}
17 changes: 17 additions & 0 deletions wxbizdatacrypt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package wxbizdatacrypt

import "testing"

func TestWXBizDataCrypt_Decrypt(t *testing.T) {
appID := "wx4f4bc4dec97d474b"
sessionKey := "tiihtNczf5v6AKRyjwEUhQ=="
encryptedData := "CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZMQmRzooG2xrDcvSnxIMXFufNstNGTyaGS9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+3hVbJSRgv+4lGOETKUQz6OYStslQ142dNCuabNPGBzlooOmB231qMM85d2/fV6ChevvXvQP8Hkue1poOFtnEtpyxVLW1zAo6/1Xx1COxFvrc2d7UL/lmHInNlxuacJXwu0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn/Hz7saL8xz+W//FRAUid1OksQaQx4CMs8LOddcQhULW4ucetDf96JcR3g0gfRK4PC7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB6Y5anaHqZ9J6nKEBvB4DnNLIVWSgARns/8wR2SiRS7MNACwTyrGvt9ts8p12PKFdlqYTopNHR1Vf7XjfhQlVsAJdNiKdYmYVoKlaRv85IfVunYzO0IKXsyl7JCUjCpoG20f0a04COwfneQAGGwd5oa+T8yO5hzuyDb/XcxxmK01EpqOyuxINew=="
iv := "r7BXXKkLb8qrSNn05n0qiA=="
pc := NewWXBizDataCrypt(appID, sessionKey)
userInfo, err := pc.Decrypt(encryptedData, iv)
if err != nil {
t.Error(err)
} else {
t.Logf("%#v\n", userInfo)
}
}

0 comments on commit b1c7b56

Please sign in to comment.