Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
3.2k views
in .NET FTP by (280 points)
This is the code snippet I am using from the EG: program.
Am I missing any thing
Unix box has the private key
and the windows box has the public key
connection is from windows to unix

SecureFTPConnection ftpConnection = new SecureFTPConnection();
string serveradddr = "192.157.104.567";
ftpConnection.ServerAddress = "192.157.104.567";
ftpConnection.ServerPort = 22;
ftpConnection.UserName = "connectme";
ftpConnection.Protocol = FileTransferProtocol.SFTP;
string publicKeyFile = @"d:\keys\authorized_keys";
ftpConnection.Password = "connectme123";

ftpConnection.ServerValidation = SecureFTPServerValidationType.Automatic;
ftpConnection.KnownHosts.AddKnownHost(serveradddr, publicKeyFile);


PrintLine("Connecting to server " + ftpConnection.ServerAddress);
ftpConnection.Connect();


PrintLines(ftpConnection.GetFiles());

PrintLine("Closing client");
ftpConnection.Close();
PrintLine("Example complete");

3 Answers

0 votes
by (162k points)
For public key authentication - for the server to validate the client - the user's public key must be registered with the server (typically by copying it into an authorized_keys file on the server), and the user's private key must be loaded by the client.

In addition to this, the server's public key must be available to the client (usually in a known_hosts file) for the client to validate the server.

So here you need to set the ClientPrivateKeyFile property to the location of the client's private key. And you need to ensure the client's public key is in the server's authorized_keys file.
0 votes
by (280 points)
I am trying to do the other way around....

public key with the client
and private key in the server

here is the eg: in the example viewer

Demonstrates how to connect to an SFTP server where the server is validated by
matching the public key that it presents against one stored in a file.

public void Run(string serverAddress, int serverPort, string userName, string password, string publicKeyFile)
{
// Instantiate SecureFTPConnection
SecureFTPConnection ftpConnection = new SecureFTPConnection();

// setting server address and credentials
ftpConnection.ServerAddress = serverAddress;
ftpConnection.ServerPort = serverPort;
ftpConnection.UserName = userName;
ftpConnection.Password = password;

// select SFTP
ftpConnection.Protocol = FileTransferProtocol.SFTP;

// turn on server validation and set the public key for the host
ftpConnection.ServerValidation = SecureFTPServerValidationType.Automatic;
ftpConnection.KnownHosts.AddKnownHost(serverAddress, publicKeyFile);

// connect to server
PrintLine("Connecting to server " + serverAddress);
ftpConnection.Connect();

// get the current working directory and files
PrintLine("Files in directory " + ftpConnection.WorkingDirectory + ":");
PrintLines(ftpConnection.GetFiles());

// Shut down client
PrintLine("Closing client");
ftpConnection.Close();

PrintLine("Example complete");
}
0 votes
by (162k points)
You must be talking about the server's keys for this to work. The server's private key is stored on the server, and the client needs access to its public key, normally stored in a known_hosts file on the client.

The authorized_keys file normally contains the *client's* public key for the server to validate the client - not the one you should be loading via AddKnownHost.

You shouldn't need to do anything with the server's private key. All you need is the server's public key.

To start off with, you should disable server validation altogether just to make sure you can connect:

ftpConnection.ServerValidation = SecureFTPServerValidationType.None;

Categories

...