【问题标题】:SSH host key fingerprint does not match pattern C# WinSCPSSH 主机密钥指纹与模式 C# WinSCP 不匹配
【发布时间】:2016-01-27 23:39:29
【问题描述】:

我正在尝试通过 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);
}

【问题讨论】:

    标签: c# ftp winscp ftps winscp-net


    【解决方案1】:

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

    这是两个completely different protocols

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

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

    对于 FTPS,SshHostKeyFingerprint 的等效项是 TlsHostCertificateFingerprint。但仅当 TLS/SSL 证书未由受信任的机构(例如自签名证书)签署时,您才需要使用它。


    最简单的方法是为您提供WinSCP GUI generate code

    【讨论】:

      【解决方案2】:

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

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

      示例:ssh-rsa 2048 N48XXXXH2x9W1ZIFXXXXXXXXX6p3UqI6kGA8BbO1XXX

      【讨论】:

      • 使用 FileZilla 显示指纹值,必须在指纹后面添加一个“=”(除了您清楚解释的步骤之外),WinSCP 连接才能正常工作。
      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        我正在从事类似的任务。您是否尝试过在指纹字符串中添加“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

        【讨论】:

          猜你喜欢
          • 2020-03-08
          • 2019-12-03
          • 1970-01-01
          • 2014-04-03
          • 2019-01-08
          • 2013-09-03
          • 1970-01-01
          • 1970-01-01
          • 2016-07-23
          相关资源
          最近更新 更多