she*_*ron 7 php openssl openssh rsa
我一直尝试使用PHP的openssl扩展生成RSA密钥对,并将结果保存为OpenSSH兼容密钥对 - 意味着私钥是PEM编码(这很容易),公钥以OpenSSH特定格式存储以下形式:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABA...more base64 encoded stuff...
Run Code Online (Sandbox Code Playgroud)
据我所知,这种格式包括:
我尝试使用PHP的pack()函数实现这一点,但无论我尝试什么,结果永远不会等同于我ssh-keygen -y -f在openssl生成的相同RSA私钥上使用命令所得到的结果.
这是我的代码的简化版本:
<?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");
?>
Run Code Online (Sandbox Code Playgroud)
关于如何在不依赖ssh-keygen的情况下做到这一点的任何提示?
好吧,我刚刚解决了我自己的问题,仔细看看从转换pem键到ssh-rsa格式的C实现引用(我之前做过,但显然我错过了一些重要的东西).我需要AND和N的第一个字符和0x80,如果匹配则在数字的开头添加另一个NULL字符并分别将大小增加1.
我不知道为什么这样做(我在网上搜索时没有找到任何参考)但是它有效.
我只对此进行了基本测试,但似乎运行良好,这里我的代码:
<?php
$privKey = openssl_pkey_get_private($rsaKey);
$pubKey = sshEncodePublicKey($privKey);
echo "PHP generated RSA public key:\n$pubKey\n\n";
function sshEncodePublicKey($privKey)
{
$keyInfo = openssl_pkey_get_details($privKey);
$buffer = pack("N", 7) . "ssh-rsa" .
sshEncodeBuffer($keyInfo['rsa']['e']) .
sshEncodeBuffer($keyInfo['rsa']['n']);
return "ssh-rsa " . base64_encode($buffer);
}
function sshEncodeBuffer($buffer)
{
$len = strlen($buffer);
if (ord($buffer[0]) & 0x80) {
$len++;
$buffer = "\x00" . $buffer;
}
return pack("Na*", $len, $buffer);
}
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6194 次 |
| 最近记录: |