golang实现百度智能小程序用户数据的解密

golang实现百度智能小程序用户数据的解密

百度智能小程序官方文档

博主在网上gitee github找了很多 几乎没有看到适配golang的方法,以下方法可行

类库

/*
 * @author anderyly
 * @email admin@aaayun.cc
 * @link https://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)
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇