Subject: Error: A public key corresponding to the supplied private key was not accepted by the server or the user name is incorrect Date: 2018-01-24 15:56:18 From: ARCON Support Source: error-public-key-corresponding-supplied-private-key-not-accepted-server-user-name-incorrect ---------------------------------------------------------------------- Hi, I have generated Public Key Private Key combination, and have transferred Public Key to the Server whereas I am saving Private Key on my local Machine for now, but once this procedure is done I want to check whether I as a same user is getting access to my account using the Key-based Authentication System and it fails. 1. Generating Keys: ---------------------- public SSHKeyModel GenerateKeys(SSHKeyModel objSshKeyModel) { //1. Setting up details. objSshKeyModel.KeySize = 2048; SecureShellHostKeyAlgorithm secureShellHostKeyAlgorithm = SecureShellHostKeyAlgorithm.RSA; objSshKeyModel.PrivateKeyAlgorithm = "RSA"; string publicKeyAlgorithm = string.Empty; switch (secureShellHostKeyAlgorithm) { case SecureShellHostKeyAlgorithm.RSA: publicKeyAlgorithm = "ssh-rsa"; break; case SecureShellHostKeyAlgorithm.DSS: publicKeyAlgorithm = "ssh-dss"; break; default: throw new ApplicationException("Unsupported algorithm."); } objSshKeyModel.PublicKeyAlgorithm = publicKeyAlgorithm; //1. Generating Private Key. var _privateKey = SecureShellPrivateKey.Create(secureShellHostKeyAlgorithm, objSshKeyModel.KeySize); objSshKeyModel._PrivateKey = _privateKey.GetPrivateKey();//byte[] objSshKeyModel.PrivateKey = Convert.ToBase64String(objSshKeyModel._PrivateKey); //string MemoryStream streamOutput = new MemoryStream(); _privateKey.Save(streamOutput, objSshKeyModel.Passphrase, "putty"); streamOutput.Position = 0; objSshKeyModel.PrivateKey_ = new StreamReader(streamOutput);//StreamOutput objSshKeyModel.PrivateKeyPhysicalLocation = @"T:\Kaushal\Component_privateKey.ppk"; _privateKey.Save(objSshKeyModel.PrivateKeyPhysicalLocation, objSshKeyModel.Passphrase, "putty"); //Location based. SecureShellPrivateKey objSecureShellPrivateKey = new SecureShellPrivateKey(objSshKeyModel.PrivateKeyPhysicalLocation, objSshKeyModel.Passphrase); //2. Getting Public Key. objSshKeyModel._PublicKey = _privateKey.GetPublicKey();//byte[] objSshKeyModel.PublicKey = objSshKeyModel.PublicKeyAlgorithm " " Convert.ToBase64String(objSshKeyModel._PublicKey) " " objSecureShellPrivateKey.Comment;//String streamOutput = new MemoryStream(); _privateKey.SavePublicKey(streamOutput); streamOutput.Position = 0; objSshKeyModel.PublicKey_ = new StreamReader(streamOutput);//stream objSshKeyModel.PublicKeyPhysicalLocation = @"T:\Kaushal\Component_publicKey.pub"; _privateKey.SavePublicKey(objSshKeyModel.PublicKeyPhysicalLocation);//Location Based. //3. Getting Fingerprint objSshKeyModel.Fingerprint = _privateKey.Fingerprint.ToString(); return objSshKeyModel; } ---------------------- 2. Transferring Keys : (Please do not worry about the functions used internally.) ---------------------- public bool TransferPublicKey(SSHKeyModel objSshKeyModel) { bool transferSuccess = false; //1. Connect to Server. SshClient objSshClient = new SshClient(); objSshClient.Connect(objSshKeyModel.IpAddress, objSshKeyModel.Port); //2. Check whether server was connected. if (objSshClient.IsConnected) { //2.1 Authenticating User, via username and password. objSshClient.Authenticate(objSshKeyModel.UserName, objSshKeyModel.Password); if (objSshClient.IsAuthenticated) { //User is Authenticated. var command = string.Empty; //1. Getting into the User Directory. command = @"cd /home/" objSshKeyModel.UserName ""; var recievedData = ExecuteCommand(objSshClient: objSshClient, command: command, objSshKeyModel: objSshKeyModel); //2. Setting permission to the .ssh folder. if (string.IsNullOrEmpty(recievedData.Trim())) { //Command executed Successfully. chmod 700 /home/test1/.ssh command = @"chmod 700 /home/" objSshKeyModel.UserName "/.ssh"; recievedData = ExecuteCommand(objSshClient: objSshClient, command: command, objSshKeyModel: objSshKeyModel); if (string.IsNullOrEmpty(recievedData.Trim())) { //Command Executed Successfully. chmod 644 /home/test1/.ssh/authorized_keys command = @"chmod 644 /home/" objSshKeyModel.UserName "/.ssh/authorized_keys"; recievedData = ExecuteCommand(objSshClient: objSshClient, command: command, objSshKeyModel: objSshKeyModel); if (string.IsNullOrEmpty(recievedData.Trim())) { //Command Executed Successfully. //3. Setting permission to the authorized Key file. command = @"cd /home/" objSshKeyModel.UserName "/.ssh"; //2.2 Check whether Location-To-Be-Public-Key-Transferred exists. recievedData = ExecuteCommand(objSshClient: objSshClient, command: command, objSshKeyModel: objSshKeyModel); if (string.IsNullOrEmpty(recievedData.Trim())) { var permission = true; if (recievedData.Contains("Permission denied")) permission = false; if (permission) { //Permission was Granted. //#TODO: Remove this Command. Clearing the Contents of the File. command = @"echo '' > /home/" objSshKeyModel.UserName "/.ssh/authorized_keys"; var strRecievedData = ExecuteCommand(objSshClient: objSshClient, command: command, objSshKeyModel: objSshKeyModel); //2.2.1 strRecievedData = string.Empty; command = @"echo '" objSshKeyModel.PublicKey "' | " "cat >> /home/" objSshKeyModel.UserName "/.ssh/authorized_keys"; strRecievedData = ExecuteCommand(objSshClient: objSshClient, command: command, objSshKeyModel: objSshKeyModel); transferSuccess = (string.IsNullOrEmpty(recievedData.Trim()) || recievedData.Trim().Contains("''")) ? true : false; } else { //Permission was not set for the Location to be accessed. } } } } } } else { //User is not Authenticated. } } objSshClient.Disconnect(); return transferSuccess; } ---------------------- 3. Login Via Private Key: ---------------------- public bool ConnectWithKeys(SSHKeyModel objSshKeyModel) { var userAuthenticated = false; try { //Step 1: Authenticating via SSHClient. SshClient objSshClient = new SshClient(); objSshClient.Connect(objSshKeyModel.IpAddress, objSshKeyModel.Port); if (objSshClient.IsConnected) { SecureShellPrivateKey objSecureShellPrivateKey = new SecureShellPrivateKey(objSshKeyModel.PrivateKeyPhysicalLocation, objSshKeyModel.Passphrase);// objSshKeyModel._PrivateKey);//@"D:\id_rsa.ppk" objSshClient.Authenticate(objSshKeyModel.UserName, objSecureShellPrivateKey); userAuthenticated = (objSshClient.IsAuthenticated) ? true : false; } } catch (SecureShellException ex) { File.AppendAllText(@"D:\Log.txt", " Exception Occured : Stack Trace: " ex.StackTrace " "); } return userAuthenticated; } ---------------------- Error at Line : objSshClient.Authenticate(objSshKeyModel.UserName, objSecureShellPrivateKey); Error Details: Message: A public key corresponding to the supplied private key was not accepted by the server or the user name is incorrect. ProtocolCode: 13 ProtocolMessage: AuthenticationCancelledByUser Source: ComponentPro.Network Status: OperationFailed Could you please help me identify/verify whether or not the key formats generated are correct? Also please let me know if anything placed in above code is not correct? ---------------------------------------------------------------------- Note: This question has been asked on the Q&A forum of Thang Dang's fraudulent ComponentPro brand If you purchased anything from ComponentPro, you have been scammed. Contact the payment processor who sold you the license and ask for your money back. Back to ComponentPro Q&A Forum Index