英文:
golang illegal base64 data at input byte 0
问题
我有一个用于从文件中读取加密内容并解密的Go测试程序,但是它输出以下内容:
输入字节0处的非法base64数据
如果我在一个Go字符串变量中硬编码加密内容,它可以正常解密。我在这里漏掉了什么?我在stackoverflow上搜索了类似的错误,有类似的报告,但不是我遇到的完全相同的问题。测试代码如下:
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"io"
"bufio"
"os"
"log"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
plaintext := []byte("textstring")
key := []byte("a very very very very very secre")
fmt.Printf("%s\n", plaintext)
fh, err := os.Open("./test.txt")
check(err)
scanner := bufio.NewScanner(fh)
var encrypted_text string
if scanner.Scan() { //<==========从文件中读取
encrypted_text = scanner.Text()
fmt.Println("encrypted_text from file: ", encrypted_text)
} else { //<===========在这里硬编码
encrypted_text = "\xf2F\xbc\x15\x9d\xaf\xceϘ\xa3L(>%\xa2\x94\x03_\x99\u007fG\xd8\v\xbf\t#u\xf8:\xc0D\u007f"
fmt.Println("encrypted_text hard coded: ", encrypted_text)
}
encrypted_byte := []byte(encrypted_text)
fmt.Printf("encrypted_byte: %s\n", encrypted_byte)
result, err := decrypt(key, encrypted_byte)
if err != nil {
log.Fatal(err)
}
fmt.Printf("result %s\n", string(result))
}
func encrypt(key, text []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
b := base64.StdEncoding.EncodeToString(text)
ciphertext := make([]byte, aes.BlockSize+len(b))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
cfb := cipher.NewCFBEncrypter(block, iv)
cfb.XORKeyStream(ciphertext[aes.BlockSize:], []byte(b))
return ciphertext, nil
}
func decrypt(key, text []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
if len(text) < aes.BlockSize {
return nil, errors.New("ciphertext too short")
}
iv := text[:aes.BlockSize]
text = text[aes.BlockSize:]
cfb := cipher.NewCFBDecrypter(block, iv)
cfb.XORKeyStream(text, text)
data, err := base64.StdEncoding.DecodeString(string(text))
if err != nil {
return nil, err
}
return data, nil
}
英文:
I have a go test program to read encrypted content from file and decrypt it, but it get output like below:
illegal base64 data at input byte 0
if I hard code the encrypted content in a golang string variable, it can decrypt it fine. what I am missing here? I searched similar error in stackoverflow, there is similar report, but not exact the same problem I have. the test code like below:
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"io"
"bufio"
"os"
"log"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
plaintext := []byte("textstring")
key := []byte("a very very very very very secre")
fmt.Printf("%s\n", plaintext)
fh, err := os.Open("./test.txt")
check(err)
scanner := bufio.NewScanner(fh)
var encrypted_text string
if scanner.Scan() { //<==========READ FROM FILE
encrypted_text = scanner.Text()
fmt.Println("encrypted_text from file: ", encrypted_text)
} else { //<===========HARD CODE HERE
encrypted_text = "\xf2F\xbc\x15\x9d\xaf\xceϘ\xa3L(>%\xa2\x94\x03_\x99\u007fG\xd8\v\xbf\t#u\xf8:\xc0D\u007f"
fmt.Println("encrypted_text hard coded: ", encrypted_text)
}
encrypted_byte := []byte(encrypted_text)
fmt.Printf("encrypted_byte: %s\n", encrypted_byte)
result, err := decrypt(key, encrypted_byte)
if err != nil {
log.Fatal(err)
}
fmt.Printf("result %s\n", string(result))
}
func encrypt(key, text []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
b := base64.StdEncoding.EncodeToString(text)
ciphertext := make([]byte, aes.BlockSize+len(b))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
cfb := cipher.NewCFBEncrypter(block, iv)
cfb.XORKeyStream(ciphertext[aes.BlockSize:], []byte(b))
return ciphertext, nil
}
func decrypt(key, text []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
if len(text) < aes.BlockSize {
return nil, errors.New("ciphertext too short")
}
iv := text[:aes.BlockSize]
text = text[aes.BlockSize:]
cfb := cipher.NewCFBDecrypter(block, iv)
cfb.XORKeyStream(text, text)
data, err := base64.StdEncoding.DecodeString(string(text))
if err != nil {
return nil, err
}
return data, nil
}
答案1
得分: 3
你需要对从扫描仪返回的encrypted_text
进行取消引用。这是一个最小示例。
将你的scanner.Scan()
代码块修改为以下形式:
if scanner.Scan() { //<==========从文件中读取
encrypted_text = scanner.Text()
fmt.Println("从文件中读取的encrypted_text: ", encrypted_text)
// 取消引用,不要忘记导入strconv包!
encrypted_text, err := strconv.Unquote(`"` + encrypted_text + `"`)
check(err)
}
为什么需要取消引用?
我猜测你的文件test.txt
包含原始字符串:
\xf2F\xbc\x15\x9d\xaf\xceQ\xa3L(>%\xa2\x94\x03_\x99\u007fG\xd8\v\xbf\t#u\xf8:\xc0D\u007f
当扫描仪从文件中读取时,它将\
读取为\
。
然而,当你在代码中硬编码时,像这样:
encrypted_text = "\xf2F\xbc\x15\x9d\xaf\xceϘ\xa3L(>%\xa2\x94\x03_\x99\u007fG\xd8\v\xbf\t#u\xf8:\xc0D\u007f"
你使用的是双引号"”,所以
`不是\
。它会解释转义序列。如果你使用反引号,如下所示:
encrypted_text = `\xf2F\xbc\x15\x9d\xaf\xceϘ\xa3L(>%\xa2\x94\x03_\x99\u007fG\xd8\v\xbf\t#u\xf8:\xc0D\u007f`
你将面临相同的问题。
解决方案是使用strconv.Unquote对该字符串进行取消引用。
另外,参考这个Stack Overflow问题。
英文:
You need to unquote the encrypted_text
returned from the scanner.
Here's a minimal example
Modify your scanner.Scan()
if block to look like this
if scanner.Scan() { //<==========READ FROM FILE
encrypted_text = scanner.Text()
fmt.Println("encrypted_text from file: ", encrypted_text)
// Unquoting, don't forget to import strconv !
encrypted_text, err := strconv.Unquote(`"` + encrypted_text + `"`)
check(err)
}
> why you need to unquote
I'm guessing your file test.txt
contains the raw string
\xf2F\xbc\x15\x9d\xaf\xceQ\xa3L(>%\xa2\x94\x03_\x99\u007fG\xd8\v\xbf\t#u\xf8:\xc0D\u007f
When scanner reads this from the file, it is reading a \
as a \
.
However, when you hardcode it in your code like this
encrypted_text = "\xf2F\xbc\x15\x9d\xaf\xceϘ\xa3L(>%\xa2\x94\x03_\x99\u007fG\xd8\v\xbf\t#u\xf8:\xc0D\u007f"
You are using double quotes "
, so a \
isn't a \
. It interprets the escape sequences. If you were to use a backquote as follows
encrypted_text = `\xf2F\xbc\x15\x9d\xaf\xceϘ\xa3L(>%\xa2\x94\x03_\x99\u007fG\xd8\v\xbf\t#u\xf8:\xc0D\u007f`
you would face the same issue.
The solution is to unquote this string using strconv.Unquote
Also, take a look at This SO question
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论