我试图了解使用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))
}但我绝对不会要回“狐步舞”你能发现我做错了什么吗?
发布于 2011-08-31 22:37:25
问题是,在测试基本知识之前,我试图做的太多了。我想把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))
}我明白了
现在回来让预先准备好的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
}https://stackoverflow.com/questions/7263928
复制相似问题