0

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:

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
0

1 Answer 1

1

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-----";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.