使用SSH.NET从配置字符串加载SSH私钥时,出现“ Renci.SshNet.Common.SshException:私钥文件无效”

时间:2019-03-27 13:07:53

标签: c# .net ssh sftp ssh.net

我正在尝试使用SFTP将文件发送到某些服务器。在此过程中,我遇到了异常

  

Renci.SshNet.Common.SshException:无效的私钥文件。在Renci.SshNet.PrivateKeyFile.Open(Stream privateKey,String passPhrase)

使用PuTTYgen生成密钥,如下所示是私钥文件的示例格式。它同时具有公共密钥和私有密钥。

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

我从上述文件中复制了私钥部分到配置文件中,并且在我的代码中以SftpKey的身份对其进行访问。

使用上述密钥的OpenSSH格式,类似于

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

我仅复制了上述文件中的关键部分,并复制到了配置文件中并运行了我的代码。问题尚未解决。

下面是我用于SFTP上传的代码

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();
    }
}

代码是否有问题或可能是什么问题?非常感谢您的帮助。

2 个答案:

答案 0 :(得分:5)

因为这是该错误消息的最佳答案,所以我认为值得扩展您原始问题中的一个点-转换为OpenSSH格式密钥。

Renci.SshNet无法使用以以下开头的PuTTY密钥:

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

您可以使用puttygen.exe转换为OpenSSH格式

  1. 将密钥文件加载到puttygen.exe中
  2. 转化> 导出OpenSSH密钥(不是“强制新文件格式”选项)

这将使密钥以:

开头
-----BEGIN RSA PRIVATE KEY-----

那将起作用

答案 1 :(得分:0)

  

我只复制了上面文件中的关键部分

您需要在MemoryStream中拥有完整的密钥文件。并且与文件中的文件完全相同(就像您在文本密钥文件中使用FileStream一样)。所以没有Convert.FromBase64String

var keyStr = @"-----BEGIN RSA PRIVATE KEY-----
MIIEoQIBAAKCAQEAiCYlBq7NITBpCCe48asfXKMpnJJJK+7FQj6wIRJCNuBk76tL
jNooDDPPrnrE9VKxRds4olPftjRj87s9gjm4EirbvijZ9PoDlW9CWFhjJPwCPJpA
onkhaiA7SV+abRDQHm/lst5Fk9tzl+DZcS/EleilGDV7rCYEP692UJRsi3GvzngQ
dpRvVvO4o2rXnEkdp+254KHsah0pSxri23+jqbxPguHKGIMylrswokMI0QKcfm+1
/pjrV64EQCxli3i2yPl4WVh/QaNyHMKoze/WN00Pia99QhE1Rm3YCCarFWFeX+R5
7LgIUhtrE7vZGvimfZN7oBdR2pEq10PIc+8q9QIBJQKCAQAWFAYBFW1fU/VbRLY1
Bv4qsqzNSCeKlWwYlItDohiTRvucfKR3tKyMW23JRFdKYG/GI4yks6e8roy/vX+Y
k7z8BvMzl+v+NmFyLbe7TJp0sz6iCy0TbZa3Q388VLFCHmbwLdI4rmwl0I9JD7SO
5SbMM5BkymcU/z71khMvqV21vym5Ge/ApvX0K0XNJs/N/OLnX46Z8taYEyTmreSR
rxAbma4I5BhqXbH0CMOI5u8zCyycghytl5sYyMr+LIWQKWLzQU+mPNN0qIy0pO5t
r8lGNJh5Lnmu1lQw9yAGo2IPPIERP90X64pVrteIjPtt30n87bWDS8gOiam8S/qk
2ZJVAoGBAPZi6E/KpYpzYGKPAfialu0QN1X7uFio1MUmDum+phk5+xeQb/VvlP6Y
d+/o03EMnhvUsop9p7E2CwLZfT6DO7x3LKtumfceq5dPE5hQSWXi9RkBhcOJaZvZ
z+36c8N8iSZZzlxdA5TeDTUqtuVli4HLrcsXaAaVMxEr/G2JwUgTAoGBAI12Gnoy
k/gsiHz4pDLgxWQE6R8vkBMXfQCWhkzvzKca4twQ8z4ZAb/yt+BCiioJn5g58CVS
dP2zd3Lx8e9kkxggZLcUR3Ao6HceYKeD6mx4vkpHiyCtKJI+qfnkw2A64xwbtvTR
h/O5Aq90SjqP4YcaK9E0W/mWYoL3ctFG8DHXAoGBAKZ6LkPARlag+++RDytvXw7h
cX9JN15/6bWkF+oLMfVehw/r+J7qh0Q9gXiWZVo49TVmM3JU5u1b3e0rKxxmgk7o
vVE85JI3UVhl3M6yyc84fBfQmKa2ytEWoT/uaeTzR+l68zd9Hhh6W/N9udlEnIgh
1kr0I7FruriTV4hIUinHAoGASKRudhn49Q/zD73zdBKO4FWMd8xQ5zWTN6c+C9UW
EJ8ajK7CGPgVp8HUC2BwdnOk+ySrwCNsgkdm2ik3DDqQuVy+GNMP7XzKZq68Av6N
IvHlLQ/7VfgN6jvavpgRTRdSB4Pafbe0hBLltAtItknig6WnzEtR0zGMiHE69dhR
1GcCgYBckoyMXpT0HzOjLXWClSiIaDDfgGcmgEKbYJ7c3mncjLinbCVFdJ0UcrqJ
tiauWBvmecAhnJvQGnmInawNUHetAgJoCbqd7cckjI8VtBgHlQyT93wo9fSDz0Kt
dDHspRvVQkhiR/6IWz1PtCT0QGrHP8fJq/PCbLnJf/EJqJv/xA==
-----END RSA PRIVATE KEY-----
";

using (var keystrm = new MemoryStream(Encoding.ASCII.GetBytes(keyStr)))
{
    var privateKey = new PrivateKeyFile(keystrm);
}

  

实际上,我使用的是先前的代码,它表明存在一个可以成功发送文件的现有实现。

然后您的连接字符串不包含您要求的内容。检查implementation of PrivateKeyFile.Open。它显式检查流starts with ---- BEGIN ... PRIVATE KEY。如果不是,则抛出“无效的私钥文件”。

实际上可能甚至不可能(或很难)将多行内容存储到连接字符串中。如果您的代码曾经奏效,那一定是因为您的SftpKey连接字符串包含完整密钥文件(包括BEGIN ... PRIVATE KEY信封),但还是(再次)在 Base64中进行了编码(作为一行)。像这样:

Convert.ToBase64String(File.ReadAllBytes(@"C:\path\to\key"))

哪个会给你这样的字符串:

LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQ0KTUlJRW9RSUJBQUtDQVFFQWlDWWxCcTdOSVRCcENDZTQ4YXNmWEtNcG5KSkpLKzdGUWo2d0lSSkNOdUJrNzZ0TA0Kak5vb0REUFBybnJFOVZLeFJkczRvbFBmdGpSajg3czlnam00RWlyYnZpalo5UG9EbFc5Q1dGaGpKUHdDUEpwQQ0Kb25raGFpQTdTVithYlJEUUhtL2xzdDVGazl0emwrRFpjUy9FbGVpbEdEVjdyQ1lFUDY5MlVKUnNpM0d2em5nUQ0KZHBSdlZ2TzRvMnJYbkVrZHArMjU0S0hzYWgwcFN4cmkyMytqcWJ4UGd1SEtHSU15bHJzd29rTUkwUUtjZm0rMQ0KL3BqclY2NEVRQ3hsaTNpMnlQbDRXVmgvUWFOeUhNS296ZS9XTjAwUGlhOTlRaEUxUm0zWUNDYXJGV0ZlWCtSNQ0KN0xnSVVodHJFN3ZaR3ZpbWZaTjdvQmRSMnBFcTEwUEljKzhxOVFJQkpRS0NBUUFXRkFZQkZXMWZVL1ZiUkxZMQ0KQnY0cXNxek5TQ2VLbFd3WWxJdERvaGlUUnZ1Y2ZLUjN0S3lNVzIzSlJGZEtZRy9HSTR5a3M2ZThyb3kvdlgrWQ0Kazd6OEJ2TXpsK3YrTm1GeUxiZTdUSnAwc3o2aUN5MFRiWmEzUTM4OFZMRkNIbWJ3TGRJNHJtd2wwSTlKRDdTTw0KNVNiTU01Qmt5bWNVL3o3MWtoTXZxVjIxdnltNUdlL0FwdlgwSzBYTkpzL04vT0xuWDQ2Wjh0YVlFeVRtcmVTUg0KcnhBYm1hNEk1QmhxWGJIMENNT0k1dTh6Q3l5Y2doeXRsNXNZeU1yK0xJV1FLV0x6UVUrbVBOTjBxSXkwcE81dA0KcjhsR05Ka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