相关疑难解决方法(0)

将OpenSSL生成的RSA公钥转换为OpenSSH格式(PHP)

我一直尝试使用PHP的openssl扩展生成RSA密钥对,并将结果保存为OpenSSH兼容密钥对 - 意味着私钥是PEM编码(这很容易),公钥以OpenSSH特定格式存储以下形式:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABA...more base64 encoded stuff...
Run Code Online (Sandbox Code Playgroud)

据我所知,这种格式包括:

  • 明文中的键类型,后跟空格(即"openssh-rsa")
  • base64编码的字符串,表示以下数据:
    • 算法名称的长度(以字节为单位)(在本例中为7)编码为32位无符号长大端
    • 算法名称,在本例中为'ssh-rsa'
    • RSA'e'数字的长度,以字节为单位,编码为32位无符号长大端
    • RSA'e'号码
    • RSA'n'的长度,以字节为单位,编码为32位无符号长大端
    • RSA'n'号码

我尝试使用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)

php openssl openssh rsa

7
推荐指数
1
解决办法
6194
查看次数

将phpseclib整合到Laravel 5中

我目前正将我的项目从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)

php laravel phpseclib

3
推荐指数
2
解决办法
5561
查看次数

标签 统计

php ×2

laravel ×1

openssh ×1

openssl ×1

phpseclib ×1

rsa ×1