Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

c# - "Renci.SshNet.Common.SshException: Invalid private key file" when loading SSH private key from configuration string using SSH.NET

I'm trying to send a file to some server using SFTP. During this process I'm getting the exception

Renci.SshNet.Common.SshException: Invalid private key file. at Renci.SshNet.PrivateKeyFile.Open(Stream privateKey, String passPhrase)

Generated the keys using PuTTYgen, shown below is an sample format of private key file. It has both the public and the private keys.

PuTTY-User-Key-File-2: ssh-rsa  
Encryption:none  
comment: rsa-key-20190327  
Public-Lines: 4  
AAAAB.....  
......  
Private-Lines: 8  
AAAAgQ......  
.......  
Private-MAC: 54901783....  

I copied the private key part from the above file in the config file and I'm accessing it as SftpKey in my code.

Got an OpenSSH format of the above key which looks like

------BEGIN RSA PRIVATE KEY-----  
MIIE....  
.......  
------END RSA PRIVATE KEY-------  

I copied only the key part from the above file and copied in my config file and ran my code. Issue was not resolved.

Below is the code i'm using for SFTP upload

var fileLength = data.Length;

var keyStr = ConfigurationManager.ConnectionStrings["SftpKey"].ConnectionString;
using (var keystrm = new MemoryStream(Convert.FromBase64String(keyStr)))
{
    var privateKey = new PrivateKeyFile(keystrm);
    using (var ftp = new SftpClient(_ftpServer, _ftpUser, new[] { privateKey }))
    {
        ftp.ErrorOccurred += ErrorOccurred;
        ftp.Connect();
        ftp.ChangeDirectory(_ftpPath);
        using (var dataStream = new MemoryStream(Encoding.UTF8.GetBytes(data)))
        {
            ftp.UploadFile(dataStream, Path.GetFileName(message.MessageId), true,
                (length) => result = fileLength == (int)length);
        }
        ftp.Disconnect();
    }
}

Is there anything wrong with the code or what could be the issue? Any help is much appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...