Golang AES ECB加密

 shaihaiyou 发布于 2023-01-11 13:39

尝试在Go中模拟基本上是AES ECB模式加密的算法.

这是我到目前为止所拥有的

func Decrypt(data []byte) []byte {
    cipher, err := aes.NewCipher([]byte(KEY))
    if err == nil {
        cipher.Decrypt(data, PKCS5Pad(data))
        return data
    }
    return nil
}

我还有一个PKCS5Padding算法,经过测试和工作,它首先填充数据.我无法找到有关如何在Go AES包中切换加密模式的任何信息(它绝对不在文档中).

我有这个代码用另一种语言,这就是我知道这个算法不能正常工作的方式.

编辑:这是我在问题页面上解释的方法

func AESECB(ciphertext []byte) []byte {
    cipher, _ := aes.NewCipher([]byte(KEY))
    fmt.Println("AESing the data")
    bs := 16
    if len(ciphertext)%bs != 0     {
        panic("Need a multiple of the blocksize")
    }

    plaintext := make([]byte, len(ciphertext))
    for len(plaintext) > 0 {
        cipher.Decrypt(plaintext, ciphertext)
        plaintext = plaintext[bs:]
        ciphertext = ciphertext[bs:]
    }
    return plaintext
}

这实际上并没有返回任何数据,也许我在将其从编写脚本转换为decripting时搞砸了

2 个回答
  • ESB是一种非常简单的操作模式。要加密的数据分为字节块,所有字节块的大小相同。对于每个块,应用一个密码,在这种情况下为AES,生成加密的块。

    下面的代码片段解密ECB中的AES-128数据(请注意,块大小为16个字节):

    package main
    
    import (
        "crypto/aes"
    )
    
    func DecryptAes128Ecb(data, key []byte) []byte {
        cipher, _ := aes.NewCipher([]byte(key))
        decrypted := make([]byte, len(data))
        size := 16
    
        for bs, be := 0, size; bs < len(data); bs, be = bs+size, be+size {
            cipher.Decrypt(decrypted[bs:be], data[bs:be])
        }
    
        return decrypted
    }
    

    如@OneOfOne所述,ECB是不安全的并且非常易于检测,因为重复的块将始终加密为相同的加密块。这个Crypto SE答案很好地解释了原因。

    2023-01-11 13:43 回答
  • 欧洲央行故意被排除在外,因为它不安全,请查看问题5597.

    2023-01-11 13:43 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有