[GO] 解决:crypto/aes: invalid key size 14

当使用AES加解密的时候报了这个错误

原因是AES的key字节长度不对

看源码

// NewCipher creates and returns a new cipher.Block.
// The key argument should be the AES key,
// either 16, 24, or 32 bytes to select
// AES-128, AES-192, or AES-256.
func NewCipher(key []byte) (cipher.Block, error) {
    k := len(key)
    switch k {
    default:
        return nil, KeySizeError(k)
    case 16, 24, 32:
        break
    }
    return newCipher(key)
}

只允许16、24、32字节长度

所以把key设置成16字节长度就ok了,英文等字符,一个字符一个字节

posted @ 2021-07-18 12:18  唯一客服系统开发笔记  阅读(1228)  评论(0编辑  收藏  举报