golang实现百度智能小程序用户数据的解密
博主在网上gitee github找了很多 几乎没有看到适配golang的方法,以下方法可行
类库
/*
* @author anderyly
* @email admin@aaayun.cc
* @link http://blog.aaayun.cc
* @copyright Copyright (c) 2023
*
*/
package baidu
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"regexp"
)
var _ AppletDataCrypt = (*appletDataCrypt)(nil)
type AppletDataCrypt interface {
Decrypt(data string, iv string, isJSON bool) (interface{}, error) // 解密
}
type appletDataCrypt struct {
AppID string
SessionKey string
}
var errorCode = map[string]int{
"IllegalAppID": -41000,
"IllegalAesKey": -41001,
"IllegalIV": -41002,
"IllegalBuffer": -41003,
"DecodeBase64Error": -41004,
"DecodeJsonError": -41005,
}
type showError struct {
errorCode int
errorMsg error
}
func NewAppletDataCrypt(appid, sessionKey string) AppletDataCrypt {
return &appletDataCrypt{
AppID: appid,
SessionKey: sessionKey,
}
}
func (e showError) Error() string {
return fmt.Sprintf("{code: %v, error: \"%v\"}", e.errorCode, e.errorMsg)
}
func (con *appletDataCrypt) Decrypt(data string, iv string, isJSON bool) (interface{}, error) {
aesKey, err := base64.StdEncoding.DecodeString(con.SessionKey)
if err != nil {
return nil, showError{errorCode["DecodeBase64Error"], err}
}
if len(iv) != 24 {
return nil, showError{errorCode["IllegalIV"], errors.New("iv length is error")}
}
aesIV, err := base64.StdEncoding.DecodeString(iv)
if err != nil {
return nil, showError{errorCode["DecodeBase64Error"], err}
}
aesCipherText, err := base64.StdEncoding.DecodeString(data)
if err != nil {
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}
}
mode := cipher.NewCBCDecrypter(aesBlock, aesIV)
mode.CryptBlocks(aesPlantText, aesCipherText)
aesPlantText = con.PKCS7UnPadding(aesPlantText)
var decrypted map[string]interface{}
re := regexp.MustCompile(`[^\{]*(\{.*\})[^\}]*`)
aesPlantText = []byte(re.ReplaceAllString(string(aesPlantText), "$1"))
err = json.Unmarshal(aesPlantText, &decrypted)
if err != nil {
return nil, showError{errorCode["DecodeJsonError"], err}
}
if isJSON == true {
return string(aesPlantText), nil
}
return decrypted, nil
}
func (con *appletDataCrypt) PKCS7UnPadding(plantText []byte) []byte {
length := len(plantText)
if length > 0 {
unPadding := int(plantText[length-1])
return plantText[:(length - unPadding)]
}
return plantText
}
调用方法
// res为json需要自行json.Unmarshal这里不做演示
res, err := baidu.NewAppletDataCryp("appid", "sessionKey").Decrypt("加密的数据", "偏移量", true)
评论 (0)