Go lang 3DES对加密字符串进行了部分解密



使用3des执行解密时,给定的加密文本未完全解密,不确定哪里出了问题,请帮助我完成解密错误

该代码可在昆虫游戏的Go Playground上获得,并运行

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

输出

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

给定的加密文本未完全解密

您提供的加密文本将完全解密。问题不在于解密,而在于加密。作为记录的des.NewTripleDESCipher返回cipher.Block,而cipher.Block.Encrypt仅加密作为记录的输入数据的第一块。假定DES具有8字节的块大小,则只有输入数据的前8字节被加密,即https://

这意味着为了加密所有数据,必须加密所有块。类似地,解密时需要解密所有块,但cipher.Block.Decrypt也只解密单个块。

除此之外,DES坏了,所以不要把它用于严重的事情。

最新更新