尝试在Golang使用crypto/AES验证NIST AES示例vectos

尝试在Golang使用crypto/AES验证NIST AES示例vectos,go,encryption,aes,Go,Encryption,Aes,正如标题所说,我正在尝试验证使用Golang crypto/AES实现的AES 128 ECB之一。 例如: 来自NIST SP 800-38A附录F F.1.1 ECB-AES128.1 图例2b7e151628aed2a6abf7158809cf4f3c 块#1 纯文本6BC12E409F96E93D7E117393172A 输入块6BC12E409F96E93D7E117393172A 输出块3ad77bb40d7a3660a89ecaf32466ef97 密文3ad77bb40d7a36

正如标题所说,我正在尝试验证使用Golang crypto/AES实现的AES 128 ECB之一。 例如: 来自NIST SP 800-38A附录F

F.1.1 ECB-AES128.1

图例2b7e151628aed2a6abf7158809cf4f3c

块#1
纯文本6BC12E409F96E93D7E117393172A

输入块6BC12E409F96E93D7E117393172A

输出块3ad77bb40d7a3660a89ecaf32466ef97

密文3ad77bb40d7a3660a89ecaf32466ef97

我的代码是:

package main

import (
    "crypto/aes"
    "fmt"
)

func main() {
    key := []byte("2b7e151628aed2a6abf7158809cf4f3c")
    c, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }
    in := []byte("6bc1bee22e409f96e93d7e117393172a")
    fmt.Println(string(in))

    expected := []byte("3ad77bb40d7a3660a89ecaf32466ef97")
    fmt.Println(string(expected))
    temp := make([]byte, c.BlockSize())
    out := make([]byte, len(in))

    c.Encrypt(temp, in[:c.BlockSize()])
    for i := 0; i < c.BlockSize(); i++ {
        out[i] = temp[i]
    }
    c.Encrypt(temp, in[c.BlockSize():])
    for i := c.BlockSize(); i < len(out); i++ {
        out[i] = temp[i-16]
    }

    fmt.Println(string(out))

    decrypted := make([]byte, len(in))
    c.Decrypt(temp, out[:c.BlockSize()])
    for i := 0; i < c.BlockSize(); i++ {
        decrypted[i] = temp[i]
    }
    c.Decrypt(temp, out[c.BlockSize():])
    for i := c.BlockSize(); i < len(decrypted); i++ {
        decrypted[i] = temp[i-16]
    }
    fmt.Println(string(decrypted))
}
然而,我希望他们:

6bc1bee22e409f96e93d7e117393172a
3ad77bb40d7a3660a89ecaf32466ef97
3ad77bb40d7a3660a89ecaf32466ef97
6bc1bee22e409f96e93d7e117393172a
如前所述,使用encoding/hex库的DecodeString将字符串更改为十六进制有助于我获得正确的解决方案

使用EncodeToString,我设法再次使用正确的字符串表示。 这是正确的答案,多亏了马克

package main

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

func main() {
    key, err := hex.DecodeString("2b7e151628aed2a6abf7158809cf4f3c")
    if err != nil {
        panic(err)
    }
    c, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }
    in, err := hex.DecodeString("6bc1bee22e409f96e93d7e117393172a")
    if err != nil {
        panic(err)
    }
    fmt.Println(hex.EncodeToString(in))

    expected, err := hex.DecodeString("3ad77bb40d7a3660a89ecaf32466ef97")
    if err != nil {
        panic(err)
    }
    fmt.Println(hex.EncodeToString(expected))
    out := make([]byte, len(in))
    c.Encrypt(out, in)
    fmt.Println(hex.EncodeToString(out))

    decrypted := make([]byte, len(in))
    c.Decrypt(decrypted, out)
    fmt.Println(hex.EncodeToString(decrypted))
}
现在输出为:

6bc1bee22e409f96e93d7e117393172a
3ad77bb40d7a3660a89ecaf32466ef97
~�i�◄`���∟ՁLl��↔��
6bc1bee22e409f96e93d7e117393172a
6bc1bee22e409f96e93d7e117393172a
3ad77bb40d7a3660a89ecaf32466ef97
3ad77bb40d7a3660a89ecaf32466ef97
6bc1bee22e409f96e93d7e117393172a

您正在打印二进制数据。十六进制字符串的打印:
fmt.Printf(“%x”,out)
它不起作用,我得到的是3360ec85c7925b94340e49a64c6ce3a41df28e0d7e8169c91160a5d7f61cd581而不是3AD77BB40D7A3660A89ECAF326EF97谢谢你的回复!请阅读文章中的说明:“所有字符串均以十六进制表示法表示”。