JSCH - 无效的私钥 [英] JSCH - Invalid private key

查看:1349
本文介绍了JSCH - 无效的私钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行JDK 1.7& Windows 7使用netbeans 7.2
我已经生成了一个SSH私有&使用putty-keygen的公钥对(SSH2-2048位)。私钥没有任何密码。
我现在正尝试使用SFTP连接到其中一台主机。但是当我传递私钥(ppk)来设置Identity时,代码返回无效的私钥错误。我在WinSCP中使用相同的私钥连接到同一主机&它工作正常。请帮我解决错误。
这是我的代码:

I am running JDK 1.7 & Windows 7 using netbeans 7.2 I have generated a SSH private & public key pair (SSH2-2048 bits) using putty-keygen. I do not have any password for private key. I am now trying to connect to one of the host machine using SFTP. But when I pass private key (ppk) to set Identity, code is returning invalid private key error. I used same private key in WinSCP to connect to same host & it is working fine. Kindly help me to resolve the error. Here is my code:

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();
}


推荐答案

我想你的关键是不是OpenSSH密钥文件格式。 JSch期望私钥是OpenSSH格式。

I guess that your key is not in OpenSSH key file format. JSch expects the private key to be in OpenSSH format.

您可以按照描述的步骤此处

You can use PuTTYgen to convert your private key to work with OpenSSH by following the steps described here:


  1. 按Load并选择使用
    PuTTYgen创建的私钥。

  2. 输入密码以加载密钥。

  3. 来自转换
    菜单选择导出OpenSSH键

  4. 保存私钥。

  1. Press Load and select the Private Key that was created with PuTTYgen.
  2. Enter the passphrase to load the key.
  3. From the Conversions menu select export OpenSSH key
  4. Save the private key.

这篇关于JSCH - 无效的私钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆