【问题标题】:Jsch addidentity using Android使用 Android 的 Jsch 加性
【发布时间】: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


    【解决方案1】:

    很简单,你可以从原始文件夹中获取文件。

      InputStream privateKeyByteStream = getResources()
                .openRawResource(
                   getResources().getIdentifier("private_key_without_extension", "raw", getPackageName()));
    

    然后添加身份

    jSch.addIdentity("anyIdentityName", privateKeyBytes, publicKeyBytes, passphrase);
    

    【讨论】:

      【解决方案2】:

      我不确定是否可以将原始资源作为文件访问。

      您可能需要将资源保存到临时路径并参考addIdentity 中的路径。

      如需访问资源,请参阅:
      Android how to get access to raw resources that i put in res folder?

      【讨论】:

        猜你喜欢
        • 2011-12-15
        • 1970-01-01
        • 2015-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-30
        • 2015-09-17
        • 2017-09-08
        相关资源
        最近更新 更多