简体   繁体   English

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

[英]How do I decode a PEM-encoded PKIX public key?

The example given in Go's official documentation for x509.ParsePKIXPublicKey encounters a runtime error because the Block that pem.Decode() returns is nil . Go的官方文档 x509.ParsePKIXPublicKey给出的示例遇到运行时错误,因为pem.Decode()返回的Blocknil ( Playground example ) 游乐场示例

However, the example in the official documentation for pem.Decode() runs just fine. 但是, pem.Decode() 官方文档中的pem.Decode()运行得很好。 ( Playground example ) 游乐场示例

The only difference I've spotted between the two examples is the the way the public key is initialized and they way it is used in pem.Decode() . 我在两个示例之间发现的唯一区别是公钥的初始化方式以及它们在pem.Decode()使用方式。

First example: 第一个例子:

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

Second example: 第二个例子:

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

I've edited the first example so that it is done in the same way as the second, but this did not change the outcome. 我已经编辑了第一个示例,以便以与第二个示例相同的方式进行操作,但这并没有改变结果。 ( Playground example ) 游乐场示例

What is causing the first to fail but not the second? 是什么导致第一个失败而不是第二个失败?

您的字符串中有空格,将其删除即可使用: 固定示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM