You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java-Selenium中用JSch配置Ed25519主机密钥与ChaCha20加密

适配Ed25519主机密钥与ChaCha20加密的JSch配置方案

首先注意:JSch从0.1.55版本开始支持Ed25519主机密钥和ChaCha20-Poly1305加密算法,先确保你的Maven依赖使用该版本或更高:

<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.55</version>
</dependency>

接下来修改Java代码,添加对应配置项以适配目标服务器的SSH要求:

package Demo;

import static org.testng.Assert.assertTrue;

import java.io.InputStream;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class JSchExampleSSHConnection {
    public static String getPlayerTestData(String command1) {
        String host = "HostIP";
        String user = "User";
        String password = "Password";
        StringBuilder strBuilder = new StringBuilder();
        try {
            java.util.Properties config = new java.util.Properties();
            // 自动化测试临时关闭严格主机密钥检查,建议后续添加公钥后改为"ask"或"yes"
            config.put("StrictHostKeyChecking", "no");
            // 强制使用SSH-2协议
            config.put("protocol.version", "2.0");
            // 优先使用ChaCha20加密,补充兼容算法
            config.put("cipher.s2c", "chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr");
            config.put("cipher.c2s", "chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr");
            // 支持Ed25519主机密钥,保留其他常见密钥类型
            config.put("hostkeyalgorithms", "ssh-ed25519,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-rsa,ssh-dss");
            
            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, 22);
            session.setPassword(password);
            session.setConfig(config);
            session.connect();
            System.out.println("Connected");

            Channel channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand(command1);

            channel.setInputStream(null);
            ((ChannelExec) channel).setErrStream(System.err);

            InputStream in = channel.getInputStream();
            channel.connect();
            byte[] tmp = new byte[1024];
            while (true) {
                while (in.available() > 0) {
                    int i = in.read(tmp, 0, 1024);
                    if (i < 0) break;
                    System.out.print(new String(tmp, 0, i));
                    strBuilder.append(new String(tmp, 0, i));
                }
                if (channel.isClosed()) {
                    System.out.println("exit-status: " + channel.getExitStatus());
                    break;
                }
                try {
                    Thread.sleep(1000);
                } catch (Exception ee) {
                }
            }
            channel.disconnect();
            session.disconnect();
            System.out.println("DONE");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strBuilder.toString();
    }
}

关键配置说明

  • 强制SSH-2协议:通过protocol.version指定为"2.0",确保连接仅使用SSH-2,匹配服务器要求。
  • ChaCha20加密优先级:在双向加密配置中把chacha20-poly1305@openssh.com放在首位,让JSch优先选用该算法。
  • Ed25519主机密钥支持:在hostkeyalgorithms中加入ssh-ed25519,使JSch能识别并验证服务器的Ed25519密钥。
  • 主机密钥检查说明:当前StrictHostKeyChecking设为"no"是为了自动化测试快速适配,生产环境建议将服务器Ed25519公钥添加到本地known_hosts文件,再将该配置改为"yes"或"ask"以提升安全性。

内容的提问来源于stack exchange,提问作者Harshalata Ulmale

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 03:40:14