登录
首页 >  Golang >  Go问答

Go 语言将模数指数转换为 X.509 证书

来源:Golang技术栈

时间:2023-03-22 13:55:58 320浏览 收藏

哈喽!今天心血来潮给大家带来了《Go 语言将模数指数转换为 X.509 证书》,想必大家应该对Golang都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到golang,若是你正在学习Golang,千万别错过这篇文章~希望能帮助到你!

问题内容

我已经看过很多关于如何将 X.509 转换为指数模数的示例,但我没有看到相反的情况。

我从关键端点获得的值如下所示:

{
  "keys": [
    {
      "alg": "RS256",
      "e": "AQAB",
      "n": "ok6rvXu95337IxsDXrKzlIqw_I_zPDG8JyEw2CTOtNMoDi1QzpXQVMGj2snNEmvNYaCTmFf51I-EDgeFLLexr40jzBXlg72quV4aw4yiNuxkigW0gMA92OmaT2jMRIdDZM8mVokoxyPfLub2YnXHFq0XuUUgkX_TlutVhgGbyPN0M12teYZtMYo2AUzIRggONhHvnibHP0CPWDjCwSfp3On1Recn4DPxbn3DuGslF2myalmCtkujNcrhHLhwYPP-yZFb8e0XSNTcQvXaQxAqmnWH6NXcOtaeWMQe43PNTAyNinhndgI8ozG3Hz-1NzHssDH_yk6UYFSszhDbWAzyqw",
      "kid": "wyMwK4A6CL9Qw11uofVeyQ119XyX-xykymkkXygZ5OM",
      "kty": "RSA",
      "use": "sig"
    },
    {
      "alg": "RS256",
      "e": "AQAB",
      "n": "nXv6FSAcMjuanQ2hIIUb8Vkqe94t98kPh2T8-0j6-Jq8pOclgKdtVeIZcBE9F_XiuJvg4b6WVs-uvA-pS8mmMvQ21xU5Q_37Cojv8v_QlHWETHwEJdXXiY2Xq5LgXDSwEhhdDZHSMQYDuvhp_P6nl2LNqqUvJkjoFWcnn2btgSIUQROIaDdxtx7_2h4oUi5u11BGSF2SZZiEpDAKT08Htv3uwXdwDA6ll99fbi8X8RmH5oY_tIZTeIzu50qHxElPewoYO8QrJYsO9oFcCPMHGxYWjXQEa-QZYgo0wS9zRIkeJc5kshc4-9Uhv2DVIjk_-ofGlML9ieggGyillBKptw",
      "kid": "GRF55Lbzgg4sANCmER-sm55eX_qUOpY8UTptDmDG_6U",
      "kty": "RSA",
      "use": "sig"
    }
  ]
}

我认为这是关键:

ok6rvXu95337IxsDXrKzlIqw_I_zPDG8JyEw2CTOtNMoDi1QzpXQVMGj2snNEmvNYaCTmFf51I-EDgeFLLexr40jzBXlg72quV4aw4yiNuxkigW0gMA92OmaT2jMRIdDZM8mVokoxyPfLub2YnXHFq0XuUUgkX_TlutVhgGbyPN0M12teYZtMYo2AUzIRggONhHvnibHP0CPWDjCwSfp3On1Recn4DPxbn3DuGslF2myalmCtkujNcrhHLhwYPP-yZFb8e0XSNTcQvXaQxAqmnWH6NXcOtaeWMQe43PNTAyNinhndgI8ozG3Hz-1NzHssDH_yk6UYFSszhDbWAzyqw

var key = "ok6rvXu95337IxsDXrKzlIqw_I_zPDG8JyEw2CTOtNMoDi1QzpXQVMGj2snNEmvNYaCTmFf51I-EDgeFLLexr40jzBXlg72quV4aw4yiNuxkigW0gMA92OmaT2jMRIdDZM8mVokoxyPfLub2YnXHFq0XuUUgkX_TlutVhgGbyPN0M12teYZtMYo2AUzIRggONhHvnibHP0CPWDjCwSfp3On1Recn4DPxbn3DuGslF2myalmCtkujNcrhHLhwYPP-yZFb8e0XSNTcQvXaQxAqmnWH6NXcOtaeWMQe43PNTAyNinhndgI8ozG3Hz-1NzHssDH_yk6UYFSszhDbWAzyqw"

预期产出

我正在尝试像这样转换为 .PEM 格式

-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDfu6fJMNQNy13wCYvlIuKPBfBt
BHgeAeUbrn4JVmuAJGnQ2i08RizbWPzbckWQX8IXQJkYykCU1YJ+Y98tLPo/276c
zJLaFXhwe4qzocQ52SXSMTTTGN51CgYEAfNY4PJTU3gayahC0iYkV0CHuT8liVsEdV67X0DXMBpCAjNSwIDAQAB
-----END PUBLIC KEY----- 鈥�

如果有人能指出我在 GO 语言中执行此操作的正确方向,将不胜感激。我还将它放在 Go Lang 操场上,以便其他人可以使用它。

正确答案

您可以将值直接放入rsa.PublicKey可以按原样使用的 中。由于您想将其编码为 pem 编码的 x.509,您可以使用该x509.MarshalPKIXPublicKey函数来编组 der 块,并使用 pem 包对其进行编码。

将示例中的第一个键解组为jwk

// decode the base64 bytes for n
nb, err := base64.RawURLEncoding.DecodeString(jwk["n"])
if err != nil {
    log.Fatal(err)
}

e := 65537
// The default exponent is usually 65537, so just compare the
// base64 for [1,0,1] or [0,1,0,1]
if jwk["e"] != "AQAB" && jwk["e"] != "AAEAAQ" {
    // still need to decode the big-endian int
    log.Fatal("need to deocde e:", jwk["e"])
}

pk := &rsa.PublicKey{
    N: new(big.Int).SetBytes(nb),
    E: e,
}

der, err := x509.MarshalPKIXPublicKey(pk)
if err != nil {
    log.Fatal(err)
}

block := &pem.Block{
    Type:  "RSA PUBLIC KEY",
    Bytes: der,
}

var out bytes.Buffer
pem.Encode(&out, block)
fmt.Println(out.String())

这将输出:

-----BEGIN RSA PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAok6rvXu95337IxsDXrKz
lIqw/I/zPDG8JyEw2CTOtNMoDi1QzpXQVMGj2snNEmvNYaCTmFf51I+EDgeFLLex
r40jzBXlg72quV4aw4yiNuxkigW0gMA92OmaT2jMRIdDZM8mVokoxyPfLub2YnXH
Fq0XuUUgkX/TlutVhgGbyPN0M12teYZtMYo2AUzIRggONhHvnibHP0CPWDjCwSfp
3On1Recn4DPxbn3DuGslF2myalmCtkujNcrhHLhwYPP+yZFb8e0XSNTcQvXaQxAq
mnWH6NXcOtaeWMQe43PNTAyNinhndgI8ozG3Hz+1NzHssDH/yk6UYFSszhDbWAzy
qwIDAQAB
-----END RSA PUBLIC KEY-----

https://play.golang.org/p/Ch6Nr9F6Ik

以上就是《Go 语言将模数指数转换为 X.509 证书》的详细内容,更多关于golang的资料请关注golang学习网公众号!

声明:本文转载于:Golang技术栈 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表