我一直尝试使用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 …Run Code Online (Sandbox Code Playgroud) 我目前正将我的项目从Laravel 4迁移到Laravel 5,我仍然是Laravel和OOP的新手用户,但到目前为止一切顺利.
但是,在我的L4项目中,我使用phpseclib生成SSH密钥,我通过以下方式导入:
MyController.php
...
include('../app/libs/phpseclib0.3.10/Crypt/RSA.php');
$rsa = new Crypt_RSA();
...
Run Code Online (Sandbox Code Playgroud)
这似乎不再起作用了:
syntax error, unexpected 'if' (T_IF), expecting identifier (T_STRING)
Run Code Online (Sandbox Code Playgroud)
... /应用/库/ phpseclib0.3.10 /地穴/ RSA.php
/**
* Include Crypt_Random
*/
// the class_exists() will only be called if the crypt_random_string function hasn't been defined and
// will trigger a call to __autoload() if you're wanting to auto-load classes
// call function_exists() a second time to stop the include_once from being called outside
// of the auto loader
if (!function_exists('crypt_random_string')) …Run Code Online (Sandbox Code Playgroud)