Golang 3DES 部分解密加密字符串
Go lang 3DES partially decrypted the encrypted string
使用3des解密时给定的密文没有完全解密,不知道哪里出错了,帮我完成解密错误
Insection 和 运行
的代码位于 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: %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)
}
输出
Original Text: https://8gwifi.org
3DES Encrypted Text: a6e5215154bf86d000000000000000000000
3DES Decrypyed Text: https://
the given encrypted text is not fully decrypted
您提供的密文已完全解密。问题不在于(还)解密而是你的加密。如记录的那样,des.NewTripleDESCipher
returns cipher.Block
和 cipher.Block.Encrypt
只加密输入数据的第一个块。鉴于 DES 的块大小为 8 字节,只有输入数据的前 8 字节被加密,即 https://
。
这意味着为了加密所有数据,您必须加密所有块。类似的,你在解密时需要解密所有的块——但是cipher.Block.Decrypt
也只解密一个块。
除此之外,DES 已损坏,因此请不要将其用于严重的事情。
使用3des解密时给定的密文没有完全解密,不知道哪里出错了,帮我完成解密错误
Insection 和 运行
的代码位于 Go Playgroundpackage 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)
}
输出
Original Text: https://8gwifi.org
3DES Encrypted Text: a6e5215154bf86d000000000000000000000
3DES Decrypyed Text: https://
the given encrypted text is not fully decrypted
您提供的密文已完全解密。问题不在于(还)解密而是你的加密。如记录的那样,des.NewTripleDESCipher
returns cipher.Block
和 cipher.Block.Encrypt
只加密输入数据的第一个块。鉴于 DES 的块大小为 8 字节,只有输入数据的前 8 字节被加密,即 https://
。
这意味着为了加密所有数据,您必须加密所有块。类似的,你在解密时需要解密所有的块——但是cipher.Block.Decrypt
也只解密一个块。
除此之外,DES 已损坏,因此请不要将其用于严重的事情。