JSch 和 SharpSSH 的主要文件格式是什么?

发布于 2024-12-15 04:29:32 字数 281 浏览 5 评论 0原文

我正在考虑使用 JSch 库(或者实际上是 sharpSSH 它的 C# 端口)。不幸的是,我找不到有关密钥加载功能使用的文件格式的任何文档:

jsch.addIdentity(filename, passphrase);
jsch.setKnownHosts(filename);

私钥和已知主机文件使用什么文件格式?

I'm looking at setting up public key client and server authentication for SFTP using the JSch library (or actually the sharpSSH C# port of it). Unfortunately I cannot find any documentation for the file formats used by the key loading functions:

jsch.addIdentity(filename, passphrase);
jsch.setKnownHosts(filename);

What file format is used by the private key and known hosts files?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷梦 2024-12-22 04:29:32

JSch 使用 OpenSSH 密钥文件 格式(公钥和私钥)。我没有找到这种格式的规范,但您可以使用 OpenSSH 的 ssh-keygen 工具,用于将密钥从/转换为其他格式(Google 将展示其他从/到更多格式转换的工具)。

更新:在 OpenSSH 邮件列表上询问并阅读一些 RFC 后,看起来 OpenSSH 的公钥文件包含(对于版本 2 密钥)RFC 4253(第 6.6 节),只需base64-环绕它(键类型作为前缀,注释字段作为后缀)。我还没有找到私钥文件的规范。

不幸的是,JSch 的官方文档几乎不存在,但我确实写了一些 Javadocs为它。 (虽然它似乎没有提到关键文件格式......我必须解决这个问题。)
JSch Wiki 中还有
手册,其中包含关于公钥的页面身份验证(还没有提到密钥格式:-/)。

已知的hosts文件也与OpenSSH客户端对应的文件格式相同。它的格式在 OpenSSH 的 sshd 手册页SSH 已知主机文件格式部分:

这些文件中的每一行包含以下字段:标记
(可选)、主机名、位、指数、模数、注释。这些字段是
用空格分隔。

这实际上仅适用于 SSH 1 RSA 密钥。对于 SSH 2 密钥,您有一个类型标识符(ecdsa-sha2-nistp256ecdsa-sha2-nistp384ecdsa-sha2-nistp521ssh-dssssh-rsa),然后是 Base-64 编码形式的密钥。 (有关授权密钥文件,请参阅同一手册页的上方部分)。 (我认为 JSch 仅支持 DSA 和 RSA 密钥格式,不支持 ECDSA。)

请注意,这些文件中的行通常有数百个字符
很长,而且您肯定不想手动输入主机密钥。
相反,通过脚本 ssh-keyscan(1) 或通过采取
/etc/ssh/ssh_host_key.pub 并在前面添加主机名。
ssh-keygen(1) 还提供了一些基本的自动编辑功能
~/.ssh/known_hosts 包括删除与主机名匹配的主机和
将所有主机名转换为其散列表示形式。

JSch uses the OpenSSH key file format (both for public and private keys). I didn't find a specification of this format, but you can use OpenSSH's ssh-keygen tool to convert the keys from/to other formats (and Google will show other tools of converting from/to even more formats).

Update: After asking on the OpenSSH mailing list and reading some RFC's, it looks like OpenSSH's public key file contains (for version 2 keys) the public key as specified in RFC 4253 (section 6.6), just with a base64-wrapping around it (and the key type as prefix, and a comment field as postfix). I still didn't yet find a specification for the private key file.

Unfortunately, the official documentation for JSch is almost nonexistent, but I did write some Javadocs for it. (Though it seems not to mention the key file format ... I'll have to fix this.)
There is also the Manual in the JSch Wiki, containing a page about public key authentication (which also doesn't mention the key format yet :-/).

The known hosts file is also in the same format as the corresponding file of the OpenSSH client. It's format is explained in OpenSSH's sshd manual page, section SSH KNOWN HOSTS FILE FORMAT:

Each line in these files contains the following fields: markers
(optional), hostnames, bits, exponent, modulus, comment. The fields are
separated by spaces.

This is actually only correct for SSH 1 RSA keys. For SSH 2 keys, you have a type identifier (ecdsa-sha2-nistp256,ecdsa-sha2-nistp384, ecdsa-sha2-nistp521, ssh-dss or ssh-rsa) and then the key in base-64 encoded form. (See a bit up in the same manual page, for the authorized keys file). (I think JSch only supports the DSA and RSA key formats, no ECDSA.)

Note that the lines in these files are typically hundreds of characters
long, and you definitely don't want to type in the host keys by hand.
Rather, generate them by a script, ssh-keyscan(1) or by taking
/etc/ssh/ssh_host_key.pub and adding the host names at the front.
ssh-keygen(1) also offers some basic automated editing for
~/.ssh/known_hosts including removing hosts matching a host name and
converting all host names to their hashed representations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文