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

在输入字节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

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题