为了账号安全,请及时绑定邮箱和手机立即绑定

使用 dgrijalva/jwt-go golang 包解析 RS256 公钥时出现的问题

使用 dgrijalva/jwt-go golang 包解析 RS256 公钥时出现的问题

Go
MM们 2022-08-24 12:50:40
我有一对键,我用它生成了:ssh-keygen -t rsa -P "" -b 2048 -m PEM -f jwtRS256.keyssh-keygen -e -m PEM -f jwtRS256.key > jwtRS256.key.pub现在我正在尝试解析它们,它对私钥工作正常,但对公钥不起作用。jwt.ParseRSAPrivateKeyFromPEMjwt.ParseRSAPublicKeyFromPEM我得到的错误是:2021/04/07 13:34:22 errorPublic: asn1: structure error: tags don't match (16 vs {class:0 tag:2 length:257 isCompound:false}) {optional:false explicit:false application:false private:false defaultValue:<nil> tag:<nil> stringType:0 timeType:0 set:false omitEmpty:false} tbsCertificate @4我的代码:package mainimport (    "fmt"    "log"    "github.com/dgrijalva/jwt-go")var key = `-----BEGIN RSA PRIVATE KEY-----MIIEowIBAAKCAQEAuMVb3lrWKlmGIzTaJqtVJi2rPIy7/BkRKlAZ7Q1u0VlyOhzIcXq6zAGsh31uWeJBJFKrZdwA6b2LD5vscnuilHi0nfQZA5l+meQT9LJ7STnfJ7f81CniBIhj5g6dOva9o/ljrLCmRSE4MjJRl3LkydvrHNokzicOAvieq4BYgHVJ2DC7r7cSWrHeIiBIEBb1zAghc1OxtkFkxumva2gmywq0zB0VfzsYrtSpWT77qSNA+UEHH+UCahefkun1uaBcOrEIPUh/j95N3rFTAdIAzFvDqLvLdQbmm/NT78O7izSGvJ1K4rM2fSiecALSROZqOeJy3pf2v6Caqlp2gdDHUQIDAQABAoIBAEeOBrnhq7bS3KOdwC3hhCQ442ubhOFoQ8GDK8clwJjKbvYaV3W69cQzkcEWzjl46YlLipzzyla61LPCypq7Tob5B9lzwowmUWT/csr8o8oD42vaUMtJPQJMX4OkfTdsfpyV5AfokTuMVdr6qaZhFEEoLbEKud4sObzk023PUnbMTMyz2weQsmVLT9gm64K9GHzauoEDWGpr3Boz+mEKo9rhySeK73qXqJFFATl0IB50GpdKondere/XofLQ2kqUB2fFEWIeRXawFd4u-----END RSA PRIVATE KEY-----``func main() {    _, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(key))    if err != nil {        log.Println("errorPrivate:", err)    }    _, err = jwt.ParseRSAPublicKeyFromPEM([]byte(pubkey))    if err != nil {        log.Println("errorPublic:", err)    }    fmt.Println("Done")}我做错了什么?
查看完整描述

1 回答

?
蛊毒传说

TA贡献1895条经验 获得超3个赞

新浪网.ParseRSAPublicKeyFromPEM() 在内部调用 x509。ParsePKIXPublicKey() 方法,用于导入 X.509/SPKI 格式的 PEM 编码密钥。

X.509/SPKI 格式的 PEM 编码密钥可以使用选项 -e -m pkcs8 从带有 ssh-keygen 的私钥派生。目前,应用了选项 -e -m pem,该选项生成 PKCS#1 格式的公钥,该公钥无法由 处理。jwt.ParseRSAPublicKeyFromPEM()

如果使用 X.509/SPKI 格式的 PEM 编码密钥,则公钥导入工作正常:

package main


import (

    "fmt"

    "log"

    "github.com/dgrijalva/jwt-go"

)


var key = `-----BEGIN RSA PRIVATE KEY-----

MIIEowIBAAKCAQEAo+fW9kv2Y5Fjky8TQLK3rYtjqCKIEf0Yjm1lXTnkpWVBjO7x

EzB7HYBkHnIA19HEdzjeKLlLcJevOU6G3p+t8/5vVhciPnhKpwU6ZzrR0P3Q2toC

+KgtrnPHXpm8Py/HVwBxHWZ352NNa5J3dHv6a7B1k7IMAaBT053P99l1NQrBCVJf

kXckQAUOnsYk/PKFFufNhlu3nAd+eUl0Iv1IAWsUb5lHKkIOOWxnlreFN5gdCgL6

YxLwXRBV010zu/Y9zF8zdhSZXWoIvD/JpOvkKh9ym1VE0YzYkSW+m0XnTa4VMdZf

mWcQNKhYZdNg0TqwRco893fVeo756JzOHQ0KcQIDAQABAoIBADnxO4zWTcPlIc5m

VegJReWT4ScPDgtN7eBry+mpDatqoEGyNocSHHPRb5NTufiRr2J2OBMtbf3foZIg

sI0C5fvbdoB4rSJoY/unXX4gzQuUrsC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B

sV0tGy1CL+hkyk3EuUUDPapwXmSoA5P5eJqN16XiqSQCv7Cis0zr

-----END RSA PRIVATE KEY-----`


var pubkey = `-----BEGIN PUBLIC KEY-----

MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAo+fW9kv2Y5Fjky8TQLK3

rYtjqCKIEf0Yjm1lXTnkpWVBjO7xEzB7HYBkHnIA19HEdzjeKLlLcJevOU6G3p+t

8/5vVhciPnhKpwU6ZzrR0P3Q2toC+KgtrnPHXpm8Py/HVwBxHWZ352NNa5J3dHv6

a7B1k7IMAaBT053P99l1NQrBCVJfkXckQAUOnsYk/PKFFufNhlu3nAd+eUl0Iv1I

AWsUb5lHKkIOOWxnlreFN5gdCgL6YxLwXRBV010zu/Y9zF8zdhSZXWoIvD/JpOvk

Kh9ym1VE0YzYkSW+m0XnTa4VMdZfmWcQNKhYZdNg0TqwRco893fVeo756JzOHQ0K

cQIDAQAB

-----END PUBLIC KEY-----`


func main() {

    _, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(key))

    if err != nil {

        log.Println("errorPrivate:", err)

    }

    _, err = jwt.ParseRSAPublicKeyFromPEM([]byte(pubkey))

    if err != nil {

        log.Println("errorPublic:", err)

    }


    fmt.Println("Done")

}

请注意,dgrijalva/jwt-go 似乎没有被维护,s. #457,并且存在许多未解决的问题。


查看完整回答
反对 回复 2022-08-24
  • 1 回答
  • 0 关注
  • 298 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信