SourceForge has been redesigned. Learn more.

key auth with sftp channel supported?

  • vicks

    vicks - 2005-11-22

    I am wondering if JSch library  support the  key based authentication for the channel type SFTP.

    I am trying this code and it does not work Sometimes I get java.lang.OutofMemoryError. After couple of tries it throws "java.lang.NegativeArraySizeException". I found  that the ChannelSftp code is failing and e.printStackTrace in the ChannelSftp shows me this exception
           at com.jcraft.jsch.Buffer.getString(
           at com.jcraft.jsch.ChannelSftp.start(
           at com.jcraft.jsch.Channel.connect(
        at TestKey.main(


          JSch jsch=new JSch();
          String path="Path to private key";
          jsch.addIdentity(path );
          String user="sshuser";
          String host="abchost";

          Session session=jsch.getSession(user, host, 22);

          SftpUserInfo ui=new SftpUserInfo();


          Channel channel=session.openChannel("sftp");

          channel.connect();    // line #34
          System.out.println("Conected successsfully - channel "+channel);
      } Catch (Exception e){
        //log error message
      In the above code if I change the "openChannel" statement as this line, it works fine
       Channel channel=session.openChannel("shell");
      Unfortunately I can not type cast this channel object to ChannelSftp
       Will appreciate any help in this regard.


    • djelimon

      djelimon - 2007-01-30

      String phase = null;
              try {
                  phase = "instantiate JSch object";
                  appJSch = new JSch();
                   phase = "add identity to JSch object";
                   appJSch.addIdentity("C:\\eclpise 3.1.1\\eclipse\\workspace\\blotter_dl\\.ssh\\rsa_ssh_key");
                  phase = "Setting up Session";
                  appSession = appJSch.getSession(parmUser, parmHost, ssh_port);
                  phase = "Setting up User Info object";
                  MyUserInfo ui = new MyUserInfo();
                  phase = "Setting Session user info";
                  phase = "Connecting to server";
                  phase = "Setting up SFTP protocol channel";
                  appChannel = appSession.openChannel(SFTP_PROTOCOL);
                  phase = "connecting via SFTP protocol channel";
                  phase = "Setting up SFTP channel";
                  appSftpChannel = (ChannelSftp) appChannel;
              } catch (JSchException e) {
                  // TODO Auto-generated catch block
                  System.out.println("Error encountered during phase: " + phase);
                  System.out.println("Message is: " + e.getMessage());

      I can do keyed connections with this using pairs created by Jsch, provided they are in the authorized_keys file in the remote box, and this file is chmodded to 600 or 644.

    • Frode

      Frode - 2008-07-08

      I have keys created by the Solaris geygen-tool, but this shouldn't be a problem.
      The public part is installed of the remote SFTP-server.

      There is a firewall blocking my windows-machine from the SFTP-server but I can use an SSH-tunnel via the solaris-machine.

      I am successfully connecting from windows (via the SSH-tunnel), but get this from the Solaris :
      Error encountered during phase: Connecting to server
      Message is: Auth cancel
      com.jcraft.jsch.JSchException: Auth cancel
              at com.jcraft.jsch.Session.connect(
              at com.jcraft.jsch.Session.connect(
              at test.TestSftpWrapper.testPlainConnection(
              at test.TestSftpWrapper.main(

      I have altered th code a bit :

                  phase = "Connecting to server";
                  java.util.Properties config = new java.util.Properties();
                  config.put("StrictHostKeyChecking", "no");


      What can I do ?

    • Frode

      Frode - 2008-07-08

      I did some more testing, and used this UserInfo :
          public static class MyUserInfo implements UserInfo{
              public String getPassword(){ return ""; }
              public boolean promptYesNo(String str){System.out.println(str); return true; }
              public String getPassphrase(){ return ""; }
              public boolean promptPassphrase(String message){System.out.println(message); return true; }
              public boolean promptPassword(String message){System.out.println(message); return true; }
              public void showMessage(String message){ System.out.println(message);

      And when using this one, it clearly states that it asks for password as if the SSH-key is ignored.
      I get the same question from windoes when I use a incorrect key-file.

      It seems that the JSCH-client doesn't send the keys correctly to the server when running it from Solaris ?


Log in to post a comment.