Golang:如何使用DES、CBC和PKCS7进行解密?

huangapple go评论89阅读模式
英文:

Golang: How do I decrypt with DES, CBC, and PKCS7?

问题

目前正在努力弄清楚为什么我的解密方法不起作用。我使用了DES、CBC和PKCS7Padding来加密我的字符串。我的当前代码在解密过程中输出panic: crypto/cipher: input not full blocks

英文:

Currently trying to figure out why my decryption method is not working. I used DES, CBC, and PKCS7Padding to encrypt my string. My current code outputs panic: crypto/cipher: input not full blocks during decryption.

答案1

得分: 8

伙计,它完全正常工作。

package main

import (
    "bytes"
    "crypto/des"
    "crypto/cipher"
    "fmt"
)

func DesEncryption(key, iv, plainText []byte) ([]byte, error) {

    block, err := des.NewCipher(key)

    if err != nil {
        return nil, err
    }

    blockSize := block.BlockSize()
    origData := PKCS5Padding(plainText, blockSize)
    blockMode := cipher.NewCBCEncrypter(block, iv)
    cryted := make([]byte, len(origData))
    blockMode.CryptBlocks(cryted, origData)
    return cryted, nil
}

func DesDecryption(key, iv, cipherText []byte) ([]byte, error) {

    block, err := des.NewCipher(key)

    if err != nil {
        return nil, err
    }

    blockMode := cipher.NewCBCDecrypter(block, iv)
    origData := make([]byte, len(cipherText))
    blockMode.CryptBlocks(origData, cipherText)
    origData = PKCS5UnPadding(origData)
    return origData, nil
}

func PKCS5Padding(src []byte, blockSize int) []byte {
    padding := blockSize - len(src)%blockSize
    padtext := bytes.Repeat([]byte{byte(padding)}, padding)
    return append(src, padtext...)
}

func PKCS5UnPadding(src []byte) []byte {
    length := len(src)
    unpadding := int(src[length-1])
    return src[:(length - unpadding)]
}


func main() {
    originalText := "sysys"
    fmt.Println(originalText)
    mytext := []byte(originalText)

    key := []byte{0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC }
    iv := []byte{0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC }
    

    cryptoText,_ := DesEncryption(key, iv, mytext)
    fmt.Println(string(cryptoText))
    decryptedText,_ := DesDecryption(key, iv, cryptoText)
    fmt.Println(string(decryptedText))

}
英文:

Buddy it's work completely fine.

package main
import (
"bytes"
"crypto/des"
"crypto/cipher"
"fmt"
)
func DesEncryption(key, iv, plainText []byte) ([]byte, error) {
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
origData := PKCS5Padding(plainText, blockSize)
blockMode := cipher.NewCBCEncrypter(block, iv)
cryted := make([]byte, len(origData))
blockMode.CryptBlocks(cryted, origData)
return cryted, nil
}
func DesDecryption(key, iv, cipherText []byte) ([]byte, error) {
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
blockMode := cipher.NewCBCDecrypter(block, iv)
origData := make([]byte, len(cipherText))
blockMode.CryptBlocks(origData, cipherText)
origData = PKCS5UnPadding(origData)
return origData, nil
}
func PKCS5Padding(src []byte, blockSize int) []byte {
padding := blockSize - len(src)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(src, padtext...)
}
func PKCS5UnPadding(src []byte) []byte {
length := len(src)
unpadding := int(src[length-1])
return src[:(length - unpadding)]
}
func main() {
originalText := "sysys"
fmt.Println(originalText)
mytext := []byte(originalText)
key := []byte{0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC }
iv := []byte{0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC }
cryptoText,_ := DesEncryption(key, iv, mytext)
fmt.Println(string(cryptoText))
decryptedText,_ := DesDecryption(key, iv, cryptoText)
fmt.Println(string(decryptedText))
}

答案2

得分: 1

你可以这样做:

package controllers

import (
	"bytes"
	"crypto/aes"
	"crypto/cipher"
	"encoding/hex"
	"fmt"
)

func PKCS7Padding(ciphertext []byte) []byte {
	padding := aes.BlockSize - len(ciphertext)%aes.BlockSize
	padtext := bytes.Repeat([]byte{byte(padding)}, padding)
	return append(ciphertext, padtext...)
}

func PKCS7UnPadding(plantText []byte) []byte {
	length := len(plantText)
	unpadding := int(plantText[length-1])
	return plantText[:(length - unpadding)]
}

func OpensslDecrypt(keyStr string, ivStr string, text string) string {

	key, _ := hex.DecodeString(keyStr)
	iv, _ := hex.DecodeString(ivStr)
	ciphertext, _ := hex.DecodeString(text)

	block, err := aes.NewCipher(key)
	if err != nil {
		panic(err)
	}
	plaintext := make([]byte, len(ciphertext))
	mode := cipher.NewCBCDecrypter(block, iv)
	mode.CryptBlocks(plaintext, ciphertext)

	plaintext = PKCS7UnPadding(plaintext)
	return fmt.Sprintf("%s\n", plaintext)
}

func OpensslEncrypt(keyStr string, ivStr string, text string) string {

	plaintext := []byte(text)
	key, _ := hex.DecodeString(keyStr)
	iv, _ := hex.DecodeString(ivStr)

	plaintext = PKCS7Padding(plaintext)
	ciphertext := make([]byte, len(plaintext))
	block, err := aes.NewCipher(key)
	if err != nil {
		panic(err)
	}

	mode := cipher.NewCBCEncrypter(block, iv)
	mode.CryptBlocks(ciphertext, plaintext)

	return fmt.Sprintf("%x\n", ciphertext)
}

这是一个Go语言的代码示例,包含了一些加密和解密函数。它使用了AES加密算法和CBC模式。其中,OpensslDecrypt函数用于解密数据,OpensslEncrypt函数用于加密数据。这些函数依赖于PKCS7PaddingPKCS7UnPadding函数,用于进行填充和去填充操作。你可以根据需要使用这些函数进行数据的加密和解密。

英文:

you can do like this

package controllers
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"fmt"
)
func PKCS7Padding(ciphertext []byte) []byte {
padding := aes.BlockSize - len(ciphertext) % aes.BlockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func PKCS7UnPadding(plantText []byte) []byte {
length   := len(plantText)
unpadding := int(plantText[length-1])
return plantText[:(length - unpadding)]
}
func OpensslDecrypt(keyStr string, ivStr string, text string) string{
key, _ := hex.DecodeString(keyStr);
iv, _ := hex.DecodeString(ivStr);
ciphertext, _ := hex.DecodeString(text);
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
plaintext := make([]byte,len(ciphertext))
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(plaintext, ciphertext)
plaintext = PKCS7UnPadding(plaintext)
return fmt.Sprintf("%s\n", plaintext)
}
func OpensslEncrypt(keyStr string, ivStr string, text string) string{
plaintext := []byte(text)
key, _ := hex.DecodeString(keyStr);
iv, _ := hex.DecodeString(ivStr);
plaintext = PKCS7Padding(plaintext);
ciphertext := make([]byte,len(plaintext))
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(ciphertext, plaintext)
return fmt.Sprintf("%x\n", ciphertext)
}

huangapple
  • 本文由 发表于 2017年1月11日 06:10:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/41579325.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定