【发布时间】:2015-04-12 12:21:18
【问题描述】:
我对 Android 开发相当陌生,我正在尝试使用 Jsch 使用私钥来访问远程服务器。我已将私钥放在 res/raw 文件夹中,但在尝试进行身份验证时如何访问私钥的文件路径时遇到了困难。我以前曾为 Java 项目工作过。这是我目前所拥有的副本。
private Session sshConnect() throws JSchException, IOException
{
try
{
//Login details
jschSession = jsch.getSession(sshUsername, sshServer, 22);
//Connect using private key and corresponding passphrase
jsch.addIdentity("./res/raw/id_rsa", passphrase);
//Ignore SSH key warnings
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
jschSession.setConfig(config);
//System.out.println(localPort);
jschSession.connect();
return jschSession;
}
catch (Exception ex)
{
throw new RuntimeException("SSH connection failed: " + ex.getMessage(), ex);
}
}
然后当我尝试运行时会引发以下错误
java.lang.RuntimeException: SSH connection failed: java.io.FileNotFoundException: ./res/raw/id_rsa: open failed: ENOENT (No such file or directory)
我尝试了以下方法来尝试访问 res 文件夹的内容,但没有这样的运气:
jsch.addIdentity("file:///android_res/raw/id_rsa", passphrase);
【问题讨论】:
标签: android jsch private-key