相关疑难解决方法(0)

在 SSH.NET 中使用 PPK 密钥进行身份验证

我正在创建一个窗口服务,用于从 SFTP 服务器下载文件。为此,我正在使用Renci.SshNet,Renci.SshNet.CommonRenci.SshNet.Sftp.

我有这个代码:

String Host = "HostName";
int Port = 22;
String RemoteFileDirectory =
    Convert.ToString(ConfigurationManager.AppSettings["SourcePath"]);
String Username = "UserName";
String Password = "*******";

var KeybasedMethod = new KeyboardInteractiveAuthenticationMethod(Username);
KeybasedMethod.AuthenticationPrompt +=
    (sender, e) => { e.Prompts.First().Response = password; };

AuthenticationMethod[] methods = new AuthenticationMethod[] 
{
    new PrivateKeyAuthenticationMethod(Username, new PrivateKeyFile(@"Z:\SFTP SETUP\CJ22")),
    KeybasedMethod
};
ConnectionInfo connectionInfo = new ConnectionInfo(hostname, username, methods);

using (var sftp = new SftpClient(connectionInfo))
{
    sftp.Connect();
    // ...
}
Run Code Online (Sandbox Code Playgroud)

我得到例外。

私钥文件无效。

我无法弄清楚我的代码中缺少什么。

下面是我使用 …

.net c# ssh sftp ssh.net

9
推荐指数
1
解决办法
2万
查看次数

在 C# 中使用带有 SSH.NET 的字符串中的“OPENSSH”私钥文件失败并显示“无效的私钥文件”

我对 SFTP 或 OpenSSH 没有经验。我正在尝试连接到客户端的 SFTP 以上传文件。

我正在使用 SSH.NET 库 – https://github.com/sshnet/SSH.NET

在我的 C# 代码中,我有一个字符串变量中的私钥:

var key = @"-----BEGIN OPENSSH PRIVATE KEY-----
// snipped
-----END OPENSSH PRIVATE KEY-----
";

MemoryStream keyStream = new MemoryStream(Encoding.UTF32.GetBytes(key));

Renci.SshNet.ConnectionInfo conn = new Renci.SshNet.ConnectionInfo(
    host, 
    port,
    username, 
    new AuthenticationMethod[]
    {
         new PrivateKeyAuthenticationMethod(username, new PrivateKeyFile[]
         {
            new PrivateKeyFile(keyStream, ""), 
         }), 
    });
Run Code Online (Sandbox Code Playgroud)

但我收到一个错误:

无效的私钥文件。

我的问题是,我已经看到其他键开始BEGIN RSA但我的键开始BEGIN OPENSSH- 这是问题吗?

.net c# ssh ssh.net private-key

3
推荐指数
1
解决办法
2505
查看次数

标签 统计

.net ×2

c# ×2

ssh ×2

ssh.net ×2

private-key ×1

sftp ×1