title: PHP ssh2_auth_pubkey_file(): Authentication failed using public key: Invalid key data, not base64 encoded
body: <p>In PHP5.3.3 (on CentOS and apache2) I am attempting to connect to a SFTP via a php script. The code grabs the keys and server details from the constructor</p>
  
  <pre><code>function __construct(){
      $this-&gt;host     = 'servername.loc';
      $this-&gt;port     = SFTP_PORT;
      $this-&gt;auth_user    = 'username';
      $this-&gt;auth_pub     = '/data/home/username/.ssh/id_rsa.pub';
      $this-&gt;auth_priv    = '/data/home/username/.ssh/id_rsa';
      $this-&gt;auth_pass    = null;
      $this-&gt;connection   = null;
  }
  </code></pre>
  
  <p>and uses those details to create the connection. </p>
  
  <pre><code>    private function connect(){
      if (!($this-&gt;connection = ssh2_connect($this-&gt;host, $this-&gt;port))) {
          $this-&gt;response  = array('code' =&gt; "20",
                                   "message" =&gt; "Error connecting to SFTP server.");
          return false;
      }
      if (!ssh2_auth_pubkey_file($this-&gt;connection, $this-&gt;auth_user, $this-&gt;auth_pub,
                                  $this-&gt;auth_priv, $this-&gt;auth_pass)) {
          $this-&gt;response  = array('code' =&gt; "40",
                                   "message" =&gt; "Error authenticating to SFTP server with key.");
          $this-&gt;disconnect();
          return false;
      }
  }
  </code></pre>
  
  <p>The result I get is an error on the call to <code>ssh2_auth_pubkey_file()</code>.</p>
  
  <p>The error is:</p>
  
  <blockquote>
    <p>"<strong><em>ssh2_auth_pubkey_file(): Authentication failed for USERNAME using public key: Invalid key data, not base64 encoded</em></strong>"</p>
  </blockquote>
  
  <p>There is no password on the key, and I can use these keys via CLI ssh to connect to the server manually.</p>
  
  <p>I am stumped. Do I need to encode the keys somehow? Suggestions?</p>