You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取与Windows SSH客户端匹配的SSH主机密钥用于验证

如何通过SSH.NET获取与Windows SSH客户端格式一致的主机密钥(ed25519/rsa)

我正尝试通过SSH.NET从树莓派这类SSH主机获取与Windows SSH客户端一致的ed25519和rsa主机密钥,用来和%USERPROFILE%\.ssh\known_hosts文件中的内容比对。

Win11内置SSH客户端存储主机密钥的格式如下:

offsite ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICuy9wsbY7fBFFoB6K0RU/BsISB3mL4PQ3311ta0S/cW
offsite ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDAEmXakR9rh5Sokj/PvmyiL/tZtQp7OPbQYb9PdlQjPXELJpBZkFs5gnoqxFj5ysqKnBREY/GpOKkXxdh5dMGGzoir93ypicI7A1VQTd3d8WQdtV1tnP7u9hbAfSqtj+7OGCsi0eMUBYx1fS3XbSC1jKr2MG2ovMCp3kdHtDoGrysP5ai0EXY07ZMmTpxTJbZFtZ8IX3U+ON71KIfZSC6UqlptFyFh1SUmpZXbnsbSr4BDoI8fG0A9Km9BTohWK9ioPSLL530KXz72S4EaBgx0Za1c+TGPqnjAu+s9kc0DeBrwLTEZzfS40xj+sdM4IvPERMBZAPH6cmMutHNWVtIdPyiTi93gCn8oz3D0UX8QT3Yc9Qhx0KLw7AzLajAXSc7zdOebtovZmOejyl4YrYO4Q2rZ3ODTqotFgXGJWNFkg/PoV+Yzy4tILOujDlTHUv3/BkFaj4TSnzgl7NzstdZpNM1R0KBkHlDuM10K9yyLEmNlHSvx8zTZWBTEkzb1FoU=

尝试过程与问题

我遍历SSH.NET的Client.ConnectionInfo.HostKeyAlgorithms字典,但不清楚调用条目值时所需的字节数组来源:

Dim aData As Byte() = Nothing

For Each oAlgorithm In oClient.ConnectionInfo.HostKeyAlgorithms
  Dim oInstance = oAlgorithm.Value(aData)
End For

这段代码执行失败,因为Func(Of Byte(), KeyHostAlgorithm)需要传入实际有效的数组。尝试空数组或任意数组都会报错:

  • 传入空数组oAlgorithm.Value({})时,报错:

The requested length (4) is greater than the actual number of bytes read (0).Parameter name: length

  • 传入{0, 1, 2}时,报错:

The requested length (4) is greater than the actual number of bytes read (3).Parameter name: length

  • 传入{0, 1, 2, 3}时,报错:

Data longer than 2147483647 is not supported.

另外,我在Client.HostKeyReceived事件处理程序中试过以下代码:

Dim sHostKey = Convert.ToBase64String(New SHA256Managed().ComputeHash(e.HostKey))

但生成的Base64字符串和known_hosts中的条目不匹配。


编辑1
我找到了函数调用应该使用的字节数组:

oHandler = Sub(Sender As SshClient, e As HostKeyEventArgs)
             Dim aData As Byte()

             For Each oAlgorithm In Sender.ConnectionInfo.HostKeyAlgorithms
               Dim oInstance = oAlgorithm.Value(e.HostKey)

               If TypeOf oInstance.Key Is Ed25519Key Then
                 aData = DirectCast(oInstance.Key, Ed25519Key).PublicKey
                 Exit For
               End If
             Next
           End Sub

不过aData的Base64编码结果仍与Win11 SSH客户端的输出不匹配,有人知道这些值的生成规则吗?


编辑2
有进展了!现在我得到了和Windows SSH客户端一致的ed25519值——直接对服务器的HostKey做无哈希的Base64编码即可:

Dim sBase64 = Convert.ToBase64String(e.HostKey)
' 返回结果:AAAAC3NzaC1lZDI1NTE5AAAAICuy9wsbY7fBFFoB6K0RU/BsISB3mL4PQ3311ta0S/cW

接下来需要解决RSA密钥的匹配问题。

内容的提问来源于stack exchange,提问作者InteXX

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 23:40:40