请教 go 标准库 aes 的默认的加密模式是什么?没有 openSSL 里的那种初始化变量 IV,是有个默认值?还是说 go 标准库里默认的初始化 iv=key ?

2022-05-28 14:29:59 +08:00
 xiaoyanbot
请教如下的示例代码,没有 IV (初始化向量)

来源: https://www.developer.com/languages/cryptography-in-go/

上面文章提到的案例是:
~~~

package main

import (
"crypto/aes"
"encoding/hex"
"fmt"
)

func encryptMessage(key string, message string) string {
c, err := aes.NewCipher([]byte(key))
if err != nil {
fmt.Println(err)
}
msgByte := make([]byte, len(message))
c.Encrypt(msgByte, []byte(message))
return hex.EncodeToString(msgByte)
}

func decryptMessage(key string, message string) string {
txt, _ := hex.DecodeString(message)
c, err := aes.NewCipher([]byte(key))
if err != nil {
fmt.Println(err)
}
msgByte := make([]byte, len(txt))
c.Decrypt(msgByte, []byte(txt))

msg := string(msgByte[:])
return msg
}

func main() {

plainText := "This is a secret"
key := "this_must_be_of_32_byte_length!!"

emsg := encryptMessage(key, plainText)
dmesg := decryptMessage(key, emsg)

fmt.Println("Encrypted Message: ", emsg)
fmt.Println("Decrypted Message: ", dmesg)

}

~~~

请教下:aes 的默认的加密模式是什么? [比如 cbc 之类的加密模式是哪一种?] 没有 openSSL 里的那种初始化变量 IV ,是有个默认值?还是说 go 标准库里默认的初始化 iv=key ?
587 次点击
所在节点    问与答
1 条回复
Frankcox
2022-05-28 15:27:57 +08:00
这里发的示例代码使用的是 crypto/aes 包下的 cipher 。是不使用初始化向量 IV 的,AES-ECB 模式不使用 IV 。
如果要使用需要初始化向量 IV 的其他模式,需要使用 crypto/cipher 下的 CBC 、CFB 等。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/855847

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX