使用CTR模式解密

发布于 2024-12-02 12:27:24 字数 1552 浏览 2 评论 0原文

我试图了解使用 CTR 模式的加密是如何工作的,所以我创建了这些函数来测试它:

import (
    "crypto/cipher"
    "crypto/rand"
)

// generateIV generates an initialization vector (IV) suitable for encryption.
//
// http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Initialization_vector_.28IV.29
func generateIV(bytes int) []byte {
    b := make([]byte, bytes)
    rand.Read(b)
    return b
}

func encrypt(block cipher.Block, value []byte) []byte {
    iv := generateIV(block.BlockSize())
    encrypted := make([]byte, len(value) + block.BlockSize())
    encrypted = append(encrypted, iv...)
    stream := cipher.NewCTR(block, iv)
    stream.XORKeyStream(encrypted, value)
    return encrypted
}

func decrypt(block cipher.Block, encrypted []byte) []byte {
    iv := encrypted[:block.BlockSize()]
    ciphertext := encrypted[block.BlockSize():]
    stream := cipher.NewCTR(block, iv)
    plain := make([]byte, len(ciphertext))
    // XORKeyStream is used to decrypt too?
    stream.XORKeyStream(plain, ciphertext)
    return plain
}

加密似乎工作正常,但我真的不知道,因为我不明白解密的输出。我也应该使用stream.XORKeyStream来解密吗?测试看起来像这样:

import (
    "crypto/aes"
    "fmt"
    "testing"
)

func TestEncryptCTR(t *testing.T) {
    block, err := aes.NewCipher([]byte("1234567890123456"))
    if err != nil {
        panic(err)
    }

    value := "foobarbaz"
    encrypted := encrypt(block, []byte(value))
    decrypted := decrypt(block, encrypted)
    fmt.Printf("--- %s ---", string(decrypted))
}

但我绝对不会得到“foobarbaz”回来。你能发现我做错了什么吗?

I'm trying to understand how encryption using the CTR mode works, so I created these functions to test it:

import (
    "crypto/cipher"
    "crypto/rand"
)

// generateIV generates an initialization vector (IV) suitable for encryption.
//
// http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Initialization_vector_.28IV.29
func generateIV(bytes int) []byte {
    b := make([]byte, bytes)
    rand.Read(b)
    return b
}

func encrypt(block cipher.Block, value []byte) []byte {
    iv := generateIV(block.BlockSize())
    encrypted := make([]byte, len(value) + block.BlockSize())
    encrypted = append(encrypted, iv...)
    stream := cipher.NewCTR(block, iv)
    stream.XORKeyStream(encrypted, value)
    return encrypted
}

func decrypt(block cipher.Block, encrypted []byte) []byte {
    iv := encrypted[:block.BlockSize()]
    ciphertext := encrypted[block.BlockSize():]
    stream := cipher.NewCTR(block, iv)
    plain := make([]byte, len(ciphertext))
    // XORKeyStream is used to decrypt too?
    stream.XORKeyStream(plain, ciphertext)
    return plain
}

Encryption seems to work fine, but well I don't know really because I don't understand the output of decryption. Should I use stream.XORKeyStream to decrypt too? The test looks like this:

import (
    "crypto/aes"
    "fmt"
    "testing"
)

func TestEncryptCTR(t *testing.T) {
    block, err := aes.NewCipher([]byte("1234567890123456"))
    if err != nil {
        panic(err)
    }

    value := "foobarbaz"
    encrypted := encrypt(block, []byte(value))
    decrypted := decrypt(block, encrypted)
    fmt.Printf("--- %s ---", string(decrypted))
}

But I'm definitely not getting "foobarbaz" back. Can you spot what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖伴 2024-12-09 12:27:24

问题是我在测试基础知识之前尝试做太多事情。我想将 IV 添加到生成的密文中,但当我这样做时,我有点破坏了一切。这个简单的版本,没有预先设置的 IV,可以工作:

import (
    "crypto/cipher"
    "crypto/rand"
)

// generateIV generates an initialization vector (IV) suitable for encryption.
//
// http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Initialization_vector_.28IV.29
func generateIV(bytes int) []byte {
    b := make([]byte, bytes)
    rand.Read(b)
    return b
}

func encrypt(block cipher.Block, value []byte, iv []byte) []byte {
    stream := cipher.NewCTR(block, iv)
    ciphertext := make([]byte, len(value))
    stream.XORKeyStream(ciphertext, value)
    return ciphertext
}

func decrypt(block cipher.Block, ciphertext []byte, iv []byte) []byte {
    stream := cipher.NewCTR(block, iv)
    plain := make([]byte, len(ciphertext))
    // XORKeyStream is used to decrypt too!
    stream.XORKeyStream(plain, ciphertext)
    return plain
}

相应的测试:

import (
    "crypto/aes"
    "fmt"
    "testing"
)

func TestEncryptCTR(t *testing.T) {
    block, err := aes.NewCipher([]byte("1234567890123456"))
    if err != nil {
        panic(err)
    }

    iv := generateIV(block.BlockSize())
    value := "foobarbaz"
    encrypted := encrypt2(block, []byte(value), iv)
    decrypted := decrypt2(block, encrypted, iv)
    fmt.Printf("--- %s ---", string(decrypted))
}

我得到“--- foobarbaz ---”,如预期的那样。

现在回来让前面的 IV 工作。 :)

编辑 这就是它,带有自动生成和前置的 IV:

func encrypt(block cipher.Block, value []byte) []byte {
    // Generate an initialization vector (IV) suitable for encryption.
    // http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Initialization_vector_.28IV.29
    iv := make([]byte, block.BlockSize())
    rand.Read(iv)
    // Encrypt it.
    stream := cipher.NewCTR(block, iv)
    stream.XORKeyStream(value, value)
    // Return iv + ciphertext.
    return append(iv, value...)
}

func decrypt(block cipher.Block, value []byte) []byte {
    if len(value) > block.BlockSize() {
        // Extract iv.
        iv := value[:block.BlockSize()]
        // Extract ciphertext.
        value = value[block.BlockSize():]
        // Decrypt it.
        stream := cipher.NewCTR(block, iv)
        stream.XORKeyStream(value, value)
        return value
    }
    return nil
}

The problem was me trying to do too much before testing the basics. I wanted to prepend the IV to the generated ciphertext, but somewhat I broke everything when I did that. This simple version, with no prepended IV, works:

import (
    "crypto/cipher"
    "crypto/rand"
)

// generateIV generates an initialization vector (IV) suitable for encryption.
//
// http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Initialization_vector_.28IV.29
func generateIV(bytes int) []byte {
    b := make([]byte, bytes)
    rand.Read(b)
    return b
}

func encrypt(block cipher.Block, value []byte, iv []byte) []byte {
    stream := cipher.NewCTR(block, iv)
    ciphertext := make([]byte, len(value))
    stream.XORKeyStream(ciphertext, value)
    return ciphertext
}

func decrypt(block cipher.Block, ciphertext []byte, iv []byte) []byte {
    stream := cipher.NewCTR(block, iv)
    plain := make([]byte, len(ciphertext))
    // XORKeyStream is used to decrypt too!
    stream.XORKeyStream(plain, ciphertext)
    return plain
}

And the corresponding test:

import (
    "crypto/aes"
    "fmt"
    "testing"
)

func TestEncryptCTR(t *testing.T) {
    block, err := aes.NewCipher([]byte("1234567890123456"))
    if err != nil {
        panic(err)
    }

    iv := generateIV(block.BlockSize())
    value := "foobarbaz"
    encrypted := encrypt2(block, []byte(value), iv)
    decrypted := decrypt2(block, encrypted, iv)
    fmt.Printf("--- %s ---", string(decrypted))
}

I get "--- foobarbaz ---", as expected.

Now back to make the prepending IV work. :)

Edit And this is it, with auto-generated and prepended IV:

func encrypt(block cipher.Block, value []byte) []byte {
    // Generate an initialization vector (IV) suitable for encryption.
    // http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Initialization_vector_.28IV.29
    iv := make([]byte, block.BlockSize())
    rand.Read(iv)
    // Encrypt it.
    stream := cipher.NewCTR(block, iv)
    stream.XORKeyStream(value, value)
    // Return iv + ciphertext.
    return append(iv, value...)
}

func decrypt(block cipher.Block, value []byte) []byte {
    if len(value) > block.BlockSize() {
        // Extract iv.
        iv := value[:block.BlockSize()]
        // Extract ciphertext.
        value = value[block.BlockSize():]
        // Decrypt it.
        stream := cipher.NewCTR(block, iv)
        stream.XORKeyStream(value, value)
        return value
    }
    return nil
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文