登录
首页 >  Golang >  Go问答

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

来源:stackoverflow

时间:2024-04-20 18:24:36 357浏览 收藏

最近发现不少小伙伴都对Golang很感兴趣,所以今天继续给大家介绍Golang相关的知识,本文《尝试在 Golang 中使用 crypto/aes 验证 NIST AES 示例 vectos》主要内容涉及到等等知识点,希望能帮到你!当然如果阅读本文时存在不同想法,可以在评论中表达,但是请勿使用过激的措辞~

问题内容

正如标题所示,我正在尝试使用 golang crypto/aes 实现来验证 aes 128 ecb 的 nist 示例向量之一。 例如: 来自 nist sp 800-38a 附录 f

f.1.1 ecb-aes128.加密

密钥 2b7e151628aed2a6abf7158809cf4f3c

区块 #1 明文 6bc1bee22e409f96e93d7e117393172a

输入块 6bc1bee22e409f96e93d7e117393172a

输出块 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
~�i�◄`���∟Ձll��↔��
6bc1bee22e409f96e93d7e117393172a

但是我希望它们是:

6bc1bee22e409f96e93d7e117393172a
3ad77bb40d7a3660a89ecaf32466ef97
3ad77bb40d7a3660a89ecaf32466ef97
6bc1bee22e409f96e93d7e117393172a

解决方案


正如 Marc 评论的那样,使用编码/十六进制库的 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
3ad77bb40d7a3660a89ecaf32466ef97
6bc1bee22e409f96e93d7e117393172a

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>