duanfang2708 2017-12-07 10:51
浏览 649
已采纳

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

I'm using this example, however it is slightly outdated. So, for, example SignedString(key), I guess, receives *rsa.PublicKey instead of []byte in case of RS256 signing method. I generated my private key using

openssl genrsa -out key.rsa 1024

And now trying to parse it:

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)
}

But I get 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 in response.

Is my key generating command somehow wrong? What should I read to be able to debug this? What asn1 has to do with it? Key has some particular syntax?

  • 写回答

1条回答 默认 最新

  • duanbanfei2875 2017-12-07 11:24
    关注

    You are passing a RSA PRIVATE KEY but calling jwt.ParseRSAPublicKeyFromPEM. You should be calling jwt.ParseRSAPrivateKeyFromPEM instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用Multisim设计汽车尾灯控制电路
  • ¥15 关于#java#的问题,请各位专家解答!(开发工具-eclipse)
  • ¥100 求用matlab求解上述微分方程的程序代码
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位