title: x509 Go package - ParsePKIXPublicKey is to DER or PEM?
body: <p>I'm trying to get an object of rsa.PublicKey and I made these steps:</p>
  
  <pre><code> ----BEGIN RSA PUBLIC KEY----
             ....
   ----END RSA PUBLIC KEY----
  </code></pre>
  
  <pre><code>package main
  
  import (
     "crypto/rand"
     "crypto/rsa"
     "crypto/x509"
     "encoding/pem"
     "fmt"
     "io/ioutil"
  )
  
  func main() {
  
   key, err := ioutil.ReadFile("./new_public.pem")
   if err != nil {
      fmt.Println(err.Error())
   }
  
   block, _ := pem.Decode([]byte(key))
   if block == nil {
      fmt.Println("unable to decode publicKey to request")
   }
  
   pub, err := x509.ParsePKIXPublicKey(block.Bytes)
   if err != nil {
       panic("failed to parse RSA encoded  public key" + err.Error())
   }
  
   switch pub := pub.(type) {
   case *rsa.PublicKey:
       fmt.Println("pub is of type RSA:", pub)
   default:
       panic("error")
   }
  </code></pre>
  
  <p>}</p>
  
  <p>After this, when I try to <code>x509.ParsePKIXPublicKey(block.Bytes)</code> I get an error:</p>
  
  <pre><code>panic: failed to parse RSA encoded  public keyasn1: 
  structure error: tags don't match (16 vs {class:0 tag:2 length:129 isCompound:false}) 
  {
   optional:false 
   explicit:false 
   application:false 
   private:false 
   defaultValue:&lt;nil&gt; tag:&lt;nil&gt; 
   stringType:0 
   timeType:0 
   set:false 
   omitEmpty:false
  } AlgorithmIdentifier @3
  </code></pre>
  
  <p>So, I read some blogs and documentations about DER and PEM formats, and they are differents ways to encode an certificate, basicaly one use base64 and other is just bytes.</p>
  
  <p>In x509's package of Golang, the x509.ParsePKIXPublicKey says:</p>
  
  <p><code>ParsePKIXPublicKey parses a DER-encoded public key. These values are typically found in PEM blocks with "BEGIN PUBLIC KEY"</code></p>
  
  <p>And, in the example of this function use the pem.Decode().  I'm very confused about this because this should use <code>pem.Decode</code> or something like <code>der.Decode()</code> ?</p>
  
  <p>Also, what's the real difference between <code>x509.ParsePKCS1PublicKey()</code> and <code>x509.ParsePKIXPublicKey()</code> ? Both do the same job to get a rsa.PublicKey ?</p>