7

我正在尝试通过 WinSCP 使用 C# 连接到 FTPS 服务器,但出现此错误:

SSH 主机密钥指纹...与模式不匹配...

经过大量研究,我相信这与密钥的长度有关。使用“服务器和协议信息”下的接口连接时,我从 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);
}

在此处输入图像描述

4

4 回答 4

4

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

这是两个完全不同的协议

使用Protocol = Protocol.Ftp和启用 TLS/SSL 使用FtpSecure = FtpSecure.Explicit.

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

FTPS的等价物SshHostKeyFingerprintTlsHostCertificateFingerprint. 但只有当 TLS/SSL 证书未由受信任的机构(例如自签名证书)签名时,您才需要使用它。


最简单的方法是让WinSCP GUI为您生成代码。

于 2016-01-28T06:58:34.937 回答
3

我也面临同样的问题。但是在尝试了一些不同的模式之后,以下模式对我有用:

  1. 添加 ssh-rsa 作为第一部分
  2. 添加 2048(以位为单位的密钥长度)作为第二部分
  3. 删除 SHA256:如果您获得的密钥中有
  4. 只保留关键部分,不要将它们分开在 2 组中,保留从命令 ssh-keygen -lf /etc/ssh/ssh_host_rsa_key.pub 获得的密钥原样

示例:ssh-rsa 2048 N48XXXXH2x9W1ZIFXXXXXXXXX6p3UqI6kGA8BbO1XXX

于 2020-05-11T18:34:15.903 回答
2

我也有同样的错误。就我而言,我发现我从中复制 SSH 指纹密钥的 PC 运行的 WinSCP 版本比我在开发 PC 上的版本更新。

在我的 Dev PC 上更新 WinSCP.exe 和 WinSCPnet.DLL 文件为我解决了这个问题。

于 2018-03-16T11:41:45.690 回答
1

我正在从事类似的任务。您是否尝试过在指纹字符串中添加“ssh-rsa”前缀?

一切似乎都在我这边工作,所以这让我相信这里可能会发生两件事。

  1. 您可能会丢失部分身份验证字符串:

    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";
    

    和/或

  2. 您正在使用两种协议。SFTP 和 FTPS

于 2016-04-18T18:00:53.740 回答