【问题标题】:Triple DES decryption returns wrong first 16 bytes of when decrypted again再次解密时,三重 DES 解密返回错误的前 16 个字节
【发布时间】:2015-05-19 12:49:54
【问题描述】:

当我再次尝试解密相同的字节片时,我遇到了解密问题。

澄清代码示例:

package main

import (
    "fmt"
    "crypto/cipher"
    "crypto/des"
)

const (
    // tripleKey is TripleDES key string (3x8 bytes)
    tripleKey = "12345678asdfghjkzxcvbnmq"
)

var (
    encrypter cipher.BlockMode
    decrypter cipher.BlockMode
)

func init() {
    // tripleDESChiper is chiper block based on tripleKey used for encryption/decryption
    tripleDESChiper, err := des.NewTripleDESCipher([]byte(tripleKey))
    if err != nil {
        panic(err)
    }

    // iv is Initialization Vector used for encrypter/decrypter creation
    ciphertext := []byte("0123456789qwerty")
    iv := ciphertext[:des.BlockSize]

    // create encrypter and decrypter
    encrypter = cipher.NewCBCEncrypter(tripleDESChiper, iv)
    decrypter = cipher.NewCBCDecrypter(tripleDESChiper, iv)
}

func main() {
    message := "12345678qwertyuia12345678zxcvbnm,12345678poiuytr"
    data := []byte(message)
    hash := encrypt(data)

    decoded1 := decrypt(hash)
    decoded2 := decrypt(hash)
    decoded3 := decrypt(hash)
    decoded4 := decrypt(hash)


    fmt.Printf("encrypted data :             %x\n", data)
    fmt.Printf("1 try of decryption result : %x\n", decoded1)
    fmt.Printf("2 try of decryption result : %x\n", decoded2)
    fmt.Printf("3 try of decryption result : %x\n", decoded3)
    fmt.Printf("4 try of decryption result : %x\n", decoded4)
}

func encrypt(msg []byte) []byte {
    encrypted := make([]byte, len(msg))
    encrypter.CryptBlocks(encrypted, msg)

    return encrypted
}

func decrypt(hash []byte) []byte {
    decrypted := make([]byte, len(hash))
    decrypter.CryptBlocks(decrypted, hash)

    return decrypted
}

此代码也可用且可运行 在the playground

它给出以下结果:

encrypted data :             313233343536373871776572747975696131323334353637387a786376626e6d2c3132333435363738706f6975797472
1 try of decryption result : 313233343536373871776572747975696131323334353637387a786376626e6d2c3132333435363738706f6975797472
2 try of decryption result : 5e66fa74456402c271776572747975696131323334353637387a786376626e6d2c3132333435363738706f6975797472
3 try of decryption result : 5e66fa74456402c271776572747975696131323334353637387a786376626e6d2c3132333435363738706f6975797472
4 try of decryption result : 5e66fa74456402c271776572747975696131323334353637387a786376626e6d2c3132333435363738706f6975797472

如您所见,第一次解密运行良好并返回有效结果, 但所有其他尝试都返回错误的结果。 结果的前 16 个字节与源字节切片不同。

谁能描述我做错了什么?

【问题讨论】:

    标签: encryption go tripledes


    【解决方案1】:

    短版:不要重复使用解密器对象。

    加长版:您在 CBC 模式下使用密码:加密数据时,块 N 的明文与块 N-1(或第一个块上的 IV)的密文进行异或运算。在解密时,这是反向完成的。

    这意味着当您尝试并重用您的解密器对象时,您不会得到正确的结果,因为状态不正确 - 它正在解密块,就好像它们是您消息中的后续块一样。 CBC 的一个特点是不正确的 IV 只会影响第一个解密的块。

    【讨论】:

    • 弗雷德里克,非常感谢!我只是重写我的代码为每次迭代创建新的解密器,现在它工作正常。
    • 尤里,你有这方面的例子吗?
    猜你喜欢
    • 2015-06-22
    • 1970-01-01
    • 2015-01-11
    • 1970-01-01
    • 2012-05-06
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多