在没有ssh-keygen的情况下生成SSH密钥对(私有/公共)

时间:2022-06-15 14:26:39

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.

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

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

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

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:

遗憾的是,RSA公钥和SSH公钥不是一回事,即使它们可以从相同的RSA密钥生成。 SSH公钥类似于以下内容:

ssh-rsa AAAAB3NzaC1yc2EAAAABIwA.....

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

是否可以在不使用ssh-keygen的情况下生成SSH密钥或将RSA密钥转换为Ruby中的SSH?

3 个解决方案

#1


25  

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:

当遇到问题时可能不是这种情况,但net-ssh库使用两种方法修补OpenSSL :: PKey :: RSA和:: DSA:

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

#ssh_type - 根据需要返回“ssh-rsa”或“ssh-dss”

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.

和#to_blob - 以OpenSSH二进制blob格式返回公钥。如果你对它进行64位编码,它就是你正在寻找的格式。

require 'net/ssh'

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

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

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

#2


15  

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.

事实证明,这比我预想的要复杂得多。我最终编写了SSHKey gem以将其关闭(GitHub上的源代码)。 SSH公钥的编码方式与提供的RSA公钥完全不同。 SSH密钥的数据类型编码在RFC#4251的#5部分中定义。

#3


1  

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/

这里记录了包括密钥加密在内的完整过程:http://stuff-things.net/2009/12/11/generating-rsa-key-pairs-in-ruby/

#1


25  

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:

当遇到问题时可能不是这种情况,但net-ssh库使用两种方法修补OpenSSL :: PKey :: RSA和:: DSA:

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

#ssh_type - 根据需要返回“ssh-rsa”或“ssh-dss”

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.

和#to_blob - 以OpenSSH二进制blob格式返回公钥。如果你对它进行64位编码,它就是你正在寻找的格式。

require 'net/ssh'

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

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

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

#2


15  

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.

事实证明,这比我预想的要复杂得多。我最终编写了SSHKey gem以将其关闭(GitHub上的源代码)。 SSH公钥的编码方式与提供的RSA公钥完全不同。 SSH密钥的数据类型编码在RFC#4251的#5部分中定义。

#3


1  

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/

这里记录了包括密钥加密在内的完整过程:http://stuff-things.net/2009/12/11/generating-rsa-key-pairs-in-ruby/