2013-03-11 11 views
13

私はnetbeansを使って、& Windows 7を実行しています。012 putty-keygenを使ってSSHプライベート&公開鍵ペア(SSH2-2048ビット)を生成しました。秘密鍵のパスワードはありません。 SFTPを使用してホストマシンの1つに接続しようとしています。しかし私がIDを設定する秘密鍵(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

答えて

24

私はあなたの鍵は、OpenSSHの鍵ファイル形式ではないことを推測します。 JSchは秘密鍵がOpenSSH形式であることを期待しています。

  1. 押してロードをして PuTTYgenを使用して作成された秘密鍵を選択します。

    あなたは手順がhereを説明し、以下ではOpenSSHで動作するようにあなたの秘密鍵を変換するために、PuTTYgenを使用することができます。

  2. 鍵を読み込むパスフレーズを入力します。
  3. 変換より メニューエクスポートエクスポートOpenSSHキー
  4. 秘密キーを保存します。
+0

お返事ありがとうございます。あなたのステップを使ってOpenSSHに変換した後、私はサーバーに接続することができます。あなたのサポートに非常に感謝します。 –

+0

@rgerganov JShはSSH2形式の秘密鍵の読み取りをサポートできますか? – yapkm01

+0

あなたは私の一日を保存しました! JSchはOpenSSHキーファイル形式の秘密鍵を必要とします。 – user1561521

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

次の例は、おそらく、Javaキーストア(JKS)

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

からではない返されたキーを変換しJSCH

によって受け入れられるPEM形式にあなたの秘密鍵を変換するPEMWriterを使用することができますあなたのための解決策が、私は私の問題を検索したときにこの質問を見つけました。

私は、JSCHが秘密鍵ファイルを期待しているときに私が誤って公開鍵ファイルへのパスを与えた。

+0

と非常に関連した観察を行います。これも私の場合でした。 –