dongshang1529 2018-11-16 05:52
浏览 160
已采纳

Go lang 3DES部分解密了加密的字符串

While performing decryption using 3des the given encrypted text is not fully decrypted, not sure where it went wrong, help me complete the decryption error

The code is avaialbe at Go Playground for Insection and run

package main

import (
    "crypto/des"
    "encoding/hex"
    "fmt"
)

func main() {

    // Mimimum Key Size of Length 24
    key := "mysecretPasswordkeySiz24"
    plainText := "https://8gwifi.org"
    ct := EncryptTripleDES([]byte(key),plainText)
    fmt.Printf("Original Text:  %s
",plainText)
    fmt.Printf("3DES Encrypted Text:  %s
", ct)
    DecryptTripleDES([]byte(key),ct)

}

func EncryptTripleDES(key []byte, plaintext string) string {
        c,err := des.NewTripleDESCipher(key)
    if err != nil {
        fmt.Errorf("NewTripleDESCipher(%d bytes) = %s", len(key), err)
        panic(err)
    }

    out := make([]byte, len(plaintext))
    c.Encrypt(out, []byte(plaintext))
    return hex.EncodeToString(out)

    }


func DecryptTripleDES(key []byte, ct string) {

        ciphertext, _ := hex.DecodeString(ct)
        c, err := des.NewTripleDESCipher([]byte(key))
        if err != nil {
            fmt.Errorf("NewTripleDESCipher(%d bytes) = %s", len(key), err)
            panic(err)
        }
        plain := make([]byte, len(ciphertext))
        c.Decrypt(plain, ciphertext)
        s := string(plain[:])
        fmt.Printf("3DES Decrypyed Text:  %s
", s)
}

The output

Original Text:  https://8gwifi.org
3DES Encrypted Text:  a6e5215154bf86d000000000000000000000
3DES Decrypyed Text:  https://
  • 写回答

1条回答 默认 最新

  • dongyuchen9276 2018-11-16 06:02
    关注

    the given encrypted text is not fully decrypted

    The encrypted text you gave is fully decrypted. The problem is not (yet) the decryption but your encryption. As documented des.NewTripleDESCipher returns a cipher.Block and cipher.Block.Encrypt encrypts as documented only the first block of the input data. Given that DES has a block size of 8 byte only the first 8 byte of the input data are encrypted, i.e. https://.

    This means in order to encrypt all data you must encrypt all blocks. Similar you need to decrypt all blocks when decrypting - but cipher.Block.Decrypt also decrypts only a single block.

    Apart from that DES is broken, so don't use it for something serious.

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

报告相同问题?