无法解密二进制文件

Unable to decrypt binary file

我一直在尝试使用 GPG public 密钥和 go 的 openpgp 库来加密 docx 文件。它加密文档,但我无法使用我的私钥解密它。

已经尝试对纯文本文件执行相同的操作并且解密没有任何问题。

我在这里错过了什么?

package main

import (
    "golang.org/x/crypto/openpgp"
    "bytes"
    "io/ioutil"
    "fmt"
    "os"
)

func main() {
    entitylist, _ := openpgp.ReadArmoredKeyRing(bytes.NewBufferString(...))

    buf := new(bytes.Buffer)
    w, _ := openpgp.Encrypt(buf, entitylist, nil, nil, nil)
    b, _ := ioutil.ReadFile("in.docx")

    w.Write(b)
    w.Close()

    bts, _ := ioutil.ReadAll(buf)
    ioutil.WriteFile("out.gpg", bts, os.ModePerm)
}

抱歉,大家慢慢来,Encode 函数似乎接受 FileHints 结构,因此通过二进制 from 解决了问题

w, _ := openpgp.Encrypt(buf, entitylist, nil, &openpgp.FileHints{IsBinary: true}, nil)

有关 FileHints

的更多详细信息
// FileHints contains metadata about encrypted files. This metadata is, itself,
// encrypted.
type FileHints struct {
    // IsBinary can be set to hint that the contents are binary data.
    IsBinary bool
    // FileName hints at the name of the file that should be written. It's
    // truncated to 255 bytes if longer. It may be empty to suggest that the
    // file should not be written to disk. It may be equal to "_CONSOLE" to
    // suggest the data should not be written to disk.
    FileName string
    // ModTime contains the modification time of the file, or the zero time if not applicable.
    ModTime time.Time
}

谢谢。