gpt4 book ai didi

encryption - 使用 CTR 模式解密

转载 作者:IT王子 更新时间:2023-10-29 00:46:49 40 4
gpt4 key购买 nike

我试图了解使用 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”回来。你能发现我做错了什么吗?

最佳答案

问题是我在测试基础知识之前尝试做太多事情。我想将 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
}

关于encryption - 使用 CTR 模式解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7263928/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com