SSH主机密钥指纹与模式C#WinSCP不匹配 [英] SSH host key fingerprint does not match pattern C# WinSCP

查看:462
本文介绍了SSH主机密钥指纹与模式C#WinSCP不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to connect to an FTPS server using C# via WinSCP and I am getting this error:

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

SSH host key fingerprint ... does not match pattern ...

经过大量研究,我认为这与密钥的长度有关.当使用服务器和协议信息"下的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

After tons of research, I believe is has something to do with the length of the key. The key I got from WinSCP when connected using its interface under "Server and protocol information" is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx but the ones I saw in the example is shorter like this xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx

有人可以帮忙吗,请提供任何指向我的解决方法,将不胜感激.

Can someone please help and offer me any pointer to resolve this would be greatly appreciated.

这是我的代码

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);
}

推荐答案

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

You are connecting using SFTP (over SSH) in the code, but using FTPS (FTP over TLS/SSL) in GUI.

这是两个完全不同的协议.

使用 Protocol = Protocol.Ftp 并使用SshHostKeyFingerprint等效的FTPS是 TlsHostCertificateFingerprint .但是,只有在TLS/SSL证书未由受信任的权威机构签名(例如,自签名证书)的情况下,才需要使用它.

An equivalent of SshHostKeyFingerprint for FTPS is TlsHostCertificateFingerprint. But you need to use it only when the TLS/SSL certificate is not signed by a trusted authority (e.g. a self signed certificate).

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

这篇关于SSH主机密钥指纹与模式C#WinSCP不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆