I have a program where I want to connect to an SFTP host and push/pull files. The host requires a private key to connect. Using these posts for guidance:
- "Renci.SshNet.Common.SshException: Invalid private key file" when loading SSH private key from configuration string using SSH.NET and
- Using "OPENSSH" private key file in string with SSH.NET in C# fails with "invalid private key file"
I did the following:
- With the given .ppk file, I converted it to OpenSSH format with PuTTYgen
- From the resulting private key, I copied it into my code and tried to create an
SftpClient
with it.
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?
I noticed in other example key strings, they do not include the following:
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,15BEAB6C0B73F39A