JSch 连接问题:JSchException:算法协商失败 – 即使安装了 JCE

问题描述 投票:0回答:2

我正在尝试使用 JSch 连接到服务器,但它出现连接问题。我可以使用 PuTTY 连接到服务器,但不能使用 Java 代码。

我使用的是jdk1.8.0_171

这是代码片段

session = jsch.getSession(username, server, SSH_PORT);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.setTimeout(timeout);
session.connect(timeout);

我遇到以下异常:

com.jcraft.jsch.JSchException: Algorithm negotiation fail
at com.jcraft.jsch.Session.receive_kexinit(Session.java:590)
at com.jcraft.jsch.Session.connect(Session.java:320)

以下是 JSch 日志:

INFO: Connecting to <servername> port 22
INFO: Connection established
INFO: Remote version string: SSH-2.0-OpenSSH_8.0
INFO: Local version string: SSH-2.0-JSCH-0.1.54
INFO: CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
INFO: CheckKexes: diffie-hellman-group14-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
INFO: CheckSignatures: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
INFO: SSH_MSG_KEXINIT sent
INFO: SSH_MSG_KEXINIT received
INFO: kex: server: [email protected],diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,diffie-hellman-group16-sha512
INFO: kex: server: rsa-sha2-512,rsa-sha2-256,ssh-rsa
INFO: kex: server: [email protected],[email protected],[email protected],aes256-ctr,aes192-ctr,aes128-ctr
INFO: kex: server: [email protected],[email protected],[email protected],aes256-ctr,aes192-ctr,aes128-ctr
INFO: kex: server: [email protected],[email protected],hmac-sha2-512,hmac-sha2-256,[email protected],hmac-sha1,[email protected],[email protected],[email protected],[email protected]
INFO: kex: server: [email protected],[email protected],hmac-sha2-512,hmac-sha2-256,[email protected],hmac-sha1,[email protected],[email protected],[email protected],[email protected]
INFO: kex: server: none,[email protected]
INFO: kex: server: none,[email protected]
INFO: kex: server: 
INFO: kex: server: 
INFO: kex: client: ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
INFO: kex: client: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
INFO: kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-ctr,aes192-cbc,aes256-ctr,aes256-cbc
INFO: kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-ctr,aes192-cbc,aes256-ctr,aes256-cbc
INFO: kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
INFO: kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
INFO: kex: client: none
INFO: kex: client: none
INFO: kex: client: 
INFO: kex: client: 
INFO: Disconnecting from <servername> port 22
com.jcraft.jsch.JSchException: Algorithm negotiation fail

这是pom文件:

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

我也尝试使用 JSch 0.1.55,仍然是同样的问题。

我已经检查了JSchException:算法协商失败。但正如您在日志中看到的,所有密码都可用。

如果服务器端不进行任何更改会更好。

我该如何解决这个问题?谢谢您的帮助。

java session ssh connection jsch
2个回答
5
投票

对于那些面临此问题的人,请将您的 pom 更改为:

<!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.54</version>
</dependency>

至:

<!-- https://mvnrepository.com/artifact/com.github.mwiede/jsch -->
<dependency>
    <groupId>com.github.mwiede</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.72</version>
</dependency>

这是存储库:https://github.com/mwiede/jsch


2
投票

信息:kex:服务器:[电子邮件受保护],diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,diffie-hellman-group16-sha512

JSch(截至 0.1.55)不支持任何这些 KEX 算法。

您必须让服务器支持 JSch 支持的某些算法,或者切换到另一个 SSH 客户端库。 JSch 不再维护。但它有一个支持现代算法的克隆:https://github.com/mwiede/jsch


强制警告:不要使用

StrictHostKeyChecking=no
盲目接受所有主机密钥。这是一个安全缺陷。您失去了针对 MITM 攻击的保护。

有关正确(且安全)的方法,请参阅:
使用 JSch SFTP 库时如何解析 Java UnknownHostKey?

© www.soinside.com 2019 - 2024. All rights reserved.