我尝试在 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
}
该函数返回:[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 个字符。是否可以在 Go AES 中加密超过 16 个字符而不使用 AES 中的任何模式(如 GCM、CBC、CFB、..etc),只使用纯 AES?
最佳答案
aes.NewCipher
返回 cipher.Block
的实例,它以 16 字节的 block 进行加密(这就是纯 AES 的工作原理)。
mode of operation从字面上确定如何加密超过 16 个字节的消息。最简单的一种是 ECB(一种“无操作”模式),它只是使用相同的 key 以 16 字节的 block 重复加密。您可以使用简单的 for 循环执行相同操作,但请记住 ECB 不是很安全。
关于go - Go 是否只加密 16 字节消息长度的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63269376/