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.
Asked
Active
Viewed 1.4k times
6

phil o.O
- 406
- 2
- 4
- 20
-
1. Do not use DES, it is not secure and has been superseded by AES whioch is no harder to use. 2. Do use a random IV, just prefix theIV to the encrypted data for use on decryption (the IV does not need to be secret). It is important to use a different IV when the same encryption key is used for multiple messages. 3. Consider, the point of encryption is generally to create security, improperly used encryption will not provide secure. – zaph Jan 10 '17 at 22:38
-
1It is not clear that you want to decrypt a 12 character encrypted string and specify a unique key and a unique IV. The same key and IV that was used for encryption must be used for decryption. The question could be made more clear and provide more information on exactly what you are trying to accomplish. – zaph Jan 10 '17 at 22:41
-
For starters you're not decoding the base64 when decrypting, which is why the data size is wrong. – JimB Jan 10 '17 at 22:42
-
@zaph Thanks for your feedback and insight. My code is a local prototype and not production code. – phil o.O Jan 11 '17 at 18:38
-
@JimB Whoops! Thank you for catching that. – phil o.O Jan 11 '17 at 18:38
2 Answers
8
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))
}

shivendra pratap singh
- 1,318
- 1
- 13
- 18
-
yep! Thanks! I just changed one line in main. I changed `fmt.Println(string(cryptoText))` to `fmt.Println(base64.URLEncoding.EncodeToString(cryptoText))`. – phil o.O Jan 11 '17 at 18:27
-
It's better to use this padding/unpadding code: https://github.com/go-web/tokenizer/blob/master/pkcs7.go – Artjom B. Apr 14 '17 at 17:18
1
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)
}

xucongco
- 51
- 2