【发布时间】:2016-10-03 00:52:52
【问题描述】:
我一直在我的应用程序中使用 RSA 密钥。我使用以下代码将 RSA 密钥从 OpenSSL 转换为 OpenSSH 格式。它非常适合 RSA 密钥。现在我想支持 DSA 密钥。但我的转换代码不适用于 DSA 密钥。我需要进行哪些修改才能使其与 DSA 密钥一起使用?
$private_key = openssl_pkey_get_private($rsaKey);
$public_key = sshEncodePublicKey($private_key);
echo "RSA public key in OpenSSH format:\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);
}
【问题讨论】:
标签: php openssl public-key openssh dsa