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

时间:2016-01-27 23:39:29

标签: c# ftp winscp ftps winscp-net

我正在尝试使用C#通过WinSCP连接到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);
}

enter image description here

4 个答案:

答案 0 :(得分:2)

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

这是两个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

答案 1 :(得分:2)

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

  1. 将ssh-rsa添加为第一部分
  2. 将2048(密钥长度,以位为单位)添加为第二部分
  3. 删除SHA256:如果您已获得密钥,则将其删除
  4. 仅保留关键部分,不要将它们分成2个一组,按从ssh-keygen -lf /etc/ssh/ssh_host_rsa_key.pub命令获得的状态保持其原样。

示例:ssh-rsa 2048 N48XXXXH2x9W1ZIFXXXXXXXX6p3UqI6kGA8BbO1XXX

答案 2 :(得分: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

答案 3 :(得分:1)

我也有同样的错误。在我的情况下,我发现我复制SSH指纹密钥的PC运行的是比我在开发PC上更新版本的WinSCP。

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

相关问题