V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
xiaoyanbot
V2EX  ›  问与答

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

  •  
  •   xiaoyanbot · 2022-05-28 14:29:59 +08:00 · 584 次点击
    这是一个创建于 736 天前的主题,其中的信息可能已经有所发展或是发生改变。
    请教如下的示例代码,没有 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 ?
    1 条回复    2022-05-28 15:27:57 +08:00
    Frankcox
        1
    Frankcox  
       2022-05-28 15:27:57 +08:00
    这里发的示例代码使用的是 crypto/aes 包下的 cipher 。是不使用初始化向量 IV 的,AES-ECB 模式不使用 IV 。
    如果要使用需要初始化向量 IV 的其他模式,需要使用 crypto/cipher 下的 CBC 、CFB 等。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1027 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 18:38 · PVG 02:38 · LAX 11:38 · JFK 14:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.