dpxo13079 2015-05-19 12:49
浏览 57
已采纳

三重DES解密在再次解密时返回错误的前16个字节

I have a problem with decryption when I try to decrypt the same byte slice again.

Example of code for clarification:

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
", data)
    fmt.Printf("1 try of decryption result : %x
", decoded1)
    fmt.Printf("2 try of decryption result : %x
", decoded2)
    fmt.Printf("3 try of decryption result : %x
", decoded3)
    fmt.Printf("4 try of decryption result : %x
", 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
}

This code is also available and runnable on the playground.

It gives the following result:

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

As you can see the first decryption works well and returns valid result, but all other tries returns the wrong result. The first 16 bytes of result is not as in source byte slice.

Can somebody describe what I am doing wrong?

  • 写回答

1条回答 默认 最新

  • douwei6478 2015-05-19 13:05
    关注

    Short version: don't reuse the decrypter object.

    Longer version: You're using a cipher in CBC mode: when encrypting the data, the plaintext for block N is XOR-ed with the ciphertext for block N-1 (or the IV, on the first block). On decryption this is done in reverse.

    This means that when you try and reuse your decrypter object you don't get the correct results because the state isn't correct - it is decrypting the blocks as if they were subsequent blocks in your message. A peculiarity of CBC is that an incorrect IV will only affect the first decrypted block.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?