使用 jsch 和 public 键连接 sftp

Connnect sftp using jsch and public key

我正在使用 jsch 从 java 连接 sftp 服务器并将文件发送到特定目录

    private static boolean enviarCSV(String localFile) throws IOException, JSchException, SftpException {

        logger.info("Enviando fichero a maquina destino..");
        ChannelSftp channelSftp = setupJsch();
        channelSftp.connect();

        String remoteDir = Config.getParametro("wssf.conf.remoteDir");
        channelSftp.put(localFile, remoteDir + localFile);

        logger.info("Enviado con exito!");

        channelSftp.exit();

        return false;

    }


    private static ChannelSftp setupJsch() throws JSchException {
        JSch jsch = new JSch();

        String user = Config.getParametro("wssf.conf.login.usuario");
        String password = Config.getParametro("wssf.conf.login.password");
        String remoteHost = Config.getParametro("wssf.conf.remotehost");

        Session jschSession = jsch.getSession(user, remoteHost, 40020);
        jschSession.setConfig("StrictHostKeyChecking", "no");
        jschSession.setPassword(password);
        jschSession.connect();
        return (ChannelSftp) jschSession.openChannel("sftp");
    }

我需要使用 public 密钥才能连接到 SFTP 服务器。我是安全方面的新手,我不确定该怎么做,另外我只看到使用私钥的示例,但我认为我不需要它,对吗?

如果你能帮助我,我将不胜感激 谢谢

钥匙成对出现。一个私人,一个public。

要使用您的 public 密钥(您自由共享)进行身份验证,您需要向另一方证明您实际上也拥有匹配的私钥(因为任何人都可以拥有您的 public 密钥,但只有你有私人密钥)。

您可以通过使用您的私钥加密或唱一些东西来做到这一点 - 这可以通过您的 public 密钥进行验证。

因此,对于 "public key authentification",您实际上还必须使用私钥......并且可以使用引用该私钥的示例:-)