dongzilu0178 2014-08-07 09:56
浏览 357
已采纳

从Golang中的模数和指数创建公钥

I fetched from the first certificate on: https://www.googleapis.com/oauth2/v2/certs the 'n' and 'e' key values. Is there a package in Go that can build a public key with 'n' and 'e'? I don't know how it's done using the crypto/rsa package. Some code would be precious. Thank You.

  • 写回答

1条回答 默认 最新

  • douzai1074 2014-08-07 11:39
    关注

    The rsa package has a PublicKey type with fields N and E. It should be pretty straightforward to decode the parts as described in the JWA draft.

    Here is some quickly hacked code (Playground):

    package main
    
    import (
        "bytes"
        "crypto/rsa"
        "encoding/base64"
        "encoding/binary"
        "fmt"
        "math/big"
    )
    
    func main() {
        nStr := "AN+7p8kw1A3LXfAJi+Ui4o8F8G0EeB4B5RuufglWa4AkadDaLTxGLNtY/NtyRZBfwhdAmRjKQJTVgn5j3y0s+j/bvpzMktoVeHB7irOhxDnZJdIxNNMY3nUKBgQB81jg8lNTeBrJqELSJiRXQIe5PyWJWwQJ1XrtfQNcwGkICM1L"
        decN, err := base64.StdEncoding.DecodeString(nStr)
        if err != nil {
            fmt.Println(err)
            return
        }
        n := big.NewInt(0)
        n.SetBytes(decN)
    
        eStr := "AQAB"
        decE, err := base64.StdEncoding.DecodeString(eStr)
        if err != nil {
            fmt.Println(err)
            return
        }
        var eBytes []byte
        if len(decE) < 8 {
            eBytes = make([]byte, 8-len(decE), 8)
            eBytes = append(eBytes, decE...)
        } else {
            eBytes = decE
        }
        eReader := bytes.NewReader(eBytes)
        var e uint64
        err = binary.Read(eReader, binary.BigEndian, &e)
        if err != nil {
            fmt.Println(err)
            return
        }
        pKey := rsa.PublicKey{N: n, E: int(e)}
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?