上篇解决了低版本jsch连接sftp服务器的问题,后来又发现一个项目中有另一个连接工具ganymed-ssh2,再来解决一下
依赖如下
<dependency>
<groupId>ch.ethz.ganymedgroupId>
<artifactId>ganymed-ssh2artifactId>
<version>build210version>
dependency>
使用此依赖,测试连接本地sftp服务器,本地sftp服务器警告kex algorithms不匹配问题 :
exceptionCaught(ServerSessionImpl[null@/192.168.1.5:56754])
[state=Opened]
SshException: Unable to negotiate key exchange for kex algorithms
(
client:
diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
/
server:
ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,
diffie-hellman-group-exchange-sha256,diffie-hellman-group18-sha512,
diffie-hellman-group17-sha512,diffie-hellman-group16-sha512,
diffie-hellman-group15-sha512,diffie-hellman-group14-sha256,
ext-info-s
)
Disconnecting(ServerSessionImpl[null@/192.168.1.5:56754]):
SSH2_DISCONNECT_KEY_EXCHANGE_FAILED -
Unable to negotiate key exchange for kex algorithms
(
client:
diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
/
server:
ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,
diffie-hellman-group-exchange-sha256,diffie-hellman-group18-sha512,
diffie-hellman-group17-sha512,diffie-hellman-group16-sha512,
diffie-hellman-group15-sha512,diffie-hellman-group14-sha256,
ext-info-s
)
意思是客户端支持的算法和服务端没有能对应上的
在此类其实能搜到
org.apache.sshd.common.kex.BuiltinDHFactories
但均标注了@Deprecated表示废弃,应该是安全性不够
算法装载处在下面类,那我找有没有重写的入口
org.apache.sshd.common.BaseBuilder
可以看到是启动服务器时加载的
我们用反射修改一下静态变量
测试,依然报错
exceptionCaught(ServerSessionImpl[null@/192.168.1.5:58098])
[state=Opened] SshException: Unable to negotiate key exchange for server host key algorithms
(
client: ssh-rsa,ssh-dss
/
server: ecdsa-sha2-nistp256
)
Disconnecting(ServerSessionImpl[null@/192.168.1.5:58098]): SSH2_DISCONNECT_KEY_EXCHANGE_FAILED - Unable to negotiate key exchange for server host key algorithms
(
client: ssh-rsa,ssh-dss
/
server: ecdsa-sha2-nistp256
)
根据上半场推断,sftp服务器默认使用了ec算法,并且长度为256
看KeyPairProvider里是包含ssh-rsa,ssh-dss的,所以我们继续对算法进行”降级“。。。。。。。。。。
org.apache.sshd.common.keyprovider.KeyPairProvider
默认使用了ec,也可以改rsa、dsa
org.apache.sshd.server.keyprovider.AbstractGeneratorHostKeyProvider
org.apache.sshd.common.config.keys.KeyUtils
重启,看到server算法变成rsa了
org.apache.sshd.common.session.helpers.AbstractSession
使用jsch与ganymed-ssh2测试都可以成功
项目中除了jsch,还使用了ganymed-ssh2包,版本是2006年的,支持的算法更少更简单,通过对sftp服务算法调整到客户端的rsa或das达到兼容的目的