dongyi2534 2016-11-03 17:30
浏览 623
已采纳

在输入字节0处出现golang非法base64数据

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
", 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
", encrypted_byte)
    result, err := decrypt(key, encrypted_byte)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("result %s
", 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条回答 默认 最新

  • doupingyun73833 2016-11-04 14:34
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!