2018-11-16 05:52
浏览 70

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 (

func main() {

    // Mimimum Key Size of Length 24
    key := "mysecretPasswordkeySiz24"
    plainText := ""
    ct := EncryptTripleDES([]byte(key),plainText)
    fmt.Printf("Original Text:  %s
    fmt.Printf("3DES Encrypted Text:  %s
", 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)

    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)
        plain := make([]byte, len(ciphertext))
        c.Decrypt(plain, ciphertext)
        s := string(plain[:])
        fmt.Printf("3DES Decrypyed Text:  %s
", s)

The output

Original Text:
3DES Encrypted Text:  a6e5215154bf86d000000000000000000000
3DES Decrypyed Text:  https://
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

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

    点赞 评论