首页 > 解决方案 > 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();
        }
    }
}

标签: javaencryptionsshdapache-mina

解决方案


下面应该可以解决问题。

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>

推荐阅读