go - 如何解码 PEM 编码的 PKIX 公钥?

标签 go pem

Go的official documentation中给出的例子x509.ParsePKIXPublicKey 遇到运行时错误,因为 pem.Decode() 返回的 Blocknil。 (Playground example)

但是,official documentation 中的示例pem.Decode() 运行得很好。 ( Playground example )

我发现这两个示例之间的唯一区别是公钥的初始化方式以及它们在 pem.Decode() 中的使用方式。

第一个例子:

const pubPEM = `
    -----BEGIN PUBLIC KEY-----
    ...
    -----END PUBLIC KEY-----`
block, _ := pem.Decode([]byte(pubPEM))

第二个例子:

var pubPEMData = []byte(`
    -----BEGIN PUBLIC KEY-----
    ...
    -----END PUBLIC KEY-----`)
block, rest := pem.Decode(pubPEMData)

我已经编辑了第一个示例,使其以与第二个示例相同的方式完成,但这并没有改变结果。 ( Playground example )

是什么导致第一个失败但第二个没有?

最佳答案

你的字符串中有空格,删除它们,它将起作用:fixed example

关于go - 如何解码 PEM 编码的 PKIX 公钥?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47820459/

相关文章:

sql - 戈朗 : Ping succeed the second time even if database is down

postgresql - 如何在 PostgreSQL 中匹配模式时转义字符串

openssl - 如何续订自签名 openssl PEM 证书

go - 如何设置默认选择选项以使用 Golang 选择标签?

go - mgo 中的模型关系

json.Unmarshal 没有正确解码到内部接口(interface){}

openssl - 从 ExpKey + Modkey 到 PEM

ios - 如何在 ios 中生成公钥/私钥对,swift

ssl - 从 openssl s_server 查看 pem 格式的客户端证书

ssh - AWS pem key 不起作用