无需 ssh-keygen 即可生成 SSH 密钥对(私有/公共)

发布于 2024-10-21 13:26:51 字数 585 浏览 7 评论 0原文

我正在开发一个需要生成 SSH 密钥对的 Ruby/Rack 应用程序。尽管我很想从应用程序中调用 ssh-keygen ,但我不能,因为它设计为在 Heroku 上运行,并且它们不支持调用该命令。

我已经能够使用 Ruby 标准库中的 OpenSSL 获取私钥/公钥 RSA 密钥,执行以下操作:

key = OpenSSL::PKey::RSA.generate(2048)
# => -----BEGIN RSA PRIVATE KEY----- ....
key.public_key
# => -----BEGIN RSA PUBLIC KEY----- ....

不幸的是,RSA 公钥和 SSH 公钥不是同一件事,即使它们可以从相同的 RSA 密钥生成。 SSH 公钥如下所示:

ssh-rsa AAAAB3NzaC1yc2EAAAABIwA.....

Is it possible to generated SSH key or conversion RSA key to SSH in Ruby without using ssh-keygen?

I'm working on a Ruby/Rack application that needs to generate SSH keypairs. As much as I'd like to call ssh-keygen from the application, I can't because it's designed to run on Heroku and they don't support calling that command.

I've been able to get private/public RSA keys using OpenSSL in the Ruby standard library doing the following:

key = OpenSSL::PKey::RSA.generate(2048)
# => -----BEGIN RSA PRIVATE KEY----- ....
key.public_key
# => -----BEGIN RSA PUBLIC KEY----- ....

Unfortunately an RSA public key and an SSH public key is not the same thing, even though they can be generated from the same RSA key. An SSH public key looks something like the following:

ssh-rsa AAAAB3NzaC1yc2EAAAABIwA.....

Is it possible to generate SSH keys or convert RSA keys to SSH in Ruby without using ssh-keygen?

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

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

发布评论

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

评论(3

ぇ气 2024-10-28 13:26:51

当您遇到问题时,情况可能并非如此,但是 net-ssh 库补丁< a href="https://net-ssh.github.io/net-ssh/OpenSSL/PKey/RSA.html" rel="noreferrer">OpenSSL::PKey::RSA 和 ::DSA 有两种方法:

#ssh_type - 根据需要返回 "ssh-rsa""ssh-dss"

以及 #to_blob - 返回 OpenSSH 二进制 blob 中的公钥格式。如果您对其进行 base64 编码,这就是您正在寻找的格式。

require 'net/ssh'

key = OpenSSL::PKey::RSA.new 2048

type = key.ssh_type
data = [ key.to_blob ].pack('m0')

openssh_format = "#{type} #{data}"

It may not have been the case when you had the problem, but the net-ssh library patches OpenSSL::PKey::RSA and ::DSA with two methods:

#ssh_type - returns "ssh-rsa" or "ssh-dss" as appropriate

and #to_blob - returns the public key in OpenSSH binary-blob format. If you base64-encode it, it's the format you're looking for.

require 'net/ssh'

key = OpenSSL::PKey::RSA.new 2048

type = key.ssh_type
data = [ key.to_blob ].pack('m0')

openssh_format = "#{type} #{data}"
皓月长歌 2024-10-28 13:26:51

事实证明这比我预期的要复杂得多。我最终编写了 SSHKey gem 来实现它(源代码 GitHub 上)。 SSH 公钥的编码方式与提供的 RSA 公钥完全不同。 SSH 密钥的数据类型编码在 RFC #4251 的第 #5 节中定义。

Turns out this was much more complicated than I anticipated. I ended up writing the SSHKey gem to pull it off (source code on GitHub). SSH Public keys are encoded totally differently from the RSA public key provided. Data type encoding for SSH keys are defined in section #5 of RFC #4251.

故事↓在人 2024-10-28 13:26:51
key.public_key.to_pem

包括密钥加密的完整过程记录在此处: http://stuff-things.net/2009/12/11/generate-rsa-key-pairs-in-ruby/

key.public_key.to_pem

The full process including key encryption is documented here: http://stuff-things.net/2009/12/11/generating-rsa-key-pairs-in-ruby/

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