How can I set Public key File and Private key File with Pass-phrase. Also I need to accept the fingerprint sent by the connecting sftp. I am using Tamir.SharpSsh source. Is it possible to incorporate there in the source ? Please suggest.
Asked
Active
Viewed 4,951 times
1 Answers
2
I have successfully used the SSH.NET open source library to work with SFTP. You can see this gist for more code on creating a folder, uploading, etc.
This is the code to connect to SFTP 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 SftpClient(con);
client.Connect();
// .....
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.
- Open the private key in puttygen
- Go to the Conversions menu and choose Export OpenSSH Key.
- Save that new key to a file and use it.

Robin Rodricks
- 110,798
- 141
- 398
- 607