Bad packet length error message

Jun 4, 2012 at 7:38 PM
Edited Jun 4, 2012 at 7:46 PM

I'm trying to upload files via SFTP, using SSH.NET.  SFTPClient.Connect() throws an SshConnectionException with the message, "Bad packet length 1302334341."  This is my code:

 

 

static void Main()� � � �
{
try
{
 SftpClient sftp = new SftpClient(server, 22, userID, password);
 sftp.Connect();
}
catch(Exception e)
{
 Console.WriteLine(e.Message);
}
}

 

I saw in another discussion that this probably has to do with encryption.  I'm using AES, and I have a host key.  I don't understand how to enter the encryption, though.  I was playing with this:

 

ConnectionInfo connectionInfo = new PasswordConnectionInfo(server, 22, userID, password);
connectionInfo.Encryptions.Clear();
connectionInfo.Encryptions.Add("aes","ssh-rsa 2048 11:22:34:d4:56:d6:78:90:01:22:6b:46:34:54:55:8a")

but I know that's not the right way to pass arguments to Encryptions.Add().  Can you help me figure out how to set the encryption properly (assuming that's the problem)?

Jun 4, 2012 at 9:08 PM

Hi,

yes, you're doing things in the wrong way. You should remove all of the encryptions available and only leave the one that you think is right.

For example, if you want to use aes encryption, you can try something like that:

foreach (var d in ConnectionInfo.Encryptions.Where(p => p.Key != "aes128-cbc").ToList()) { ConnectionInfo.Encryptions.Remove(d.Key); }

Please note that pure "aes" is not available. You have to choose between the following supported encryptions:

3des-cbc, aes128-cbc, aes192-cbc, aes256-cbc, aes128-ctr, aes192-ctr, aes256-ctr, blowfish-cbc and cast128-cbc

Jun 4, 2012 at 10:04 PM

Thanks for the advice; I was able to successfully establish a connection using this code.  How can I check to see which type of encryption I should choose?  I'm currently using WinSCP; under "encryption algorithm" it just says "aes".  Is this just a personal choice, or does my host key indicate which encryption is appropriate?

Jun 4, 2012 at 10:44 PM

Nice to hear this great news.

I think that the encryption to be used depend from what the client needs regarding security/performance. More the encryption use a stronger algorithm, more are the required system resources.

The following article helped me to decide the type of encryption to choose

Arcfour appear to be the faster one, but unfortunately it's not supported from SSH.NET. Not sure on how winscp chooses the encryption, but I have seen that most of the programs, when not instructed from the user, chooses a default one.
Since you got problems to establish the connection, I suggest you to download my latest patches which fixes some serious bugs related to the negotiation phase.

Jun 4, 2012 at 10:54 PM
Edited Jun 4, 2012 at 10:55 PM

Ok, that article helps, thanks a lot.

By the way, I posted this problem at Stack Overflow (http://stackoverflow.com/questions/10886665/how-to-resolve-a-bad-packet-length-error-in-ssh-net), so if you want some reputation points for it, post the solution that you recommended above and I'll accept it as the answer:


foreach (var d in ConnectionInfo.Encryptions.Where(p => p.Key != "aes128-cbc").ToList()) { ConnectionInfo.Encryptions.Remove(d.Key); }
Jun 4, 2012 at 11:34 PM

Unfortunately I don't have a stackoverflow account, but thanks anyway. bye