开发者

Decrypt using the CTR mode

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?


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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜