2013-03-11 146 views
13

我使用7.2 我已經產生了SSH私有&公共密鑰使用膩子凱基對(SSH2-2048位)運行的NetBeans 1.7 JDK & Windows 7操作系統。我沒有任何私鑰密碼。 我現在試圖使用SFTP連接到其中一臺主機。但是,當我通過私鑰(ppk)設置標識時,代碼將返回無效的私鑰錯誤。我在WinSCP中使用相同的私鑰連接到相同的主機&它工作正常。請幫助我解決錯誤。 這是我的代碼:JSCH - 私有密鑰無效

JSch jsch = new JSch(); 

Session session = null; 

try { 

    jsch.addIdentity("D:\\TEMP\\key.ppk"); 

    session = jsch.getSession("tiabscp", "ssiw.support.qvalent.com", 22); 
    session.setConfig("StrictHostKeyChecking", "no"); 
    //session.setPassword(""); 
    session.connect(); 
    Channel channel = session.openChannel("sftp"); 
    System.out.println("Getting connected"); 
    channel.connect(); 
    System.out.println("connected successfully"); 
    ChannelSftp sftpChannel = (ChannelSftp) channel; 
    sftpChannel.get("remotefile.txt", "localfile.txt"); 
    sftpChannel.exit(); 
    session.disconnect(); 
}catch (JSchException e) { 

    e.printStackTrace(); 

}catch (SftpException e) { 

    e.printStackTrace(); 
} 
+0

請在帖子中包含打印堆棧跟蹤 – Visruth 2013-03-11 06:50:29

回答

24

我猜,你的鑰匙是不是在OpenSSH密鑰文件格式。 JSch預計私鑰是OpenSSH格式。

可以使用的puttygen通過下面的步驟描述here將私鑰與OpenSSH的工作轉換:

  1. 按裝載,並選擇與 的puttygen創建的私鑰。
  2. 輸入密碼以加載密鑰。
  3. 從轉換 菜單選擇導出OpenSSH密鑰
  4. 保存私鑰。
+0

非常感謝您的回覆。轉換爲OpenSSH(使用您的步驟)後,我可以連接到服務器。非常感謝您的支持。 – 2013-03-11 19:50:09

+0

@rgerganov JSch是否支持以SSH2格式讀取私鑰? – yapkm01 2015-06-12 04:37:16

+0

你救了我的一天! JSch需要OpenSSH密鑰文件格式的私鑰。 – user1561521 2015-06-17 23:47:50

2

以下示例代碼可能會對您有所幫助。

package ssh.control; 

import java.io.BufferedReader; 
import java.io.ByteArrayOutputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Properties; 

import android.util.Log; 

import com.jcraft.jsch.ChannelExec; 
import com.jcraft.jsch.JSch; 
import com.jcraft.jsch.Session; 


public class SSHConnections { 
    static String user=""; 
    static String pass=""; 
    static String ip=""; 


    static Session session; 

    public static ChannelExec getChannelExec() throws Exception{ 
     //System.out.println("connected"); 
     //This class serves as a central configuration point, and as a factory for Session objects configured with these settings. 
     JSch jsch = new JSch(); 
     //A Session represents a connection to a SSH server. 
     session = jsch.getSession(user, ip, 22); 
     //getSession() :- the session to which this channel belongs. 
     session.setPassword(pass); 

     // Avoid asking for key confirmation 
     //http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Properties.html 
     Properties prop = new Properties(); 
     prop.put("StrictHostKeyChecking", "no"); 


     //Sets multiple default configuration options at once. 
     session.setConfig(prop); 

     session.connect(); 
     if(session.isConnected()) { 
      System.out.println("connected"); 
     } 

     // SSH Channel 
     //Opens a new channel of some type over this connection. 
     ChannelExec channelssh = (ChannelExec) session.openChannel("exec"); 

     return channelssh; 
    } 

    public static String[] executeRemoteCommand(String command) throws Exception { 

     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     ChannelExec channelssh = SSHConnections.getChannelExec(); 
     channelssh.setOutputStream(baos); 

     // Execute command 
     channelssh.setCommand(command);//gedit tt 
     InputStreamReader isr = new InputStreamReader(channelssh.getInputStream()); 

     BufferedReader bufred = new BufferedReader(isr); 

     channelssh.connect(); 
     String s = bufred.readLine(); 

     List<String> lines = new ArrayList<String>(); 

     int count = 0; 
     while(s!=null) { 
      //System.out.println(s); 
      lines.add(count,s); 
      //  filesandfolders[count]=s; 
      //  System.out.println(filesandfolders[count]); 
      s = bufred.readLine(); 
      count++; 
     } 

     String filesandfolders[] = new String[count]; 

     for(int i = 0; i<count;i++) { 
      filesandfolders[i] = lines.get(i); 
      Log.d("filesandfolders[i]", filesandfolders[i]); 
     } 
     //for(int j=0;j<filesandfolders.length;j++) { 
     //System.out.println(filesandfolders[j]); 
     //} 
     //System.out.println("lines is "+lines.get(0)); 
     //int a; 
     //while((a = isr.read()) != -1) 
     //System.out.print((char)a); 
     //channelssh.disconnect(); 
     //return baos.toString(); 
     return filesandfolders; 
    } 

    public static List<String> executeRemoteCommand1(String command) throws Exception { 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     ChannelExec channelssh=SSHConnections.getChannelExec(); 
     channelssh.setOutputStream(baos); 

     // Execute command 
     channelssh.setCommand(command);//gedit tt 
     InputStreamReader isr = new InputStreamReader(channelssh.getInputStream()); 

     BufferedReader bufred = new BufferedReader(isr); 

     channelssh.connect(); 
     String s = bufred.readLine(); 

     List<String> lines = new ArrayList<String>(); 

     int count=0; 
     while(s != null) { 
      //System.out.println(s); 
      lines.add(count, s); 
      //  filesandfolders[count] = s; 
      //  System.out.println(filesandfolders[count]); 
      s = bufred.readLine(); 
      count++; 
     } 

     String filesandfolders[] = new String[count]; 

     for(int i=0; i<count;i++) { 
      filesandfolders[i]=lines.get(i); 
     } 
     //for(int j=0;j<filesandfolders.length;j++) { 
     //System.out.println(filesandfolders[j]); 
     //} 
     //System.out.println("lines is "+lines.get(0)); 
     //int a; 
     //while((a = isr.read()) != -1) 
     //System.out.print((char)a); 
     //channelssh.disconnect(); 
     //return baos.toString(); 
     return lines; 
    } 
} 

要使目錄:

SSHConnections.user = "username"; 
SSHConnections.ip = "192.168.1.102"; 
SSHConnections.pass = "mypassword"; 
ChannelExec channelssh = SSHConnections.getChannelExec(); 

String dirname = "sampledirectory"; 
try { 
    String[] str = SSHConnections.executeRemoteCommand("mkdir "+dirname); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
0

您可以使用PEMWriter轉換您的私有密鑰將由JSch

下面的例子可以接受PEM格式轉換

Key privateKey = KeyStore.getKey(privateKeyAlias, keyStorePassword);//get key from JKS 
StringWriter stringWriter = new StringWriter(); 
PEMWriter pemWriter = new PEMWriter(stringWriter); 
pemWriter.writeObject(privateKey); 
pemWriter.close(); 

byte[] privateKeyPEM = stringWriter.toString().getBytes(); 
1

也許不是從Java密鑰存儲(JKS)返回鍵一個解決方案,但我找到了這個問題,當我搜索我的問題。

當JSCH預期私鑰文件時,我不小心給了公鑰文件的路徑。

+0

非常相關的觀察。這也是我的情況。 – 2017-12-21 11:49:48