Go 只加密 16 字节消息长度的文本吗?

Hen*_*owo -1 encryption aes go

我尝试在 Golang 中使用 AES 加密消息。

func main() {
    key := "mysupersecretkey32bytecharacters"
    plainText := "thisismyplaintextingolang"

    fmt.Println("My Encryption")
    byteCipherText := encrypt([]byte(key), []byte(plainText))
    fmt.Println(byteCipherText)
}

func encrypt(key, plaintext []byte) []byte {
    cphr, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }
    ciphertext := make([]byte, len(plaintext))
    cphr.Encrypt(ciphertext, plaintext)
    return ciphertext
}
Run Code Online (Sandbox Code Playgroud)

该函数返回: [23 96 11 10 70 223 95 118 157 250 80 92 77 26 137 224 0 0 0 0 0 0 0 0 0]

在该结果中,只有 16 个非零字节值。这意味着 Go 中的 AES 加密仅加密 16 个字符。是否可以在不使用任何 AES 模式(如 GCM、CBC、CFB、..等)的情况下,在 Go AES 中加密超过 16 个字符,而只是纯 AES?

rus*_*tyx 6

aes.NewCipher返回 的一个实例cipher.Block,它以 16 字节的块进行加密(这就是纯 AES 的工作原理)。

操作模式从字面上决定了如何加密长度超过 16 字节的消息。最简单的一种是 ECB(一种“无操作”模式),它只是使用相同的密钥以 16 字节的块重复加密。您可以使用简单的 for 循环执行相同操作,但请记住,ECB 不是很安全。