Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 未定义:调用*rsa.PublicKey时x509.MarshallPKCS1PublicKey_Go - Fatal编程技术网

Go 未定义:调用*rsa.PublicKey时x509.MarshallPKCS1PublicKey

Go 未定义:调用*rsa.PublicKey时x509.MarshallPKCS1PublicKey,go,Go,访问*rsa.PrivateKey 每当我在下面的代码中调用x509.MarshalPKCS1PublicKey(keyBytes.PublicKey)时,我会得到: [tony@localhost app]$ go run gencsr.go # command-line-arguments ./gencsr.go:37:90: undefined: x509.MarshalPKCS1PublicKey 正如您所看到的,我已经包括了x509包,并且我能够访问keyBytes.PublicKe

访问
*rsa.PrivateKey

每当我在下面的代码中调用
x509.MarshalPKCS1PublicKey(keyBytes.PublicKey)
时,我会得到:

[tony@localhost app]$ go run gencsr.go
# command-line-arguments
./gencsr.go:37:90: undefined: x509.MarshalPKCS1PublicKey
正如您所看到的,我已经包括了
x509
包,并且我能够访问
keyBytes.PublicKey
并查看它的类型是
PublicKey

资料来源:


x509.MarshallPKCS1Publickey是。您可能正在运行旧版本

如果您无法升级:并且您可以将其复制到您自己的软件包中:

// pkcs1PublicKey reflects the ASN.1 structure of a PKCS#1 public key.
type pkcs1PublicKey struct {
    N *big.Int
    E int
}

// MarshalPKCS1PublicKey converts an RSA public key to PKCS#1, ASN.1 DER form.
func MarshalPKCS1PublicKey(key *rsa.PublicKey) []byte {
    derBytes, _ := asn1.Marshal(pkcs1PublicKey{
            N: key.N,
            E: key.E,
    })
    return derBytes
}

RSA公钥可以通过以下方式生成:

主程序包
进口(
“字节”
“加密/兰德”
“加密/rsa”
“加密/x509”
“编码/pem”
“fmt”
“golang.org/x/crypto/ssh”
)
func main(){
key,b,err:=generatePrivateKey()
如果错误!=零{
fmt.Println(错误)
返回
}
格式打印项次(字符串(b))
public2,err:=generateSSHPublicKey(键)
如果错误!=零{
fmt.Println(错误)
返回
}
fmt.Println(字符串(public2))
}
func generatePrivateKey()(*rsa.PrivateKey,[]字节,错误){
//keyBytes,err:=rsa.GenerateKey(rand.Reader,2048)
keyBytes,err:=rsa.GenerateKey(rand.Reader,4096)
如果错误!=零{
返回零,零,错误
}
certBytes:=新(字节.缓冲区)
err=pem.Encode(certBytes,&pem.Block{Type:“RSA私钥”,字节:x509.marshallpkcs1privatekey(keyBytes)})
如果错误!=零{
返回零,零,错误
}
返回keyBytes,certBytes.Bytes(),nil
}
func GenerateshPublicKey(key*rsa.PrivateKey)([]字节,错误){
public,err:=ssh.NewPublicKey(&key.PublicKey)/*rsaPublicKey
如果错误!=零{
返回零,错误
}
certBytes:=新(字节.缓冲区)
err=pem.Encode(certBytes,&pem.Block{Type:“公钥”,Bytes:PUBLIC.Marshal()})
如果错误!=零{
返回零,错误
}
返回certBytes.Bytes(),无
}

go版本
的输出类似于1.9.4——没有启动到Linux,所以我不能肯定地说。所以谢谢你。顺便说一句,如果有人有代表性的话,彼得的答案应该被选为最佳答案。升级到1.10解决了我的问题。我必须将
keyBytes.PublicKey
更改为
&keyBytes.PublicKey
,因为函数需要键入
*rsa.PublicKey
。谢谢你,彼得!
// pkcs1PublicKey reflects the ASN.1 structure of a PKCS#1 public key.
type pkcs1PublicKey struct {
    N *big.Int
    E int
}

// MarshalPKCS1PublicKey converts an RSA public key to PKCS#1, ASN.1 DER form.
func MarshalPKCS1PublicKey(key *rsa.PublicKey) []byte {
    derBytes, _ := asn1.Marshal(pkcs1PublicKey{
            N: key.N,
            E: key.E,
    })
    return derBytes
}