golang在输入字节0处的base64数据非法。

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

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 (
&quot;crypto/aes&quot;
&quot;crypto/cipher&quot;
&quot;crypto/rand&quot;
&quot;encoding/base64&quot;
&quot;errors&quot;
&quot;fmt&quot;
&quot;io&quot;
&quot;bufio&quot;
&quot;os&quot;
&quot;log&quot;
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
plaintext := []byte(&quot;textstring&quot;)
key := []byte(&quot;a very very very very very secre&quot;)
fmt.Printf(&quot;%s\n&quot;, plaintext)
fh, err := os.Open(&quot;./test.txt&quot;)
check(err)
scanner := bufio.NewScanner(fh)
var encrypted_text string
if scanner.Scan() { //&lt;==========READ FROM FILE
encrypted_text = scanner.Text()
fmt.Println(&quot;encrypted_text from file: &quot;, encrypted_text)
} else { //&lt;===========HARD CODE HERE
encrypted_text  = &quot;\xf2F\xbc\x15\x9d\xaf\xceϘ\xa3L(&gt;%\xa2\x94\x03_\x99\u007fG\xd8\v\xbf\t#u\xf8:\xc0D\u007f&quot;
fmt.Println(&quot;encrypted_text hard coded: &quot;, encrypted_text)
}
encrypted_byte  := []byte(encrypted_text)
fmt.Printf(&quot;encrypted_byte: %s\n&quot;, encrypted_byte)
result, err := decrypt(key, encrypted_byte)
if err != nil {
log.Fatal(err)
}
fmt.Printf(&quot;result %s\n&quot;, 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) &lt; aes.BlockSize {
return nil, errors.New(&quot;ciphertext too short&quot;)
}
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() { //&lt;==========从文件中读取
    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() { //&lt;==========READ FROM FILE
encrypted_text = scanner.Text()
fmt.Println(&quot;encrypted_text from file: &quot;, encrypted_text)
// Unquoting, don&#39;t forget to import strconv !
encrypted_text, err := strconv.Unquote(`&quot;` + encrypted_text + `&quot;`)
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(&gt;%\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  = &quot;\xf2F\xbc\x15\x9d\xaf\xceϘ\xa3L(&gt;%\xa2\x94\x03_\x99\u007fG\xd8\v\xbf\t#u\xf8:\xc0D\u007f&quot;

You are using double quotes &quot;, 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(&gt;%\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

huangapple
  • 本文由 发表于 2016年11月4日 01:30:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/40407819.html
匿名

发表评论

匿名网友

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

确定