不使用ssh-keygen生成SSH密钥对(私有/公共) [英] Generate SSH Keypairs (private/public) without ssh-keygen

查看:227
本文介绍了不使用ssh-keygen生成SSH密钥对(私有/公共)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究需要生成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密钥生成RSA公用密钥和SSH公用密钥,也不是同一回事. SSH公钥类似于以下内容:

ssh-rsa AAAAB3NzaC1yc2EAAAABIwA.....

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

解决方案

事实证明,这比我预期的要复杂得多.我最终写了 SSHKey gem 将其发布(源代码 RFC#4251 的第5部分中定义. >

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?

解决方案

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.

这篇关于不使用ssh-keygen生成SSH密钥对(私有/公共)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆