SSH主机密钥指纹与C# WinSCP模式不匹配

12

我正在尝试使用C#通过WinSCP连接到FTPS服务器,但是我遇到了这个错误:

SSH主机密钥指纹 ... 不符合模式 ...

经过大量的研究,我认为这与密钥的长度有关。当我使用WinSCP界面下的“服务器和协议信息”连接时,从WinSCP获取的密钥是 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx ,但我在示例中看到的密钥较短,像这样 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx

请问有人可以帮忙并提供任何解决此问题的指针将不胜感激。

以下是我的代码:

string winscpPath = "C:\\Program Files (x86)\\WinSCP\\WinSCP.exe";
string username = "User123";
string password = "abc1234";
string ftpSite = "192.168.5.110";
string localPath = "C:\\Users\\ttom\\Documents";
string remoteFTPDirectory = "/Usr/thisfolder";
string sshKey = "1b:68:10:80:77:c6:65:91:51:31:5t:65:1c:g6:13:20:39:g8:d8:6d";
Boolean winSCPLog = true;
string winSCPLogPath = "C:\\Users\\ttom\\Documents\\Visual Studio 2015\\Projects\\WebApplication1\\WebApplication1";

SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Sftp,
    HostName = ftpSite,
    UserName = username,
    Password = password,
    SshHostKeyFingerprint = sshKey
};

using (Session session = new Session())
{
    // WinSCP .NET assembly must be in GAC to be used with SSIS,
    // set path to WinSCP.exe explicitly, if using non-default path.
    session.ExecutablePath = winscpPath;
    session.DisableVersionCheck = true;

    if (winSCPLog)
    {
        session.SessionLogPath = @winSCPLogPath + @"ftplog.txt";
        session.DebugLogPath = @winSCPLogPath + @"debuglog.txt";
    }

    // Connect
    session.Timeout = new TimeSpan(0, 2, 0); // two minutes
    session.Open(sessionOptions);

    TransferOptions transferOptions = new TransferOptions();
    transferOptions.TransferMode = TransferMode.Binary;

    session.GetFiles(remoteFTPDirectory + "/" +
        "test.txt", localPath, false, transferOptions);
}

在此输入图像描述

5个回答

7

如果您有WinSCP客户端,并能够连接到FTP站点,则WinSCP具有良好的“代码生成”功能。

  1. 首先进行连接
  2. 选择会话>生成会话URL /代码

在这里输入图片描述


谢谢!我不知道这个存在,它非常棒! - Daniel Williams
@DanielWilliams 我尝试过了,但是出现了错误:“WinSCP.SessionRemoteException: 密钥交换算法diffie-hellman-group1-sha1未经验证!” - Mdarende

5

我之前也遇到了同样的问题,但是在尝试了一些不同的方法后,以下的方法对我起作用:

  1. 在密钥的第一部分添加ssh-rsa。
  2. 将2048(密钥长度,以位为单位)添加为第二部分。
  3. 如果你获取的密钥中有SHA256,则将其删除。
  4. 仅保留密钥部分,不要将它们分开成两个集合,将从ssh-keygen -lf /etc/ssh/ssh_host_rsa_key.pub命令中获取的密钥保持原样。

例如:ssh-rsa 2048 N48XXXXH2x9W1ZIFXXXXXXXX6p3UqI6kGA8BbO1XXX


1
使用FileZilla显示指纹值时,除了您清楚解释的步骤之外,需要在指纹后面添加一个尾随的“=”符号,以使WinSCP连接正常工作。 - Aidan Whitehall
当我在PowerShell中使用WinSCPnet.dll时,删除“SHA256:”对我起了作用。 - Gad D Lord

4

在代码中,您正在使用SFTP(通过SSH)进行连接,但在GUI中使用FTPS(FTP over TLS/SSL)。

这是两个完全不同的协议

请使用{{link2:Protocol = Protocol.Ftp}}并启用TLS/SSL,方法是{{link3:FtpSecure = FtpSecure.Explicit}}。

SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Ftp,
    FtpSecure = FtpSecure.Explicit,
    HostName = ftpSite,
    UserName = username,
    Password = password,
};

对于FTPS,SshHostKeyFingerprint的等价物是TlsHostCertificateFingerprint。但只有在TLS/SSL证书未被受信任的机构签署(例如自签名证书)时才需要使用它。
最简单的方法是让WinSCP GUI生成代码为您生成代码。

2

我也遇到了同样的错误。在我的情况下,我发现我从中复制SSH指纹密钥的电脑上运行的WinSCP版本比我开发PC上的旧。

更新我开发PC上的WinSCP.exe和WinSCPnet.DLL文件解决了我的问题。


1
我正在处理一个类似的任务。您尝试在指纹字符串前加上“ssh-rsa”了吗?
在我的端上一切似乎都正常,这让我相信这里可能有两件事情发生了。
  1. You could be missing part of your authentication string:

    string SshHostKeyFingerpring = "ssh-rsa XXXX 1b:68:10:80:77:c6:65:91:51:31:5t:65:1c:g6:13:20:39:g8:d8:6d";
    

    and/or

  2. You are using two protocols. SFTP and FTPS


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接