java - java.lang.IllegalStateException:无法协商服务器主机密钥算法的密钥交换
问题描述
描述:
尝试向尝试访问 Netopeer2 服务器的应用程序发送请求,但出现问题并且密钥交换失败。有一些解决方案如何在服务器端配置它/etc/ssh/sshd_config
,但我们希望它在应用程序中的客户端。
该应用程序使用 Apache MINA SSHD 建立连接 ( GitHub )。默认情况下,某些算法被禁用。我们希望在下面的 Main 类中启用它们,以便能够rsa-sha2-512, rsa-sha2-256
与服务器进行交换。关于如何使用 Apache MINA SSHD 做到这一点的任何想法?
完整的错误消息说:
java.lang.IllegalStateException: Unable to negotiate key exchange for server host key algorithms
(client: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519,ssh-rsa,ssh-dss /
server: rsa-sha2-512,rsa-sha2-256)
引发错误的代码:
import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.session.ClientSession;
import java.io.IOException;
public class Main{
public static void main(String[] args) {
SshClient client = SshClient.setUpDefaultClient();
client.start();
try {
ClientSession session = client.connect("root", "172.17.0.2", 830).verify(10000).getSession();
session.addPasswordIdentity("root");
session.auth().verify(9999);
// error 'Unable to negotiate key exchange for server host key algorithms' is thrown
}
catch (IOException e){
e.printStackTrace();
}
}
}
解决方案
下面应该可以解决问题。
client.setKeyExchangeFactories(NamedFactory.setUpTransformedFactories(
false,
BuiltinDHFactories.VALUES,
ClientBuilder.DH2KEX
));
client.setSignatureFactories(new ArrayList<>(BuiltinSignatures.VALUES))
用于腻子支撑
https://github.com/apache/mina-sshd/blob/master/docs/files-parsing.md
只需添加以下内容
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-common</artifactId>
<version>...same version as the rest of the artifacts...</version>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-putty</artifactId>
<version>...same version as the rest of the artifacts...</version>
</dependency>
推荐阅读
- firebase - Firebase / NoSQL - 如何聚合数据以进行统计
- java - R Studio 错误:NoclassDefFoundError (Java):org/apache/poi/openxml4j/exceptions/InvalidFormatException
- angular - 从另一个组件绑定和编辑对象
- asp.net-core - 想要使用 Asp.net Core、EF Core(每个租户单独的数据库)创建多租户应用程序
- bash - 在一行中提取并显示多个字符串
- javascript - 如何在 react native 中使用其他组件更新 app.js 的状态?
- dictionary - 模板参数的数量错误(1,应该是 2)。任何人都可以看看吗?
- unity3d - 如何修复 untiy3d 对象在某些方面显示为白色
- asp.net-mvc - 洋葱架构中存储过程的使用
- oracle11g - oracle中度数符号的垃圾值