[英]"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:我做了以下事情:
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.