我有一个go测试程序,可以从文件中读取加密内容并将其解密,但是它会得到如下输出:

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

如果我将加密的内容硬编码到golang字符串变量中,则可以对其解密。我在这里想念什么?我在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() { //<==========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
}

最佳答案

您需要取消报价从扫描仪返回的encrypted_text
这是一个minimal example

修改scanner.Scan() if块,使其看起来像这样

    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)
    }



我猜你的文件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取消引用该字符串

另外,看看This SO question

关于encryption - 在输入字节0处出现golang非法base64数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40407819/

10-09 01:18