SSH.NET autentication fails with "openssh key type: ssh-rsa is not supported" [duplicate]

I'm not experienced with SFTP or OpenSSH. I am trying to connect to a client's SFTP to upload a file.

I am using the SSH.NET library – https://github.com/sshnet/SSH.NET

Within my C# code I have the private key in a string variable:

var key = @"-----BEGIN OPENSSH PRIVATE KEY-----
// snipped
-----END OPENSSH PRIVATE KEY-----
";

MemoryStream keyStream = new MemoryStream(Encoding.UTF32.GetBytes(key));

Renci.SshNet.ConnectionInfo conn = new Renci.SshNet.ConnectionInfo(
    host, 
    port,
    username, 
    new AuthenticationMethod[]
    {
         new PrivateKeyAuthenticationMethod(username, new PrivateKeyFile[]
         {
            new PrivateKeyFile(keyStream, ""), 
         }), 
    });

But I get an error:

invalid private key file.

My question is, I have seen other keys which begin BEGIN RSA but my key begins BEGIN OPENSSH – is this the issue?


Solution 1:

You have two problems with your code:

  • The PrivateKeyFile class reads the key using StreamReader, which defaults to UTF8. Not UTF16. That's why the reading fails with the vague

    invalid private key file.

  • If you correct the Encoding.UTF32 to Encoding.UTF8, it will still fail though. As you have rightly guesses that the SSH.NET does not support the OPENSSH format.

    The latest SSH.NET 2020.0.1 supports the OPENSSH format for ssh-ed25519 keys only. It will say:

    openssh key type: ssh-rsa is not supported

    Older versions did not support this format at all, they will say:

    Key 'OPENSSH' is not supported

    These OPENSSH keys are generated by recent versions of OpenSSH (7.8 and newer).

    You can use ssh-keygen to convert the key to the classic OpenSSH format:

    ssh-keygen -p -f file -m pem -P passphrase -N passphrase
    

    (if the key is not encrypted with a passphrase, use "" instead of passphrase)

    For Windows users: Note that ssh-keygen.exe is now built-in in Windows 10. And can be downloaded from Microsoft Win32-OpenSSH project for older versions of Windows.


    On Windows, you can also use PuTTYgen (from PuTTY package):

    • Start PuTTYgen
    • Load the key
    • Go to Conversions > Export OpenSSH key.
      For RSA keys, it will use the classic format.

    If you are creating a new key with ssh-keygen, just add -m PEM to generate the new key in the classic format:

    ssh-keygen -m PEM
    

This works:

var key = @"-----BEGIN RSA PRIVATE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9cYW3C365BpqBCVZAoGBAOSp2bdpfnwyEJ8MO0SlvFa8
0dI+aWuVu30TNOT0cfTsLht4CRxDsSwus79AMpWW5YC3IitdLod4p865n0YLz4px
D0Pe7L0Gvn4Gr8PmIjs+3HAGJVFDFKv5z5jPTR9S1JHXyY+ChfFhALwBYHFZVgL0
LmboYnbT/gixFv13yO9zAoGAcQmCg5uN4DlB2rp2p9LsDLlbwOAvFcfJ3GVcSBK4
lopbkrlVZOWZOosFXvVuVyLZKKKPL/kWg3x/Ls8XPRmpNrV9285g9y8nnO5xPMQx
ylBjOvlEju32wPvqetVMb+sdKPjhOIqJ4yzX6p4OqcxSUnQq6IfA6O60ddKex+GG
mM0CgYAqm3oISjWhyq+4iN4C2eQXtAdAjkA7c0ZaHRFFapOI+ZqPXEi+oyhuPj4Z
ZeUcZkS9cYKkgRGDG33p1ZiR+iY7I6Qv8Seyu/oh75/lL0imiq7yKTmizL2WIow7
lT1yYNZJI/YNEdWPKOZl6kYsHliW8TaUk0yvDC/LPADabjvjzg==
-----END RSA PRIVATE KEY-----
";

MemoryStream keyStream = new MemoryStream(Encoding.UTF8.GetBytes(key));
new PrivateKeyFile(keyStream);

A related question: "Renci.SshNet.Common.SshException: Invalid private key file" when loading SSH private key from configuration string using SSH.NET.