goencryptiondes3des

Go lang 3DES partially decrypted the encrypted string


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\n",plainText)
    fmt.Printf("3DES Encrypted Text:  %s\n", 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\n", s)
}

The output

Original Text:  https://8gwifi.org
3DES Encrypted Text:  a6e5215154bf86d000000000000000000000
3DES Decrypyed Text:  https://

Solution

  • 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.