The Go Programming Language
› http://golang.org/
› Go Playground
Go Projects
› Revel Web Framework
chengxiao

Aes 加密 CBC 模式的 Nopadding 填充在 GO 里怎么实现啊

  •  
  •   chengxiao · Oct 9, 2020 · 4292 views
    This topic created in 2221 days ago, the information mentioned may be changed or developed.
    对接某商户系,被这个签名搞的头大,这个 NoPadding 似乎是个 java 里的方法,没查到相关信息

    业务参数加密步骤:
    1 )将业务参数转换成 json 格式字符串

    2 )用 AES 算法加密,秘钥为 appSecret 重复两遍,偏移量为 appSecret 重复两遍(16 位秘钥不用重复),加密模式 CBC,填充模式; NoPadding

    3 )加密后字符即为公共参数 requestBody 的值

    业务参数示例:
    {
    "number":"123",
    "string":"测试",
    "double":1.0,
    "boolean":true
    }

    开发者秘钥为:12345678

    AES 加密结果:
    Vq2Kj4Z8C+hFf7VdFPbCoY3LmJgf2LM7B2a3klFxGWMlL1BAxx7v/ZZdkxqdMPXqRPjy3k0tVKgx
    KMTTAlbE0PGhp2LtqzouDWZaH1g9ttQ=
    12 replies  •  2023-08-11 10:38:13 +08:00
    kingiis
    kingiis
    Oct 9, 2020
    1
    公用 c 库可能会好点
    songjiaxin2008
    songjiaxin2008
    Oct 9, 2020
    2
    可以参考一下这个实现 https://github.com/wumansgy/goEncrypt 引用的包都是标准库的
    songjiaxin2008
    songjiaxin2008
    Oct 9, 2020
    3
    NoPadding = ZeroPadding
    songjiaxin2008
    songjiaxin2008
    Oct 9, 2020
    4
    网页上跑了下应该没啥问题
    geelaw
    geelaw
    Oct 9, 2020 via iPhone
    5
    为了数据安全最好还是不要对接这个系统,使用非伪随机的密钥以及使用密钥相关的 IV,都会破坏 AES 所有的安全保证。
    teawithlife
    teawithlife
    Oct 10, 2020
    6
    其实就是后面补 0x00
    play.golang.org/p/MxstiahPDa3
    dafsic
    dafsic
    Oct 10, 2020
    7
    我对接过,跟 java 版本有关,问问 java 是什么版本,最好要到加密部分的 java 代码看看
    TypeErrorNone
    TypeErrorNone
    Oct 10, 2020
    8
    之前写的,可以参考下

    ```
    package xaes

    import (
    "bytes"
    "crypto/aes"
    "crypto/cipher"
    "crypto/rand"
    "crypto/sha256"
    "encoding/base64"
    "github.com/pkg/errors"
    )

    type Aes struct {
    key []byte
    }

    func NewAes(key string) (*Aes) {
    if key == "" {
    panic("aes key empty")
    }
    sum := sha256.Sum256([]byte(key))
    return &Aes{
    key:sum[:],
    }
    }

    func (a *Aes) Encrypt(encodeBytes []byte) (val string, err error) {
    block, err := aes.NewCipher(a.key)
    if err != nil {
    return
    }
    blockSize := block.BlockSize()
    encodeBytes = a.pkCS5Padding(encodeBytes, blockSize)

    iv := make([]byte, blockSize)
    _,err = rand.Read(iv)
    if err != nil {
    return
    }

    blockMode := cipher.NewCBCEncrypter(block, iv)
    crypted := make([]byte, len(encodeBytes))
    blockMode.CryptBlocks(crypted, encodeBytes)

    iv = append(iv,crypted...)
    val = base64.StdEncoding.EncodeToString(iv)
    return
    }


    func (a *Aes) pkCS5Padding(ciphertext []byte, blockSize int) []byte {
    padding := blockSize - len(ciphertext)%blockSize
    padtext := bytes.Repeat([]byte{byte(padding)}, padding)
    return append(ciphertext, padtext...)
    }

    func (a *Aes) Decrypt(decodeStr string) (origData []byte,err error) {
    decodeBytes, err := base64.StdEncoding.DecodeString(decodeStr)
    if err != nil {
    return
    }
    block, err := aes.NewCipher(a.key)
    if err != nil {
    return nil, err
    }
    if len(decodeBytes) < block.BlockSize() {
    err = errors.New("decodeBytes 长度不足")
    return
    }
    iv := decodeBytes[:block.BlockSize()]
    decodeBytes = decodeBytes[block.BlockSize():]

    blockMode := cipher.NewCBCDecrypter(block, iv)
    origData = make([]byte, len(decodeBytes))
    blockMode.CryptBlocks(origData, decodeBytes)
    origData = a.pkCS5UnPadding(origData)
    return
    }

    func (a *Aes) pkCS5UnPadding(origData []byte) []byte {
    length := len(origData)
    unpadding := int(origData[length-1])
    return origData[:(length - unpadding)]
    }
    ```
    chengxiao
    chengxiao
    OP
    Oct 10, 2020
    9
    @TypeErrorNone
    @teawithlife
    @songjiaxin2008
    谢谢几位老哥 就是被这个 NoPadding 怎么补位搞蒙了 原来 ZeroPadding 就可以
    sunmoon1983
    sunmoon1983
    Dec 2, 2021
    10
    @TypeErrorNone
    哥们,我试了试你给的方法,貌似不太行呀,我把 pkCS5Padding 改成了 zeroPadding,最后出来的数据和人家给的数据就是不太一样
    sunmoon1983
    sunmoon1983
    Dec 2, 2021
    11
    @chengxiao 哥们,你搞出来了吗? Aes Nopadding 加密
    pifuant
    pifuant
    Aug 11, 2023
    12
    @sunmoon1983 @chengxiao 老哥们, 怎么解决的,ZeroPadding 不行啊
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1555 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 00:30 · PVG 08:30 · LAX 16:30 · JFK 19:30
    ♥ Do have faith in what you're doing.