本文介绍了使用“OPENSSH"C# 中带有 SSH.NET 的字符串中的私钥文件失败,并显示“无效的私钥文件";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm not experienced with SFTP or OpenSSH. I am trying to connect to a client's SFTP to upload a file.

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

I am using the SSH.NET library – https://github.com/sshnet/SSH.NET

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

Within my C# code I have the private key in a string variable:

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, ""),
         }),
    });

但我收到一个错误:

无效的私钥文件.

我的问题是,我见过其他以 BEGIN RSA 开头的密钥,但我的密钥以 BEGIN OPENSSH 开头——这是问题吗?

My question is, I have seen other keys which begin BEGIN RSA but my key begins BEGIN OPENSSH – is this the issue?

推荐答案

您的代码有两个问题:

  • PrivateKeyFile 类使用 StreamReader 读取密钥,默认为 UTF8.不是 UTF16.这就是为什么阅读会因含糊不清而失败

  • The PrivateKeyFile class reads the key using StreamReader, which defaults to UTF8. Not UTF16. That's why the reading fails with the vague

无效的私钥文件.

  • 如果您将 Encoding.UTF32 更正为 Encoding.UTF8,它仍然会失败.正如您正确猜测的那样,SSH.NET 不支持 OPENSSH 格式.

  • If you correct the Encoding.UTF32 to Encoding.UTF8, it will still fail though. As you have rightly guesses that the SSH.NET does not support the OPENSSH format.

    最新的 SSH.NET 2020.0.0 仅支持 ssh-ed25519 密钥的 OPENSSH 格式.它会说:

    The latest SSH.NET 2020.0.0 supports the OPENSSH format for ssh-ed25519 keys only. It will say:

    openssh 密钥类型:不支持 ssh-rsa

    旧版本根本不支持这种格式,他们会说:

    Older versions did not support this format at all, they will say:

    不支持密钥OPENSSH"

    这些 OPENSSH 密钥由最新版本的 OpenSSH(7.8 及更高版本)生成.

    These OPENSSH keys are generated by recent versions of OpenSSH (7.8 and newer).

    您可以使用 ssh-keygen 来转换密钥到经典 OpenSSH 格式:

    You can use ssh-keygen to convert the key to the classic OpenSSH format:

    ssh-keygen -p -f file -m pem -P passphrase -N passphrase
    

    (如果密钥未使用密码短语加密,请使用 " 而不是 passphrase)

    (if the key is not encrypted with a passphrase, use "" instead of passphrase)

    对于 Windows 用户:请注意 ssh-keygen.exe 现在已内置在 Windows 10 中.并且可以是 从 Microsoft Win32-OpenSSH 项目下载,用于旧版 Windows.

    For Windows users: Note that ssh-keygen.exe is now built-in in Windows 10. And can be downloaded from Microsoft Win32-OpenSSH project for older versions of Windows.

    在 Windows 上,您还可以使用 PuTTYgen(来自 PuTTY 包):

    On Windows, you can also use PuTTYgen (from PuTTY package):

    • 启动 PuTTYgen
    • 加载密钥
    • 转到转化率>导出 OpenSSH 密钥.
      对于 RSA 密钥,它将使用经典格式.

    如果您使用 ssh-keygen 创建新密钥,只需添加 -m PEM经典格式生成新密钥:

    If you are creating a new key with ssh-keygen, just add -m PEM to generate the new key in the classic format:

    ssh-keygen -m PEM
    

  • 这有效:

    var key = @"-----BEGIN RSA PRIVATE KEY-----
    MIIEoQIBAAKCAQEAjQtUVe5ewYro8utRXqftODAg9zpqkP3+kmpVoIIXe27ga3jy
    rUzJtCItx1jqjdZypcPrxB+8j2nUJj108Bw5y088OquGz/GhBk0uTN+/kQ7K4vnE
    y7X4VuKDOloAAXRcyUZ621RWzM/Xp5M/lU6kuR+TtlMd1lpWmeKSNXhfP4gYaPMT
    p/4+u247ng+HIwW9CLl5PGIO/AXImt/XyVMKMqw8IgGRKaYBBL2R9wLPBAYVzgHe
    svfSDYdeNyh3XG+gbLgq2lmC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Wg3x/Ls8XPRmpNrV9285g9y8nnO5xPMQx
    ylBjOvlEju32wPvqetVMb+sdKPjhOIqJ4yzX6p4OqcxSUnQq6IfA6O60ddKex+GG
    mM0CgYAqm3oISjWhyq+4iN4C2eQXtAdAjkA7c0ZaHRFFapOI+ZqPXEi+oyhuPj4Z
    ZeUcZkS9cYKkgRGDG33p1ZiR+iY7I6Qv8Seyu/oh75/lL0imiq7yKTmizL2WIow7
    lT1yYNZJI/YNEdWPKOZl6kYsHliW8TaUk0yvDC/LPADabjvjzg==
    -----END RSA PRIVATE KEY-----
    ";
    
    MemoryStream keyStream = new MemoryStream(Encoding.UTF8.GetBytes(key));
    new PrivateKeyFile(keyStream);
    


    相关问题:Renci.SshNet.Common.SshException: Invalid private key file"使用 SSH.NET 从配置字符串加载 SSH 私钥时.

    这篇关于使用“OPENSSH"C# 中带有 SSH.NET 的字符串中的私钥文件失败,并显示“无效的私钥文件";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    09-11 09:41