title: Error parsing RSA public key
body: <p>I'm trying to read an RSA public key from file with the following code:</p>
  
  <pre><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)
  }
  </code></pre>
  
  <p>But I got the following error:</p>
  
  <pre><code>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]:
  </code></pre>
  
  <p>Seems something is wrong with my type assertion converting interface{} to *rsa.PublicKey, but I didn't get a compile error.</p>
  
  <p>What's the correct way of doing this?  Thanks in advance!</p>
  
  <p>Regards,
  Terry</p>