“Renci.SshNet.Common.SshException: Invalid private key file” when loading SSH private key from configuration string using SSH.NET

As this is the top answer for that error message, so I think it’s worthwhile expanding on a point in your original question – converting to an OpenSSH format key.

Renci.SshNet can’t use PuTTY keys that start with:

PuTTY-User-Key-File-2: ssh-rsa

You can use puttygen.exe to convert to the OpenSSH format

  1. load your key file in puttygen.exe
  2. Conversions > Export OpenSSH key (not the “force new file format” option)

This will make a key that starts with:

-----BEGIN RSA PRIVATE KEY-----

and that will work

Leave a Comment