1

Anyone with experience in connecting to SFTPs with C#, with RSA keys then I'd much appreciate their expertees.

I already have some C# programs that connect to an SFTP using RSA keys, and have successfully connected with FileZilla in this instance, so I'm not sure exactly what is not working properly.

I have 'PublicKey.key' and 'PrivateKey.ppk', and when I connect by using the private key on Filezilla, it works/opens fine.

The following code is taken from another connection (different key set, but works fine):

<add key="keyDIR"   value="C:\Users\Administrator\Dropbox\NET\test2d.ppk"  /> 


using Renci.SshNet;

SftpClient xclient = null;

string sPrivateKeyPath = ConfigurationManager.AppSettings["keyDIR"];

PrivateKeyFile ObjPrivateKey = null;
PrivateKeyAuthenticationMethod ObjPrivateKeyAutentication = null;

using (Stream stream = File.OpenRead(sPrivateKeyPath))
{
    if (ConfigurationManager.AppSettings["PassPhraseCode"] != null)
    {
        string sPassPhrase = ConfigurationManager.AppSettings["PassPhraseCode"];

        ObjPrivateKey = new PrivateKeyFile(stream, sPassPhrase);
        ObjPrivateKeyAutentication = new PrivateKeyAuthenticationMethod(pub.JBuser, ObjPrivateKey);
        Console.WriteLine(DateTime.Now.ToString("HH:mm:ss") + " '" + stream.ToString() + "'");

And it is the "new PrivateKeyFile" line in which the code breaks. The error I am receiving is "The Application is in Break Mode". Your app has entered a break state, but there is no code to show because all threads were executing external code (typically system or framework code)."

I have used Console.WriteLine.. to check the directory / password, and am confident it is directing to the correct place.

Only thing I can think of is something to do with the key specifications perhaps? Unfortunately I cannot remember what specifications I selected when creating it (months ago now). The keys were created using PuTTy.

Solution: It was related to the fact that filetype of my key '.ppk' cannot work with SSH.NET. The following was used to convert it:

  1. Open PuttyGen
  2. Click Load
  3. Load your private key
  4. Go to Conversions->Export OpenSSH and export your private key
  • *"in which the code breaks"* - Come on! What a vague problem description! What does it mean *"break"*? Do you get an exception? What exception? – Martin Prikryl Oct 22 '18 at 11:33
  • Also, what SSH/SFTP library are you using? You have to tell us. Is it SSH.NET? – Martin Prikryl Oct 22 '18 at 11:38
  • 1
    Unless you provide a meaningful problem description, we can only guess that you are trying to load .ppk key pair (based on your reference to PuTTY) into SSH.NET (based on `SftpClient` and `PrivateKeyFile` class names), which does not support this format. – Martin Prikryl Oct 22 '18 at 11:38
  • Hi @MartinPrikryl Thanks for the additional questions. I am using the Renci.SshNet library for the SFTP connection, and I am getting "Application is in Break Mode" as my error message when it breaks. – dhowells217 Oct 23 '18 at 11:01
  • Did you make sure that you **do not use** PPK key file? – Martin Prikryl Oct 23 '18 at 11:03
  • Yeah, that was it. Thanks so much for your help!! Weird, as the '.ppk' filetype seems to be working in my previous code, but not this one... – dhowells217 Oct 23 '18 at 11:17
  • PPK *file type* cannot work with SSH.NET. It's more likely that the file is PEM *type*, only with .ppk *extension*. – Martin Prikryl Oct 23 '18 at 11:18
  • Yeah, that makes sense. Thanks again! – dhowells217 Oct 23 '18 at 11:20

0 Answers0