如何为JWT签名生成正确的RSA密钥?

时间:2017-12-07 10:51:22

标签: go openssl jwt asn.1

我正在使用this example,但它稍微过时了。因此,对于示例SignedString(key),我猜,在RS256签名方法的情况下,接收*rsa.PublicKey而不是[]byte。我使用

生成了我的私钥
openssl genrsa -out key.rsa 1024

现在试图解析它:

package main

import (
    "fmt"
    "github.com/dgrijalva/jwt-go"
)

func main() {
    pem := []byte(`-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQCycI9LZT8qM/zCGH0CNxAi7gy7w1YjqZN1N8yFfbmv9toVC82S
bZDv8NVYVlFNhUbtUFg77TYhHmyPpr4hKqvPrVc0rqVFe4ggQYdXovDgLZhO1MxB
i4vw9tLRlAK8TYNFqaFiX6lFgRp8trd01GFhkBJMzZCMmBQFkcEgWqT6MQIDAQAB
AoGBAKSru5GYnfIUh+2XK8YfioSxqo6cdaHznGFQXS3UGMiKxjcl7VB0TI0hc8FR
5zXjCNF4k/S5tHpb2VO4TT3OYz55/JEOGAXvIMW2UYtL4TwR3+46TRJ95KKowq+c
3JndjmGjUbvWawtbNklkmgoEkLI8wYu9yss9293GBnff14ABAkEA1scxu0svrIZ1
R5YmpXOfrtGwsjbdowGNxZTedWlbJukBSNPSIpuUvkzF2PXlPBS80p5yxbYlp1gg
UhsfXWJwIQJBANSv7lEuHV0QnVVoYVtNijwEHIi7iG7xu7hDQ7bK/kwWV6wqnvfP
PZopZPDXCRlnxbubo0/RJz4LsaJflDJKiBECQC/3FFgrll4FOTPfiInypHq6QafL
MPwyYKL7eCLogH7UEoFdLNSlwjYtlxG4+JTOXMz8rc0RvJ7+w1kSzZJhZYECQHMU
DhqWnQSx7K89nw/L0LPQda/16Eg/O5bjrkvFYfrBTAa3uPPUijxTkW5Oq3m3kGGG
QML7JASKRz7YAFOFyEECQQCUBNONRYMMzzr4llGOcKISXJtewlOPTHGP++OiHWMB
rPzZDyi5ONNgySF8t/QnJMTRpRQ5axi0UhojL/OzUt/w
-----END RSA PRIVATE KEY-----`)

    key, err := jwt.ParseRSAPublicKeyFromPEM(pem)
    fmt.Println(key, err)
}

但我得到asn1: structure error: tags don't match (16 vs {class:0 tag:2 length:1 isCompound:false}) {optional:false explicit:false application:false defaultValue:<nil> tag:<nil> stringType:0 timeType:0 set:false omitEmpty:false} tbsCertificate @2作为回应。

我的密钥生成命令是否有问题?我应该阅读什么才能调试这个? asn1与它有什么关系? Key有一些特殊的语法?

1 个答案:

答案 0 :(得分:1)

您正在传递RSA PRIVATE KEY,但正在呼叫jwt.ParseRSAPublicKeyFromPEM。你应该打电话给jwt.ParseRSAPrivateKeyFromPEM