JSch连接SFTP Exception:Algorithm negotiation fail问题解决

问题分析
Algorithm negotiation fail 算法协商失败
sftp和ssh用的是同一套加密算法,算法协商失败为客户端与服务端所支持的算法不一致,导致报该异常。解决方法为统一客户端及服务端所支持的算法。

Ssh默认支持的算法为
OpenSSH enables only the following key exchange algorithms by default:

  • curve25519-sha256@libssh.org
  • ecdh-sha2-nistp256
  • ecdh-sha2-nistp384
  • ecdh-sha2-nistp521
  • diffie-hellman-group-exchange-sha256
  • diffie-hellman-group14-sha1

jsch版本为 0.29支持的算法为
jsch因为这0.29为十年前的版本了,对应支持的算法为

Where as JSch claims to support these algorithms for key exchange:

  • diffie-hellman-group-exchange-sha1
  • diffie-hellman-group1-sha1

问题解决
该问题解决还是两种方法:
1、客户端升级jsch版本,升级完后及可支持新版本ssh
2、服务端向下兼容低版本的算法

解决方式一
直接下载最新版本的jsch的jar包进行替换即可。

这种方式对于技术来说应该是首选的了,但这十年的老代码本该淘汰,实在是没人敢动他,所以我们没有采用方式一。

解决方式二
服务端向下兼容低版本的算法,这就是直接修改ssh的配置文件

在SSH的配置文件
/etc/ssh/sshd_config

在配置文件中最后新增

KexAlgorithms curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1 

注意:

对应的算法必须包含客户端需要支持的算法
diffie-hellman-group-exchange-sha256,
diffie-hellman-group14-sha1,
diffie-hellman-group-exchange-sha1,
diffie-hellman-group1-sha1
1
2
3
4
配置完成后重启ssh服务
service sshd restart
systemctl restart sshd.service
1
2
该方法可以临时解决这个问题,但会导致漏扫提示存在漏洞。对于安全性要求高的,不建议使用。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐