title: pem.Decode - returns nil when given contents of id_rsa.pem
body: <p><br>
  I'm trying to use <code>rsa</code> package for <code>encrypt</code> and <code>decrypt</code> with <code>public</code> and <code>private</code> keys.</p>
  
  <pre><code>func main() {
      flag.Parse()
  
      text := "my super secret"
      glog.Infof("Original text: %s\n", text)
  
      glog.Infoln("Loading public key..")
      pubKey, err := loadPublicKey("id_rsa.pub")
      if err == nil {
          glog.Infoln("SUCCESS!")
      } else {
          glog.Errorln("LOOSE =(")
      }
  
      etext, err := encrypt([]byte(text), pubKey)
      if err != nil {
          glog.Errorf("Can't encrypt text: %+v\n", err)
      }
      glog.Infof("Encrypted text: %s\n", etext)
  
      glog.Infoln("Loading private key..")
      privKey, err := loadPrivateKey("id_rsa")
      if err == nil {
          glog.Infoln("SUCCESS!")
      } else {
          glog.Errorln("LOOSE =(")
      }
  
      dtext, err := decrypt(etext, privKey)
      if err != nil {
          glog.Errorf("Can't decrypt text: %+v\n", err)
      }
      glog.Infof("Decrypted text: %s\n", dtext)
  
      glog.Flush()
  }
  
  func loadPublicKey(path string) (*rsa.PublicKey, error) {
      data, err := ioutil.ReadFile(path)
      if err != nil {
          return nil, err
      }
  
      block, _ := pem.Decode(data)
      if block == nil {
          return nil, fmt.Errorf("no key found\n")
      }
  
      if block.Type != "PUBLIC KEY" {
          return nil, fmt.Errorf("invalid key type - %s\n", block.Type)
      }
  
      pubKey, err := x509.ParsePKIXPublicKey(block.Bytes)
      if err != nil {
          return nil, fmt.Errorf("can't parse key - %+v\n", err)
      }
  
      return pubKey.(*rsa.PublicKey), nil
  }
  
  func loadPrivateKey(path string) (*rsa.PrivateKey, error) {
      data, err := ioutil.ReadFile(path)
      if err != nil {
          return nil, err
      }
  
      block, _ := pem.Decode(data)
      if block == nil {
          return nil, fmt.Errorf("no key found\n")
      }
  
      if block.Type != "PRIVATE KEY" {
          return nil, fmt.Errorf("invalid key type - %s\n", block.Type)
      }
  
      return x509.ParsePKCS1PrivateKey(block.Bytes)
  }
  
  func encrypt(data []byte, pubKey *rsa.PublicKey) ([]byte, error) {
      return rsa.EncryptPKCS1v15(rand.Reader, pubKey, data)
  }
  
  func decrypt(data []byte, privKey *rsa.PrivateKey) ([]byte, error) {
      return rsa.DecryptPKCS1v15(rand.Reader, privKey, data)
  }
  </code></pre>
  
  <p>But I have an error in <code>rsa.EncryptPKCS1v15</code>:</p>
  
  <pre><code>panic: runtime error: invalid memory address or nil pointer dereference
  [signal 0xb code=0x1 addr=0x0 pc=0x5c16c]
  
  goroutine 1 [running]:
  crypto/rsa.EncryptPKCS1v15(0x11641f8, 0xc8200762a0, 0x0, 0xc82004ddc8, 0xf, 0x20, 0x0, 0x0, 0x0, 0x0, ...)
      /usr/local/go/src/crypto/rsa/pkcs1v15.go:32 +0x5c
  main.encrypt(0xc82004ddc8, 0xf, 0x20, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
      /Users/cnaize/Dropbox/develop/gocode/src/test/main.go:132 +0x87
  main.main()
      /Users/cnaize/Dropbox/develop/gocode/src/test/main.go:67 +0x3b8
  </code></pre>
  
  <p>Why where is the error? How to resolve this?</p>
  
  <p><strong>EDITED</strong>:
  <br>
  thanks, now I understand the problem - in <code>loadPublicKey("id_rsa.pub")</code> <code>pem.Decode(data)</code> returns <code>nil block</code>.
  <br>
  What is the right way to parse <code>ssh</code> keys?</p>