解析RSA公钥时出错

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

Error parsing RSA public key

问题

我正在尝试使用以下代码从文件中读取RSA公钥:

keyBytes, err := ioutil.ReadFile("pubkey.pem")
if err != nil {
  log.Fatal(err)
}
block, _ := pem.Decode(keyBytes)
fmt.Printf("block.Type: %s\n", block.Type)
pubkeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
pubkey, ok := pubkeyInterface.(*rsa.PublicKey)                                                 
if !ok {
  log.Fatal("Fatal error")
}
cipher, err := rsa.EncryptPKCS1v15(nil, pubkey, []byte(msg))                                   
if err != nil {
  log.Fatal(err)
}

但是我得到了以下错误:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x20 pc=0x463921]

goroutine 1 [running]:
io.ReadAtLeast(0x0, 0x0, 0xc200089002, 0x70, 0x7e, ...)
    /usr/lib/go/src/pkg/io/io.go:284 +0xf1
io.ReadFull(0x0, 0x0, 0xc200089002, 0x70, 0x7e, ...)
    /usr/lib/go/src/pkg/io/io.go:302 +0x6f
crypto/rsa.nonZeroRandomBytes(0xc200089002, 0x70, 0x7e, 0x0, 0x0, ...)
    /usr/lib/go/src/pkg/crypto/rsa/pkcs1v15.go:134 +0x70
crypto/rsa.EncryptPKCS1v15(0x0, 0x0, 0xc20004c550, 0xc20004c560, 0xd, ...)
    /usr/lib/go/src/pkg/crypto/rsa/pkcs1v15.go:35 +0x236
main.encode(0x536630, 0xd, 0x535ad0, 0x9, 0x54f1b0, ...)
    /home/taot/programming/go/encrypt/read_cert.go:28 +0x355
main.main()
    /home/taot/programming/go/encrypt/read_cert.go:12 +0x32

goroutine 2 [syscall]:

goroutine 3 [runnable]:

似乎我的类型断言将interface{}转换为*rsa.PublicKey时出了问题,但我没有得到编译错误。

正确的做法是什么?提前感谢!

谢谢,
Terry

英文:

I'm trying to read an RSA public key from file with the following code:

keyBytes, err := ioutil.ReadFile("pubkey.pem")
if err != nil {
  log.Fatal(err)
}
block, _ := pem.Decode(keyBytes)
fmt.Printf("block.Type: %s\n", block.Type)
pubkeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
pubkey, ok := pubkeyInterface.(*rsa.PublicKey)                                                 
if !ok {
  log.Fatal("Fatal error")
}
cipher, err := rsa.EncryptPKCS1v15(nil, pubkey, []byte(msg))                                   
if err != nil {
  log.Fatal(err)
}

But I got the following error:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x20 pc=0x463921]

goroutine 1 [running]:
io.ReadAtLeast(0x0, 0x0, 0xc200089002, 0x70, 0x7e, ...)
    /usr/lib/go/src/pkg/io/io.go:284 +0xf1
io.ReadFull(0x0, 0x0, 0xc200089002, 0x70, 0x7e, ...)
    /usr/lib/go/src/pkg/io/io.go:302 +0x6f
crypto/rsa.nonZeroRandomBytes(0xc200089002, 0x70, 0x7e, 0x0, 0x0, ...)
    /usr/lib/go/src/pkg/crypto/rsa/pkcs1v15.go:134 +0x70
crypto/rsa.EncryptPKCS1v15(0x0, 0x0, 0xc20004c550, 0xc20004c560, 0xd, ...)
    /usr/lib/go/src/pkg/crypto/rsa/pkcs1v15.go:35 +0x236
main.encode(0x536630, 0xd, 0x535ad0, 0x9, 0x54f1b0, ...)
    /home/taot/programming/go/encrypt/read_cert.go:28 +0x355
main.main()
    /home/taot/programming/go/encrypt/read_cert.go:12 +0x32

goroutine 2 [syscall]:

goroutine 3 [runnable]:

Seems something is wrong with my type assertion converting interface{} to *rsa.PublicKey, but I didn't get a compile error.

What's the correct way of doing this? Thanks in advance!

Regards,
Terry

答案1

得分: 5

错误是因为您将nil传递给rsa.EncryptPKCS1v15。作为第一个参数,它需要一个io.Reader来读取随机字节。您可以使用crypto/rand包中的rand.Reader,例如。

英文:

The error is because you're passing nil to rsa.EncryptPKCS1v15. As the first parameter it needs an io.Reader from which to read random bytes. You can use, for example, rand.Reader from package crypto/rand.

huangapple
  • 本文由 发表于 2013年6月14日 00:45:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/17092644.html
匿名

发表评论

匿名网友

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

确定