简体   繁体   中英

Problem with connect to SFTP server via private.key

I have problem with connect to SFTP server via private key. First, I had the id_rsa.pkk file. I used the PuttyGen program to load the key and convert it to the open-ssh private key as privateKey.pem starting as:

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,BA05B7BC99E7A2CE
....

and publicKey.pub as

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC9kfwanzIXJ4BAgd3myWoeSDDGprDNSTYalJ+RON4AYCjiGeDu0sOnjqVtoWMRxQ820kvsv4iUMnXsTrewyklCNLsyUNt62htuvyvcVLSHtLqJFkBXrhgncF7+SEXiakQivYD50EifpU4ZlMGN/LzwQ40zRa4uXQyaGH16ifxPtggKjfaVNKXqfsdkUJ7JlnP6VlLV3pQM/ckLHIWB9FtMWDB1fHirqwjJEZAC04ez4askDtP4jUMcfYtQOynbZuRCB33qLf2GvWpafmaC83m7i53ee34MuOAixk6HOwXwTzTc8wVwPndSXSjtAGRQ/ScjQ3dpSWLhZ7lFGUe/viwN imported-openssh-key

When I send it via console:

sftp -oIdentityFile=privateKey.pem -oServerAliveInterval=60 -oServerAliveCountMax=10000 -oTCPKeepAlive=yes username@host

everything is OK. Console asks me for a password. When I enter the password, it connects me to the server correctly...

sftp>

But when I try to use

$connection = ssh2_connect($host, $port);
ssh2_auth_pubkey_file($connection, $username, $pubKey, $privKey, $passphrase);

using $pubKey as publicKey.pub and $privKey as privateKey.pem I have a error like

ssh2_auth_pubkey_file(): Authentication failed for username using public key

When I try to use phpseclib as

$key = new Crypt_RSA();
$key->setPassword('password');
$key->loadKey(file_get_contents('privateKey.pem'));
$sftp = new Net_SFTP('host', 22);
if ($sftp->login('username', $key)) {
echo 'OK';
} else {
echo 'LOGIN FAILED';
}

I have LOGIN FAILED.

I also tried to convert the ppk file via the console:

puttygen id_rsa.ppk -O private-openssh -o privateKey.pem
and next
puttygen id_rsa.ppk -O public-openssh -o publicKey.pub

but same problem... What am I doing wrong?

EDIT: return $sftp->getLog();

<-
00000000  53:53:48:2d:32:2e:30:2d:4f:70:65:6e:53:53:48:5f  SSH-2.0-OpenSSH_
00000010  37:2e:34:0d:0a                                   7.4..

->
00000000  53:53:48:2d:32:2e:30:2d:70:68:70:73:65:63:6c:69  SSH-2.0-phpsecli
00000010  62:5f:31:2e:30:20:28:6f:70:65:6e:73:73:6c:2c:20  b_1.0 (openssl, 
00000020  62:63:6d:61:74:68:29:0d:0a                       bcmath)..

-> NET_SSH2_MSG_KEXINIT (since last: 0,0346, network: 0s)
00000000  ba:5e:c6:b4:17:db:32:79:5c:2c:19:0e:5f:78:b7:66  .^....2y\,.._x.f
00000010  00:00:00:7e:64:69:66:66:69:65:2d:68:65:6c:6c:6d  ...~diffie-hellm
00000020  61:6e:2d:67:72:6f:75:70:31:2d:73:68:61:31:2c:64  an-group1-sha1,d
00000030  69:66:66:69:65:2d:68:65:6c:6c:6d:61:6e:2d:67:72  iffie-hellman-gr
00000040  6f:75:70:31:34:2d:73:68:61:31:2c:64:69:66:66:69  oup14-sha1,diffi
00000050  65:2d:68:65:6c:6c:6d:61:6e:2d:67:72:6f:75:70:2d  e-hellman-group-
00000060  65:78:63:68:61:6e:67:65:2d:73:68:61:31:2c:64:69  exchange-sha1,di
00000070  66:66:69:65:2d:68:65:6c:6c:6d:61:6e:2d:67:72:6f  ffie-hellman-gro
00000080  75:70:2d:65:78:63:68:61:6e:67:65:2d:73:68:61:32  up-exchange-sha2
00000090  35:36:00:00:00:29:72:73:61:2d:73:68:61:32:2d:32  56...)rsa-sha2-2
000000a0  35:36:2c:72:73:61:2d:73:68:61:32:2d:35:31:32:2c  56,rsa-sha2-512,
000000b0  73:73:68:2d:72:73:61:2c:73:73:68:2d:64:73:73:00  ssh-rsa,ssh-dss.
000000c0  00:00:00:00:00:00:00:00:00:00:39:68:6d:61:63:2d  ..........9hmac-
000000d0  73:68:61:32:2d:32:35:36:2c:68:6d:61:63:2d:73:68  sha2-256,hmac-sh
000000e0  61:31:2d:39:36:2c:68:6d:61:63:2d:73:68:61:31:2c  a1-96,hmac-sha1,
000000f0  68:6d:61:63:2d:6d:64:35:2d:39:36:2c:68:6d:61:63  hmac-md5-96,hmac
00000100  2d:6d:64:35:00:00:00:39:68:6d:61:63:2d:73:68:61  -md5...9hmac-sha
00000110  32:2d:32:35:36:2c:68:6d:61:63:2d:73:68:61:31:2d  2-256,hmac-sha1-
00000120  39:36:2c:68:6d:61:63:2d:73:68:61:31:2c:68:6d:61  96,hmac-sha1,hma
00000130  63:2d:6d:64:35:2d:39:36:2c:68:6d:61:63:2d:6d:64  c-md5-96,hmac-md
00000140  35:00:00:00:04:6e:6f:6e:65:00:00:00:04:6e:6f:6e  5....none....non
00000150  65:00:00:00:00:00:00:00:00:00:00:00:00:00        e.............

<- NET_SSH2_MSG_KEXINIT (since last: 0,0113, network: 0,0112s)
00000000  5a:81:4f:df:f3:f2:41:21:b3:8c:00:99:8b:fa:18:0f  Z.O...A!........
00000010  00:00:01:40:63:75:72:76:65:32:35:35:31:39:2d:73  ...@curve25519-s
00000020  68:61:32:35:36:2c:63:75:72:76:65:32:35:35:31:39  ha256,curve25519
00000030  2d:73:68:61:32:35:36:40:6c:69:62:73:73:68:2e:6f  -sha256@libssh.o
00000040  72:67:2c:65:63:64:68:2d:73:68:61:32:2d:6e:69:73  rg,ecdh-sha2-nis
00000050  74:70:32:35:36:2c:65:63:64:68:2d:73:68:61:32:2d  tp256,ecdh-sha2-
00000060  6e:69:73:74:70:33:38:34:2c:65:63:64:68:2d:73:68  nistp384,ecdh-sh
00000070  61:32:2d:6e:69:73:74:70:35:32:31:2c:64:69:66:66  a2-nistp521,diff
00000080  69:65:2d:68:65:6c:6c:6d:61:6e:2d:67:72:6f:75:70  ie-hellman-group
00000090  2d:65:78:63:68:61:6e:67:65:2d:73:68:61:32:35:36  -exchange-sha256
000000a0  2c:64:69:66:66:69:65:2d:68:65:6c:6c:6d:61:6e:2d  ,diffie-hellman-
000000b0  67:72:6f:75:70:31:36:2d:73:68:61:35:31:32:2c:64  group16-sha512,d
000000c0  69:66:66:69:65:2d:68:65:6c:6c:6d:61:6e:2d:67:72  iffie-hellman-gr
000000d0  6f:75:70:31:38:2d:73:68:61:35:31:32:2c:64:69:66  oup18-sha512,dif
000000e0  66:69:65:2d:68:65:6c:6c:6d:61:6e:2d:67:72:6f:75  fie-hellman-grou
000000f0  70:2d:65:78:63:68:61:6e:67:65:2d:73:68:61:31:2c  p-exchange-sha1,
00000100  64:69:66:66:69:65:2d:68:65:6c:6c:6d:61:6e:2d:67  diffie-hellman-g
00000110  72:6f:75:70:31:34:2d:73:68:61:32:35:36:2c:64:69  roup14-sha256,di
00000120  66:66:69:65:2d:68:65:6c:6c:6d:61:6e:2d:67:72:6f  ffie-hellman-gro
00000130  75:70:31:34:2d:73:68:61:31:2c:64:69:66:66:69:65  up14-sha1,diffie
00000140  2d:68:65:6c:6c:6d:61:6e:2d:67:72:6f:75:70:31:2d  -hellman-group1-
00000150  73:68:61:31:00:00:00:41:73:73:68:2d:72:73:61:2c  sha1...Assh-rsa,
00000160  72:73:61:2d:73:68:61:32:2d:35:31:32:2c:72:73:61  rsa-sha2-512,rsa
00000170  2d:73:68:61:32:2d:32:35:36:2c:65:63:64:73:61:2d  -sha2-256,ecdsa-
00000180  73:68:61:32:2d:6e:69:73:74:70:32:35:36:2c:73:73  sha2-nistp256,ss
00000190  68:2d:65:64:32:35:35:31:39:00:00:00:af:63:68:61  h-ed25519....cha
000001a0  63:68:61:32:30:2d:70:6f:6c:79:31:33:30:35:40:6f  cha20-poly1305@o
000001b0  70:65:6e:73:73:68:2e:63:6f:6d:2c:61:65:73:31:32  penssh.com,aes12
000001c0  38:2d:63:74:72:2c:61:65:73:31:39:32:2d:63:74:72  8-ctr,aes192-ctr
000001d0  2c:61:65:73:32:35:36:2d:63:74:72:2c:61:65:73:31  ,aes256-ctr,aes1
000001e0  32:38:2d:67:63:6d:40:6f:70:65:6e:73:73:68:2e:63  28-gcm@openssh.c
000001f0  6f:6d:2c:61:65:73:32:35:36:2d:67:63:6d:40:6f:70  om,aes256-gcm@op
00000200  65:6e:73:73:68:2e:63:6f:6d:2c:61:65:73:31:32:38  enssh.com,aes128
00000210  2d:63:62:63:2c:61:65:73:31:39:32:2d:63:62:63:2c  -cbc,aes192-cbc,
00000220  61:65:73:32:35:36:2d:63:62:63:2c:62:6c:6f:77:66  aes256-cbc,blowf
00000230  69:73:68:2d:63:62:63:2c:63:61:73:74:31:32:38:2d  ish-cbc,cast128-
00000240  63:62:63:2c:33:64:65:73:2d:63:62:63:00:00:00:af  cbc,3des-cbc....
00000250  63:68:61:63:68:61:32:30:2d:70:6f:6c:79:31:33:30  chacha20-poly130
00000260  35:40:6f:70:65:6e:73:73:68:2e:63:6f:6d:2c:61:65  5@openssh.com,ae
00000270  73:31:32:38:2d:63:74:72:2c:61:65:73:31:39:32:2d  s128-ctr,aes192-
00000280  63:74:72:2c:61:65:73:32:35:36:2d:63:74:72:2c:61  ctr,aes256-ctr,a
00000290  65:73:31:32:38:2d:67:63:6d:40:6f:70:65:6e:73:73  es128-gcm@openss
000002a0  68:2e:63:6f:6d:2c:61:65:73:32:35:36:2d:67:63:6d  h.com,aes256-gcm
000002b0  40:6f:70:65:6e:73:73:68:2e:63:6f:6d:2c:61:65:73  @openssh.com,aes
000002c0  31:32:38:2d:63:62:63:2c:61:65:73:31:39:32:2d:63  128-cbc,aes192-c
000002d0  62:63:2c:61:65:73:32:35:36:2d:63:62:63:2c:62:6c  bc,aes256-cbc,bl
000002e0  6f:77:66:69:73:68:2d:63:62:63:2c:63:61:73:74:31  owfish-cbc,cast1
000002f0  32:38:2d:63:62:63:2c:33:64:65:73:2d:63:62:63:00  28-cbc,3des-cbc.
00000300  00:00:d5:75:6d:61:63:2d:36:34:2d:65:74:6d:40:6f  ...umac-64-etm@o
00000310  70:65:6e:73:73:68:2e:63:6f:6d:2c:75:6d:61:63:2d  penssh.com,umac-
00000320  31:32:38:2d:65:74:6d:40:6f:70:65:6e:73:73:68:2e  128-etm@openssh.
00000330  63:6f:6d:2c:68:6d:61:63:2d:73:68:61:32:2d:32:35  com,hmac-sha2-25
00000340  36:2d:65:74:6d:40:6f:70:65:6e:73:73:68:2e:63:6f  6-etm@openssh.co
00000350  6d:2c:68:6d:61:63:2d:73:68:61:32:2d:35:31:32:2d  m,hmac-sha2-512-
00000360  65:74:6d:40:6f:70:65:6e:73:73:68:2e:63:6f:6d:2c  etm@openssh.com,
00000370  68:6d:61:63:2d:73:68:61:31:2d:65:74:6d:40:6f:70  hmac-sha1-etm@op
00000380  65:6e:73:73:68:2e:63:6f:6d:2c:75:6d:61:63:2d:36  enssh.com,umac-6
00000390  34:40:6f:70:65:6e:73:73:68:2e:63:6f:6d:2c:75:6d  4@openssh.com,um
000003a0  61:63:2d:31:32:38:40:6f:70:65:6e:73:73:68:2e:63  ac-128@openssh.c
000003b0  6f:6d:2c:68:6d:61:63:2d:73:68:61:32:2d:32:35:36  om,hmac-sha2-256
000003c0  2c:68:6d:61:63:2d:73:68:61:32:2d:35:31:32:2c:68  ,hmac-sha2-512,h
000003d0  6d:61:63:2d:73:68:61:31:00:00:00:d5:75:6d:61:63  mac-sha1....umac
000003e0  2d:36:34:2d:65:74:6d:40:6f:70:65:6e:73:73:68:2e  -64-etm@openssh.
000003f0  63:6f:6d:2c:75:6d:61:63:2d:31:32:38:2d:65:74:6d  com,umac-128-etm
00000400  40:6f:70:65:6e:73:73:68:2e:63:6f:6d:2c:68:6d:61  @openssh.com,hma
00000410  63:2d:73:68:61:32:2d:32:35:36:2d:65:74:6d:40:6f  c-sha2-256-etm@o
00000420  70:65:6e:73:73:68:2e:63:6f:6d:2c:68:6d:61:63:2d  penssh.com,hmac-
00000430  73:68:61:32:2d:35:31:32:2d:65:74:6d:40:6f:70:65  sha2-512-etm@ope
00000440  6e:73:73:68:2e:63:6f:6d:2c:68:6d:61:63:2d:73:68  nssh.com,hmac-sh
00000450  61:31:2d:65:74:6d:40:6f:70:65:6e:73:73:68:2e:63  a1-etm@openssh.c
00000460  6f:6d:2c:75:6d:61:63:2d:36:34:40:6f:70:65:6e:73  om,umac-64@opens
00000470  73:68:2e:63:6f:6d:2c:75:6d:61:63:2d:31:32:38:40  sh.com,umac-128@
00000480  6f:70:65:6e:73:73:68:2e:63:6f:6d:2c:68:6d:61:63  openssh.com,hmac
00000490  2d:73:68:61:32:2d:32:35:36:2c:68:6d:61:63:2d:73  -sha2-256,hmac-s
000004a0  68:61:32:2d:35:31:32:2c:68:6d:61:63:2d:73:68:61  ha2-512,hmac-sha
000004b0  31:00:00:00:15:6e:6f:6e:65:2c:7a:6c:69:62:40:6f  1....none,zlib@o
000004c0  70:65:6e:73:73:68:2e:63:6f:6d:00:00:00:15:6e:6f  penssh.com....no
000004d0  6e:65:2c:7a:6c:69:62:40:6f:70:65:6e:73:73:68:2e  ne,zlib@openssh.
000004e0  63:6f:6d:00:00:00:00:00:00:00:00:00:00:00:00:00  com.............

EDIT: Solution: The problem was solved after using phpseclib version 2.0.3 :)

With phpseclib, the problem is that phpseclib does not offer any cipher to the server at all. What must be due to some misconfiguration of phpseclib on your side.

Make sure you set an include path at the beginning of your code. For example, if you have phpseclib in phpseclib subfolder, use::

set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM