drzil26260 2019-02-02 11:17
浏览 375
已采纳

aes.NewCipher返回一个有效的块,但调用block.BlockSize()时出现错误

I'm trying to get AES encryption and decryption working in my go API, I create a new block using aes.NewCipher(mykey) and the block seems to be valid, but then when I call cipher.NewCBCDecrypter(block, iv) the function causes a "invalid memory address or nil pointer dereference" error at line 26 in cbc.go when it tries to call b.BlockSize().

So I tried calling block.BlockSize() from my own code and I also get an exception, but when I check the block var it's not nil.

func Decrypt(data []byte) (result []byte, err error) {
    logger := logrus.New()

    logger.Infof("Starting decryption for string: %s
", string(data[:]))

    var block cipher.Block

    if block, err := aes.NewCipher([]byte(app.Config.AESKey)); (err != nil) || (block == nil) {
        logger.Infof("Failed to create block
")
        return nil, err
    }

    if len(data) < aes.BlockSize {
        logger.Infof("Input too short
")
        return nil, nil
    }

    cbc := cipher.NewCBCDecrypter(block, []byte(app.Config.AESIV))
    cbc.CryptBlocks(data, data)

    return data, nil
}

I would expect the function to work properly since aes.NewCipher doesn't return an error.

  • 写回答

2条回答 默认 最新

  • dsmnedc798226 2019-02-02 12:35
    关注

    Remove this line

    var block cipher.Block
    

    As you are initializing block in the next line.

    Essentially, you are creating 2 block variables.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器