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 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件