使用ssh-keygen生成RSA公钥。
试图通过sftp连接远程服务器:
JSch jsch = new JSch();
try {
String publicKey = "/home/testuser/.ssh/id_rsa.pub";
jsch.addIdentity(publicKey);
session = jsch.getSession(sftpUsername, sftpHostname, sftpPort);
session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
} catch (JSchException e) {
logger.error("Unable to obtain session", e);
}出现以下错误:
com.jcraft.jsch.JSchException: invalid privatekey: /home/testuser/.ssh/id_rsa.pub
at com.jcraft.jsch.IdentityFile.<init>(IdentityFile.java:261)
at com.jcraft.jsch.IdentityFile.newInstance(IdentityFile.java:135)
at com.jcraft.jsch.IdentityFile.newInstance(IdentityFile.java:130)
at com.jcraft.jsch.JSch.addIdentity(JSch.java:206)
at com.jcraft.jsch.JSch.addIdentity(JSch.java:192)有什么建议吗?
发布于 2017-03-14 14:27:37
你有:
jsch.addIdentity(publicKey);JSch javadoc说:
public void addIdentity(String prvkey) throws JSchException;添加要用于公钥身份验证的标识。在将其注册到identityRepository之前,它将使用密码破译。 参数:
当JSch想要私钥时,您已经提供了公钥。
如果你想一想,这是有道理的。公钥没什么秘密。JSch想要一个秘密,这样它就能证明你是谁。
您的私钥可能在~/.ssh/id_rsa中(没有.pub扩展)。
您可能需要使用addIdentity的两个参数版本,以便提供密码以解密私钥。
https://stackoverflow.com/questions/42788509
复制相似问题