Public/Private Key Authentication Issue

0 votes
asked Jan 19 by Hysteria86 (140 points)

Hi,

I'm currently trying to authenticate a login using a username, password and public key. I am receiving the following error: "A public key corresponding to the supplied private key was not accepted by the server or the user name is incorrect."

I am able to log in successfully without the private key, so I know the user name is not the issue.

I generated the public/private key pair using the following code:

var privateKey = SshPrivateKey.Generate(SshHostKeyAlgorithm.RSA, 2048);

privateKey.Save(@"C:\MyData\PrivateKey.pri", "password",  SshPrivateKeyFormat.OpenSsh);

privateKey.SavePublicKey(@"C:\MyData\PublicKey.pub", SshPublicKeyFormat.Ssh2Base64);

The public key generated by this code has been saved with the user on the SFTP server.

I'm then attempting to log in using the following code:

// load the private key
SshPrivateKey privateKey = new SshPrivateKey(@"C:\MyData\PrivateKey.pri", "password");

// Authenticate
client.Login(username, password, privateKey);

I believe the server being used to connect to is Bitvise, could someone advise where I'm going wrong? Thanks.

Applies to: Rebex SFTP

1 Answer

0 votes
answered Jan 19 by Lukas Pokorny (85,590 points)

Hi,

The key generation code is correct, and thee only possible issue in authentication code is the usage of both password and private key authentication - this is possible, but less common that password-only or key-only authentication.

Otherwise, the most likely cause of the failure is that the server actually refuses this key because it has not been associated with the user properly. What does "saved with the user" mean? With Bitvise, it seems it has to be imported.

In any case, it might be useful to rule out an issue in Rebex SFTP - the easiest way to do this is to try connecting and authenticating using PuTTY command-line psftp utility (get psftp.exe from PuTTY download page).

It only accepts private keys in .ppk format, which means you have to convert your key first:

SshPrivateKey privateKey = new SshPrivateKey(@"PrivateKey.pri", "password");
privateKey.Save(@"PrivateKey.ppk", "password", SshPrivateKeyFormat.Putty);

Then, try connecting to the server using psftp with this key:

psftp servername -i PrivateKey.ppk

If this fails as well, it would strongly suggest the problem is at the server.

...