PHP ssh2_auth_pubkey_file():使用公钥的身份验证失败:无效的密钥数据,未进行base64编码 [英] PHP ssh2_auth_pubkey_file(): Authentication failed using public key: Invalid key data, not base64 encoded

查看:512
本文介绍了PHP ssh2_auth_pubkey_file():使用公钥的身份验证失败:无效的密钥数据,未进行base64编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP5.3.3中(在CentOS和apache2上),我试图通过php脚本连接到SFTP.该代码从构造函数中获取密钥和服务器详细信息

In PHP5.3.3 (on CentOS and apache2) I am attempting to connect to a SFTP via a php script. The code grabs the keys and server details from the constructor

function __construct(){
    $this->host     = 'servername.loc';
    $this->port     = SFTP_PORT;
    $this->auth_user    = 'username';
    $this->auth_pub     = '/data/home/username/.ssh/id_rsa.pub';
    $this->auth_priv    = '/data/home/username/.ssh/id_rsa';
    $this->auth_pass    = null;
    $this->connection   = null;
}

,并使用这些详细信息创建连接.

and uses those details to create the connection.

    private function connect(){
    if (!($this->connection = ssh2_connect($this->host, $this->port))) {
        $this->response  = array('code' => "20",
                                 "message" => "Error connecting to SFTP server.");
        return false;
    }
    if (!ssh2_auth_pubkey_file($this->connection, $this->auth_user, $this->auth_pub,
                                $this->auth_priv, $this->auth_pass)) {
        $this->response  = array('code' => "40",
                                 "message" => "Error authenticating to SFTP server with key.");
        $this->disconnect();
        return false;
    }
}

我得到的结果是对ssh2_auth_pubkey_file()的调用出错.

The result I get is an error on the call to ssh2_auth_pubkey_file().

错误是:

" ssh2_auth_pubkey_file():使用公共密钥的USERNAME身份验证失败:无效的密钥数据,未进行base64编码 "

"ssh2_auth_pubkey_file(): Authentication failed for USERNAME using public key: Invalid key data, not base64 encoded"

密钥上没有密码,我可以通过CLI ssh使用这些密钥来手动连接到服务器.

There is no password on the key, and I can use these keys via CLI ssh to connect to the server manually.

我很困惑.我是否需要以某种方式对密钥进行编码?有建议吗?

I am stumped. Do I need to encode the keys somehow? Suggestions?

推荐答案

您提到的先决条件,即pubkey文件中没有任何注释,甚至没有尾随换行符都是不正确的(换行符在您认为时很荒谬通过).

The prerequisite that you mention, namely the pubkey file not to have any comments and not even have a trailing newline is incorrect (and the newline thingy absurd when you think it through).

如果脚本失败,则可能会出现问题.不久,偶然发现了ssh2错误,该错误使ssh2在通过libgcrypt而不是openssl进行编译时失败.解决方法是使用openssl创建PEM格式的私钥文件的PEM格式副本:

If your scriptfails, you have prob. sooner stumbled into the ssh2 bug that makes ssh2 fail when it is compiled wuth libgcrypt instead of openssl. The workaround is to create a PEM formatted copy of your private key file in PEM format with openssl:

~/.ssh> openssl rsa -in id_rsa -out id_rsa.pem

然后,在PHP脚本的ssh2_auth_pubkey_file()中,将id_rsa.pem用作privkey文件而不是id_rsa,并忽略密码短语. 那应该可以使它起作用.

Then, in ssh2_auth_pubkey_file() in your PHP script, use id_rsa.pem as privkey file instead of id_rsa, and omit the passphrase. That should make it work.

这篇关于PHP ssh2_auth_pubkey_file():使用公钥的身份验证失败:无效的密钥数据,未进行base64编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆