duankangpazhuo0347 2016-12-13 17:22
浏览 270
已采纳

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

I have seen a lot of examples of how to convert a X.509 to Exponent Modulus, but I have not seen the reverse.

The values I have from my key endpoint look like this:

{"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"}]}

I think this is the Key:

ok6rvXu95337IxsDXrKzlIqw_I_zPDG8JyEw2CTOtNMoDi1QzpXQVMGj2snNEmvNYaCTmFf51I-EDgeFLLexr40jzBXlg72quV4aw4yiNuxkigW0gMA92OmaT2jMRIdDZM8mVokoxyPfLub2YnXHFq0XuUUgkX_TlutVhgGbyPN0M12teYZtMYo2AUzIRggONhHvnibHP0CPWDjCwSfp3On1Recn4DPxbn3DuGslF2myalmCtkujNcrhHLhwYPP-yZFb8e0XSNTcQvXaQxAqmnWH6NXcOtaeWMQe43PNTAyNinhndgI8ozG3Hz-1NzHssDH_yk6UYFSszhDbWAzyqw

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

If anyone can point me in the right direction to do this in the GO language would be appreciated. I'll also put it on the Go Lang playground so others can use it.

  • 写回答

1条回答 默认 最新

  • doumeng3080 2016-12-13 23:27
    关注

    You can put the values directly into an rsa.PublicKey, which can be used as is. Since you want to encode that as a pem encoded x.509, you can use the x509.MarshalPKIXPublicKey function to marshal the der block, and encode that with the pem package.

    After unmarshaling the first key in your example to 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())
    

    Which will output:

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

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

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题