将 pem 密钥转换为 ssh-rsa 格式
我有一个 der
格式的证书,通过此命令我可以生成一个公钥:
openssl x509 -inform der -in ejbcacert.cer -noout -pubkey > pub1key.pub
其结果是:
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC7vbqajDw4o6gJy8UtmIbkcpnk
O3Kwc4qsEnSZp/TR+fQi62F79RHWmwKOtFmwteURgLbj7D/WGuNLGOfa/2vse3G2
eHnHl5CB8ruRX9fBl/KgwCVr2JaEuUm66bBQeP5XeBotdR4cvX38uPYivCDdPjJ1
QWPdspTBKcxeFbccDwIDAQAB
-----END PUBLIC KEY-----
如何获得这样的公钥? 从证书或 来自这个公钥?
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQC7vbqajDw4o6gJy8UtmIbkcpnkO3Kwc4qsEnSZp/TR+fQi62F79RHWmwKOtFmwteURgLbj7D/WGuNLGOfa/2vse3G2eHnHl5CB8ruRX9fBl/KgwCVr2JaEuUm66bBQeP5XeBotdR4cvX38uPYivCDdPjJ1QWPdspTBKcxeFbccDw==
这是通过以下命令获得的:
ssh-keygen -y -f private_key1.pem > public_key1.pub
I have a certificate in der
format, from it with this command I generate a public key:
openssl x509 -inform der -in ejbcacert.cer -noout -pubkey > pub1key.pub
Which results in this:
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC7vbqajDw4o6gJy8UtmIbkcpnk
O3Kwc4qsEnSZp/TR+fQi62F79RHWmwKOtFmwteURgLbj7D/WGuNLGOfa/2vse3G2
eHnHl5CB8ruRX9fBl/KgwCVr2JaEuUm66bBQeP5XeBotdR4cvX38uPYivCDdPjJ1
QWPdspTBKcxeFbccDwIDAQAB
-----END PUBLIC KEY-----
How can I obtain a public key like this? Either from certificate or
from this public key?
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQC7vbqajDw4o6gJy8UtmIbkcpnkO3Kwc4qsEnSZp/TR+fQi62F79RHWmwKOtFmwteURgLbj7D/WGuNLGOfa/2vse3G2eHnHl5CB8ruRX9fBl/KgwCVr2JaEuUm66bBQeP5XeBotdR4cvX38uPYivCDdPjJ1QWPdspTBKcxeFbccDw==
This was obtained with this command:
ssh-keygen -y -f private_key1.pem > public_key1.pub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
不需要编译东西。 您可以对
ssh-keygen
执行相同操作:将从
pub1key.pub
读取 openssl 格式的公钥,并以 OpenSSH 格式输出。注意:在某些情况下,您需要指定输入格式:
来自 ssh-keygen 文档(来自 man ssh-keygen):
No need to compile stuff. You can do the same with
ssh-keygen
:will read the public key in openssl format from
pub1key.pub
and output it in OpenSSH format.Note: In some cases you will need to specify the input format:
From the ssh-keygen docs (From man ssh-keygen):
不需要脚本或其他“技巧”:
openssl
和ssh-keygen
就足够了。 我假设密钥没有密码(这很糟糕)。生成 RSA 对
以下所有方法都会以相同的格式给出 RSA 密钥对
使用 openssl (man genrsa)
在 OpenSSL v1.0.1 中
genrsa
已被genpkey
取代,因此这是执行此操作的新方法(man genpkey):<前><代码>openssl genpkey -算法 RSA -out dummy-genpkey.pem -pkeyopt rsa_keygen_bits:2048
使用 ssh-keygen
将 DER 转换为 PEM
如果您有 DER 格式的 RSA 密钥对,您可能需要将其转换为 PEM 以允许以下格式转换:
生成:
转换:
提取公钥来自 PEM 格式的 RSA 对
PEM 格式:
OpenSSH v2 格式 参见:
Notes
操作系统和软件版本:
参考文献:
No need for scripts or other 'tricks':
openssl
andssh-keygen
are enough. I'm assuming no password for the keys (which is bad).Generate an RSA pair
All the following methods give an RSA key pair in the same format
With openssl (man genrsa)
In OpenSSL v1.0.1
genrsa
is superseded bygenpkey
so this is the new way to do it (man genpkey):With ssh-keygen
Converting DER to PEM
If you have an RSA key pair in DER format, you may want to convert it to PEM to allow the format conversion below:
Generation:
Conversion:
Extract the public key from the PEM formatted RSA pair
in PEM format:
in OpenSSH v2 format see:
Notes
OS and software version:
References:
为了回答我自己的问题,在 openssl 邮件列表上发帖后得到了这个:
Here is C code to conversion from an OpenSSL public key to an OpenSSH public key。
您可以从此链接获取代码并自行编译:
To answer my own question, after posting on openssl mailing list got this:
Here is C code to convert from an OpenSSL public key to an OpenSSH public key.
You can grab the code from this link and compile it yourself:
以下脚本将获取 Base64 编码的 DER 格式的 ci.jenkins-ci.org 公钥证书,并将其转换为 OpenSSH 公钥文件。 此代码假设使用 2048 位 RSA 密钥,并从 Ian Boyd 的 答案。 我在这篇文章的评论中详细解释了它的工作原理 在詹金斯维基百科中。
The following script would obtain the ci.jenkins-ci.org public key certificate in base64-encoded DER format and convert it to an OpenSSH public key file. This code assumes that a 2048-bit RSA key is used and draws a lot from this Ian Boyd's answer. I've explained a bit more how it works in comments to this article in Jenkins wiki.
我做了
信用在此处
I did with
Credit goes here
FWIW,此 BASH 脚本将采用 PEM 或 DER 格式的 X.509 证书或 OpenSSL 公钥文件(也是 PEM 格式)作为第一个参数,并释放 OpenSSH RSA 公钥。 这扩展了上面 @mkalkov 的答案。 要求是
cat
、grep
、tr
、dd
、xxd
、sed
、xargs
、文件
、uuidgen
、base64
、openssl
(1.0+),当然还有bash
。 除了openssl
(包含base64
)之外的所有内容都几乎可以保证成为任何现代 Linux 系统上基本安装的一部分,除了xxd
(其中Fedora 显示在vim-common
包中)。 如果有人想清理它并使其变得更好,请注意讲师。FWIW, this BASH script will take a PEM- or DER-format X.509 certificate or OpenSSL public key file (also PEM format) as the first argument and disgorge an OpenSSH RSA public key. This expands upon @mkalkov's answer above. Requirements are
cat
,grep
,tr
,dd
,xxd
,sed
,xargs
,file
,uuidgen
,base64
,openssl
(1.0+), and of coursebash
. All exceptopenssl
(containsbase64
) are pretty much guaranteed to be part of the base install on any modern Linux system, except maybexxd
(which Fedora shows in thevim-common
package). If anyone wants to clean it up and make it nicer, caveat lector.这对我有用,因为我只能访问公钥:
This is what worked for me, since i only had access to the Public Key:
请注意,即使是当前的 Win32-OpenSSH 版本似乎也存在阻止此转换发生的错误,如 此专用 GitHub 问题页面。
可以通过以下行为确认问题:
唯一的替代方案似乎不使用 Win32-OpenSSH 来执行此特定任务。
Please be aware that even current Win32-OpenSSH builds seem to have a bug preventing this conversion from happening, as referenced here on this dedicated GitHub issue page.
Issue can be confirmed with this behaviour :
Only alternative seems to not use Win32-OpenSSH for this specific task.
针对 Oracle 云实例公钥请求进行测试:
私钥生成(带密码):
公钥密钥提取:
公钥 “ssh-rsa”格式的密钥转换:
确保 SSH 文件夹和密钥的权限如下(公钥必须为 644,私钥必须为 400) :
Tested for Oracle Cloud Instance public key request:
Private key generation (with passphrase):
Public key extraction:
Public key conversion in "ssh-rsa" format:
Ensure that the permissions for the SSH folder and keys are as follows (public keys must be 644, private keys must be 400):
您还可以使用 phpseclib 库
$key 可以是公共或私有字符串。 如果它是一个文件,则只需将其包装在
file_get_contents($key)
中You can also use phpseclib library
$key can be a public or private string. If it is a file then just wrap it in
file_get_contents($key)
不要重新发明轮子并使用现有的东西,这对每个人都更好。
我正在使用带有 Puttygen 的 Ubuntu 服务器,这是一个很棒的工具。 有几行,或者只有一行,如果你愿意的话(单行有一些 && 和 ||):
就这样..
注意:如果可能的话,看看 puttygen 帮助,它有很多很酷的和易于访问的东西。 例如,这些是可以使用 puttygen 使用“-O”选项的输出格式:
Don't reinvent the wheel and use what exists, it's better for everyone.
I'm using an Ubuntu server with Puttygen, which is an excellent tool. There are a few lines, or just one, if you prefer (single liner with a few && and ||):
Just it..
Note: If possible, take a look at puttygen help, it has lots of cool and easy-to-access stuff. For example, theses are the output formats that can be used with puttygen using '-O' option: