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
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();
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With