将 OpenSSL 生成的 RSA 公钥转换为 OpenSSH 格式 (PHP)
我已经尝试使用 PHP 的 openssl 扩展生成 RSA 密钥对并将结果保存为 OpenSSH 兼容密钥对 - 这意味着私钥是 PEM 编码的(这很容易)并且公钥以 OpenSSH 特定格式存储格式如下:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABA...more base64 encoded stuff...
据我所知,此格式包括:
- 明文形式的密钥类型,后跟空格(即“openssh-rsa ”)
- 表示以下数据的 base64 编码字符串:
- 算法名称的长度(以字节为单位)(在本例中为 7),编码为 32 位无符号长大端字节序
- 算法名称,在本例中为“ssh-rsa”
- RSA“e”数字的长度(以字节为单位),编码为 32 位无符号长大端字节序
- RSA“e”编号
- RSA“n”数字的长度(以字节为单位),编码为 32 位无符号长大端字节序
- RSA“n”号
我尝试使用 PHP 的 pack( ) 函数,但无论我尝试什么,结果都不等于我在 openssl 生成的同一个 RSA 私钥上使用 ssh-keygen -y -f 命令得到的结果。
这是我的代码的简化版本:
<?php
// generate private key
$privKey = openssl_pkey_new(array(
'private_key_bits' => 1024,
'private_key_type' => OPENSSL_KEYTYPE_RSA
));
// convert public key to OpenSSH format
$keyInfo = openssl_pkey_get_details($privKey);
$data = pack("Na*", 7, 'ssh-rsa');
$data .= pack("Na*", strlen($keyInfo['rsa']['e']), $keyInfo['rsa']['e']);
$data .= pack("Na*", strlen($keyInfo['rsa']['n']), $keyInfo['rsa']['n']);
$pubKey = "ssh-rsa " . base64_encode($data);
echo "PHP generated RSA public key:\n$pubKey\n\n";
// For comparison, generate public key using ssh-keygen
openssl_pkey_export($privKey, $pem);
$umask = umask(0066); // this is needed for ssh-keygen to work properly
file_put_contents('/tmp/ssh-keygen-test', $pem);
umask($umask);
exec('ssh-keygen -y -f /tmp/ssh-keygen-test', $out, $ret);
$otherPubKey = $out[0];
echo "ssh-keygen generated RSA public key:\n$otherPubKey\n\n";
echo ($pubKey == $otherPubKey ? "yes! they are the same\n" : "FAIL! they are different\n");
?>
有关如何在不依赖 ssh-keygen 的情况下执行此操作的任何提示?
I've been trying for a while to generate an RSA keypair using PHP's openssl extension and save the result as an OpenSSH compatible keypair - meaning the private key is PEM encoded (which is easy) and the public key is stored in an OpenSSH specific format of the following form:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABA...more base64 encoded stuff...
As far as I could gather this format consists of:
- the key type in clear text, followed by space (i.e. "openssh-rsa ")
- A base64 encoded string representing the following data:
- the length of the algorithm name in bytes (in this case 7) encoded as 32 bit unsigned long big endian
- the algorithm name, in this case 'ssh-rsa'
- the length of the RSA 'e' number in bytes, encoded as 32 bit unsigned long big endian
- the RSA 'e' number
- the length of the RSA 'n' number in bytes, encoded as 32 bit unsigned long big endian
- the RSA 'n' number
I've tried implementing this using PHP's pack() function, but no matter what I try the result is never equivalent to what I get from using the ssh-keygen -y -f
command on the same RSA private key generated by openssl.
Here is a simplified version of my code:
<?php
// generate private key
$privKey = openssl_pkey_new(array(
'private_key_bits' => 1024,
'private_key_type' => OPENSSL_KEYTYPE_RSA
));
// convert public key to OpenSSH format
$keyInfo = openssl_pkey_get_details($privKey);
$data = pack("Na*", 7, 'ssh-rsa');
$data .= pack("Na*", strlen($keyInfo['rsa']['e']), $keyInfo['rsa']['e']);
$data .= pack("Na*", strlen($keyInfo['rsa']['n']), $keyInfo['rsa']['n']);
$pubKey = "ssh-rsa " . base64_encode($data);
echo "PHP generated RSA public key:\n$pubKey\n\n";
// For comparison, generate public key using ssh-keygen
openssl_pkey_export($privKey, $pem);
$umask = umask(0066); // this is needed for ssh-keygen to work properly
file_put_contents('/tmp/ssh-keygen-test', $pem);
umask($umask);
exec('ssh-keygen -y -f /tmp/ssh-keygen-test', $out, $ret);
$otherPubKey = $out[0];
echo "ssh-keygen generated RSA public key:\n$otherPubKey\n\n";
echo ($pubKey == $otherPubKey ? "yes! they are the same\n" : "FAIL! they are different\n");
?>
Any tips on how I can do this without relying on ssh-keygen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,我刚刚通过仔细查看 将 pem 密钥转换为 ssh-rsa 格式(我之前做过,但显然我错过了一些重要的东西)。我需要将 N 和 e 的第一个字符与 0x80 进行 AND 运算,如果匹配,则在数字开头添加另一个 NULL 字符,并将大小分别增加 1。
我不确定为什么这样做(我在网上搜索中没有找到任何对此的引用),但它有效。
我只对此进行了基本测试,但它似乎运行良好,这里是我的代码:
Ok, I have just solved my own problem by taking a closer look at the C implementation references from Convert pem key to ssh-rsa format (which I did before but apparently I missed some important stuff). I needed to AND the first character of N and e with 0x80 and if it matches add another NULL character at the beginning of the number and increase the size by 1 respectively.
I am not sure why this is done (I didn't find any reference to this in on-line searching I did) but it works.
I've only done basic tests on this but it seems to work well, and here my code:
更简单的方法,使用 phpseclib,一个纯 PHP RSA 实现:
输出:
来源:
http://phpseclib.sourceforge.net/rsa/examples.html#public,openssh
Much simpler method, using phpseclib, a pure-PHP RSA implementation:
Output:
Source:
http://phpseclib.sourceforge.net/rsa/examples.html#public,openssh