title: "Invalid privatekey" when using JSch
body: <p>I'm using the following code to work with Git in a Java application. 
  I have a valid key (use it all the time), and this specific code has work for me before with the same key and git repository, but now I get the following exception:</p>
  
  <blockquote>
    <p>invalid privatekey: [B@59c40796.</p>
  </blockquote>
  
  <p>At this line:</p>
  
  <pre><code>jSch.addIdentity("&lt;key_path&gt;/private_key.pem");
  </code></pre>
  
  <p>My full code:</p>
  
  <pre><code>    String remoteURL = "ssh://git@&lt;git_repository&gt;";
      TransportConfigCallback transportConfigCallback = new SshTransportConfigCallback();
      File gitFolder = new File(workingDirectory);
      if (gitFolder.exists()) FileUtils.delete(gitFolder, FileUtils.RECURSIVE);
  
      Git git = Git.cloneRepository()
              .setURI(remoteURL)
              .setTransportConfigCallback(transportConfigCallback)
              .setDirectory(new File(workingDirectory))
              .call();
  }
  
  
  private static class SshTransportConfigCallback implements TransportConfigCallback {
      private final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
          @Override
          protected void configure(OpenSshConfig.Host hc, Session session) {
              session.setConfig("StrictHostKeyChecking", "no");
          }
  
          @Override
          protected JSch createDefaultJSch(FS fs) throws JSchException {
              JSch jSch = super.createDefaultJSch(fs);
              jSch.addIdentity("&lt;key_path&gt;/private_key.pem");
  
              return jSch;
          }
      };
  </code></pre>
  
  <p>After searching online, I've change createDefaultJSch to use pemWriter:</p>
  
  <pre><code>@Override
  protected JSch createDefaultJSch(FS fs) throws JSchException {
      JSch jSch = super.createDefaultJSch(fs);
      byte[] privateKeyPEM = null;
  
      try {
          KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  
          List&lt;String&gt; lines = Files.readAllLines(Paths.get("&lt;my_key&gt;.pem"), StandardCharsets.US_ASCII);
          PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(String.join("", lines)));
          RSAPrivateKey privKey = (RSAPrivateKey) keyFactory.generatePrivate(privSpec);
  
          PKCS8Generator pkcs8 = new PKCS8Generator(privKey);
  
          StringWriter writer = new StringWriter();
          PemWriter pemWriter = new PemWriter(writer);
          pemWriter.writeObject(pkcs8);
  
          privateKeyPEM = writer.toString().getBytes("US-ASCII");
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  
      jSch.addIdentity("git", privateKeyPEM, null, null);
  
      return jSch;
  }
  </code></pre>
  
  <p>But still getting <em>"invalid privatekey"</em> exception.</p>