带有公钥/私钥的PHP SFTP

时间:2019-02-06 18:11:56

标签: php ssh connection key sftp

我正在尝试使用SSH密钥验证方法通过PHP脚本连接到SFTP服务器。

我的PHP代码是:

<?php
    $host = 'my_host';
    $port = 22;
    $conn = ssh2_connect($host, $port, array('hostkey'=>'ssh-rsa'));
    $username = 'my_user';
    $pub_key = 'c:/UserKeys/pub_key.pub';
    $pri_key = 'c:/UserKeys/priv_key';
    ssh2_auth_pubkey_file($conn, $username, $pub_key, $pri_key);
?>

我总是收到以下错误消息: Warning: ssh2_auth_pubkey_file(): Authentication failed for my_user using public key: Invalid key data, not base64 encoded in

我的私钥文件:

-----BEGIN DSA PRIVATE KEY-----
MIIDPwIBAAKCAQEA05/xIIgUWMqFh0J/ofIBBx28/K3pzQxw1ViLin3sAwiLVPH1
3a4fSfL+mf+KBjOgWYrNAPiVBN2vQGoPi5hOwsxM4rIls0U/TFigD6riFPE/UMUf
Cdllm0qxdjuHSVilOY6OO68Y0fjyFPpoLS2ggQgRoreexEqaRmO7sM8Ug5BG3JXJ
.
.
.
4ZOb4ihmp13x8spPdfSWWuB7MIMjUdHOzssGs357H4xvzEVtWjhleq+Wc1+jW6HD
sUgfKr1FybmYZlKtSdnfxN5U+KXNrY4J9FQhuFXfn2MZU7s20yf95jOJCLEbAhRF
7RmwYNfgUT0O2/swzGxDiVFjfQ==
-----END DSA PRIVATE KEY-----

我的公钥文件:

---- BEGIN SSH2 PUBLIC KEY ----
Subject: aaaaaa
Comment: "[2048-bit DSA, aaaaaa, 2013-10-07 12:42:06]"
AAAAB3NzaC1kc3MAAAEBANOf8SCIFFjKhYdCf6HyAQcdvPyt6c0McNVYi4p97AMIi1Tx9d
2uH0ny/pn/igYzoFmKzQD4lQTdr0BqD4uYTsLMTOKyJbNFP0xYoA+q4hTxP1DFHwnZZZtK
sXY7h0lYpTmOjjuvGNH48hT6aC0toIEIEaK3nsRKmkZju7DPFIOQRtyVyRJFlmWhomPV8t
ceYJZxmHw7rEQ7/JZ24Gq2wYy/NlCwUp8x/2MyGtMKW23zz9QIuFBUEe9J4IpXfoPWmIVN
.
.
.
vggKfZGuRTbQ5zeeF9Gyt6PKb15+77QQPey3aTXixyojp34jNfaWQ91qxmJeLAWlrluKS9
B+Ab7VB5iUjhkntnTETJzLmkvloljSJL/funlbkGpxiznU6q/YqYvlZ5kB2obdqZ8WmO+u
ssC33vA3yiu7KXuRG+7S5o2Tutj2VBfPbyg5Bpf9FmqyGf3TrYf0x1a4+R4ZOb4ihmp13x
8spPdfSWWuB7MIMjUdHOzssGs357H4xvzEVtWjhleq+Wc1+jW6HDsUgfKr1FybmYZlKtSd
nfxN5U+KXNrY4J9FQhuFXfn2MZU7s20yf95jOJCLEb
---- END SSH2 PUBLIC KEY ----

我正在Internet上查找,但无法使脚本正常工作。

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

确保正确打开了密钥文件。您提供的路径似乎格式不正确(使用正面斜杠而不是反斜杠)。首先尝试使用fopen

调试正确的路径可能会有所帮助