简体   繁体   English

使用字符串中的密钥通过 SSH.NET 对 SFTP 进行身份验证时出现“无效的私钥文件”

[英]"Invalid private key file" when authenticating to SFTP with SSH.NET using a key in a string

I have a program where I want to connect to an SFTP host and push/pull files.我有一个程序,我想连接到 SFTP 主机并推/拉文件。 The host requires a private key to connect.主机需要私钥才能连接。 Using these posts for guidance:使用这些帖子作为指导:

I did the following:我做了以下事情:

  • With the given.ppk file, I converted it to OpenSSH format with PuTTYgen使用 given.ppk 文件,我使用 PuTTYgen 将其转换为 OpenSSH 格式
  • From the resulting private key, I copied it into my code and tried to create an SftpClient with it.从生成的私钥中,我将其复制到我的代码中并尝试使用它创建一个SftpClient

The following is the code I have:以下是我的代码:

SftpClient sftp = null;

if (bHost.Equals("sftp.sftpHost.com"))
{
    var keyStr = @"-----BEGIN RSA PRIVATE KEY-----
    Proc-Type: 4,ENCRYPTED
    DEK-Info: DES-EDE3-CBC,15BEAB6C0B73F39A

    ***private key***
    -----END RSA PRIVATE KEY-----";

    using (var keystrm = new MemoryStream(Encoding.UTF8.GetBytes(keyStr)))
    {
        var privateKey = new PrivateKeyFile(keystrm, "passPhrase");
        sftp = new SftpClient(bHost, bUser, new[] { privateKey });
    }
} else
{
    sftp = new SftpClient(bHost, bPort, bUser, bPw);
}

return sftp;
                   

Is there something wrong with my code, or possibly the way I generated the OpenSSH formatted key was incorrect?我的代码是否有问题,或者我生成 OpenSSH 格式密钥的方式可能不正确?

I noticed in other example key strings, they do not include the following:我注意到在其他示例键字符串中,它们不包括以下内容:

Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,15BEAB6C0B73F39A

It's probably the leading spaces in your multi-line string.它可能是多行字符串中的前导空格。 They cannot be there.他们不能在那里。

Remove them.删除它们。

    var keyStr = @"-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,15BEAB6C0B73F39A

***private key***
-----END RSA PRIVATE KEY-----";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 C# 中通过 SSH.NET 在字符串中使用“OPENSSH”私钥文件失败,并显示“私钥文件无效” - Using "OPENSSH" private key file in string with SSH.NET in C# fails with "invalid private key file" 使用 SSH.NET 从配置字符串加载 SSH 私钥时出现“Renci.SshNet.Common.SshException: Invalid private key file” - "Renci.SshNet.Common.SshException: Invalid private key file" when loading SSH private key from configuration string using SSH.NET 使用 SSH.NET 库使用.ppk 私钥进行身份验证 - Authenticate using .ppk private key using SSH.NET library 使用 SSH.NET 流式传输私钥的问题 - Issue with streaming private key using SSH.NET 带有多个线程的SSH.NET SSH私钥文件锁定问题 - SSH.NET SSH Private Key File Lock issue with Multiple Threads 使用SSH.NET将文件上传到SFTP:无法连接 - Uploading file to SFTP using SSH.NET: Cannot connect 使用SSH.NET库获取将文件复制到sftp上的时间 - get time when a file was copied on sftp using SSH.NET library 使用 SSH.NET 将 csvhelper 创建的内容的内存流上传到 SFTP 服务器时,上传的文件为空 - When uploading memory stream with contents created by csvhelper using SSH.NET to SFTP server, the uploaded file is empty 使用SSH.Net将GzipStream上传到SFTP - Uploading GzipStream to SFTP using SSH.Net SSH.NET 仅通过私钥进行身份验证(公钥身份验证) - SSH.NET Authenticate via private key only (public key authentication)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM