手册并没有表明这一点,但是一些谷歌搜索从十年之交返回页面(例如this)表明这至少使用是这样的。我们有这样的代码:
var privateKeyAuthenticationMethod =
new PrivateKeyAuthenticationMethod(userName, new PrivateKeyFile(privateKeyLocation));
var connInfo =
new ConnectionInfo(
ftpSettings.HostAddress, ftpSettings.UserName, privateKeyAuthenticationMethod);
using (var client = new SftpClient(connInfo))
{
client.Connect();
client.UploadFile(memStreamData, destination);
}
这挂钩到我们通过SCP2成功使用的私钥(Secure Copy 2详细here) - 它是2048 DSA,在SSH.NET文档中列为有效。密钥使用ssh.com格式,如:
---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----
Subject: <FID name>
Comment: "2048-bit dsa, <FID name>@<Server name>, Mon Apr 24 201\
7 15:49:36 +0100"
<ENCRYPTED KEY>
---- END SSH2 ENCRYPTED PRIVATE KEY ----
但是,SSH.NET的私钥处理部分中有Regex
无法匹配我们的私钥,因此抛出异常:
ERROR 2017-05-10 15:31:43 UTC [T: 474] – Invalid private key file.
ERROR 2017-05-10 15:31:43 UTC [T: 474] – at Renci.SshNet.PrivateKeyFile.Open(Stream privateKey, String passPhrase)
如果我们将密钥转换为OpenSSH格式,它确实匹配,但由于业务限制,这不一定是我们可以在生产中使用的东西。
有没有人确切知道SSH.NET的私钥格式有哪些限制?我们不确定这次是否需要放弃SSH.NET用于其他SFTP包装器库,编写我们自己的或者什么。
答案 0 :(得分:6)
正如PrivateKeyFile
类型的代码内文档所示:
支持OpenSSH和ssh.com格式的RSA和DSA私钥。
我已经生成了新的ssh.com私钥:
---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----
Comment: "dsa-key-20170511"
P2/56wAAA4IAAAAmZGwtbW9kcHtzaWdue2RzYS1uaXN0LXNoYTF9LGRoe3BsYWlufX0AAA
AEbm9uZQAAA0QAAANAAAAAAAAACACOT7SBNaNYmT13gDCCFyE/3yEs6oVbmmGVM6dIesTC
iwY26oHL2JKNLh2gHYfwa9iwDESAH6CIUR+Jf19KXLyNMLQgso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ile
可以毫无问题地加载它。
PrivateKeyFile
无法处理的关键是Subject:
标题。删除它。
如果您需要完整的密钥文件,只需在运行时将其删除,同时阅读:
string key = File.ReadAllText(@"C:\path\key");
Regex removeSubjectRegex = new Regex("Subject:.*[\r\n]+", RegexOptions.IgnoreCase);
key = removeSubjectRegex.Replace(key, "");
MemoryStream buf = new MemoryStream(Encoding.UTF8.GetBytes(key));
PrivateKeyFile privateKeyFile = new PrivateKeyFile(buf);