Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSH library with support for passphrase rsa privatekeys

Tags:

c#

windows

ssh

I'm currently working on an small Visual C# app in which I need a SSH library. I already tried DotNetSSH, Renci.SshNet and SharpSsh. The Granados SSH library is poorly documented (at least I found nearly nothing), so I skipped this one.

All these libraries have one huge problem (IMHO): They fail to open SSH private keys with passphrase.

Renci.SshNet does not support AES. DotNetSSH and SharpSsh use jsch (java ssh library) as base and there seems to be a bug (?) so it does not uncrypt the private key and keeps asking for the passphrase.

Connecting with username+password and private keys without passphrase works great with SharpSsh.

Has somebody already had the same problem? Or is there another C# SSH library out there with "RSA privatekey and passphrase" support?

Thx in advance

like image 914
user1646933 Avatar asked Sep 04 '12 17:09

user1646933


1 Answers

I have successfully used the SSH.NET open source library to work with SSH and SFTP.

This is the code to connect with keyfile + passphrase.

    public void Connect(string host, int port, string user, string passPhrase, string privateKeyFilePath) {

        var keyFiles = new[] { new PrivateKeyFile(privateKeyFilePath, passPhrase) };

        var methods = new List<AuthenticationMethod>();
        methods.Add(new PasswordAuthenticationMethod(user, passPhrase));
        methods.Add(new PrivateKeyAuthenticationMethod(user, keyFiles));

        var con = new ConnectionInfo(host, port, user, methods.ToArray());
        var client = new SshClient(con);
        client.Connect();

        // create an xterm shell
        var Shell = client.CreateShellStream("xterm", 80, 24, 800, 600, 1024);

        // for reading & writing to the shell
        var reader = new StreamReader(Shell);
        var writer = new StreamWriter(Shell);

        // ....

        client.Disconnect();
    }

Private key file format

Please note that your private key file must be in OpenSSH format. If you open the key file in Notepad++ it must have "BEGIN RSA PRIVATE KEY" on the first line.

If not then convert your private key file to an OpenSSH format using puttygen.

  1. Open the private key in puttygen
  2. Go to the Conversions menu and choose Export OpenSSH Key.
  3. Save that new key to a file and use it.
like image 89
Robin Rodricks Avatar answered Oct 29 '22 14:10

Robin Rodricks