Create public key from modulus and exponent in Golang

huangapple go评论90阅读模式
英文:

Create public key from modulus and exponent in Golang

问题

我从第一个证书上获取了以下内容:https://www.googleapis.com/oauth2/v2/certs 的 'n' 和 'e' 键值。在Go语言中是否有一个可以使用 'n' 和 'e' 构建公钥的包?我不知道如何使用 crypto/rsa 包来完成这个操作。能提供一些代码吗?谢谢。

英文:

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

得分: 24

rsa包中有一个PublicKey类型,它具有NE字段。根据JWA草案中的描述,解码这些部分应该相当简单。

以下是一些快速修改的代码(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)}
}
英文:

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 (
	&quot;bytes&quot;
	&quot;crypto/rsa&quot;
	&quot;encoding/base64&quot;
	&quot;encoding/binary&quot;
	&quot;fmt&quot;
	&quot;math/big&quot;
)

func main() {
	nStr := &quot;AN+7p8kw1A3LXfAJi+Ui4o8F8G0EeB4B5RuufglWa4AkadDaLTxGLNtY/NtyRZBfwhdAmRjKQJTVgn5j3y0s+j/bvpzMktoVeHB7irOhxDnZJdIxNNMY3nUKBgQB81jg8lNTeBrJqELSJiRXQIe5PyWJWwQJ1XrtfQNcwGkICM1L&quot;
	decN, err := base64.StdEncoding.DecodeString(nStr)
	if err != nil {
		fmt.Println(err)
		return
	}
	n := big.NewInt(0)
	n.SetBytes(decN)

	eStr := &quot;AQAB&quot;
	decE, err := base64.StdEncoding.DecodeString(eStr)
	if err != nil {
		fmt.Println(err)
		return
	}
	var eBytes []byte
	if len(decE) &lt; 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, &amp;e)
	if err != nil {
		fmt.Println(err)
		return
	}
	pKey := rsa.PublicKey{N: n, E: int(e)}
}

huangapple
  • 本文由 发表于 2014年8月7日 17:56:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/25179492.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定