Golang:我如何使用DES,CBC和PKCS7进行解密? [英] Golang: How do I decrypt with DES, CBC, and PKCS7?
本文介绍了Golang:我如何使用DES,CBC和PKCS7进行解密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
panic:crypto / cipher:input not full blocks在解密期间
。 解决方案
Buddy的工作完全正常。
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 (key,iv,cipherText)[b]
blockMode.CryptBlocks(cryted,origData)
return cryted,nil
}
func DesDecryption(key,iv,cipherText []字节)([]字节,错误){
block,err:= des.NewCipher(key)
if err!= nil {
return nil,err
}
blockMode:= cipher.NewCBCDecrypter(block,iv)
orig Data:= 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([] (字节(填充)},填充)
返回附加(src,padtext ...)
}
func PKCS5UnPadding(src [] byte)[] byte {
length:= len(src)
unpadding:= int(src [length-1])$ b $ b 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))
}
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.
解决方案
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))
}
这篇关于Golang:我如何使用DES,CBC和PKCS7进行解密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文