将PuTTY / ppk私钥加载到Paramiko

时间:2018-08-21 20:08:05

标签: python ssh paramiko ssh-keys ssh-tunnel

我尝试在Windows 10上使用paramikoSSHTunnelForwarder使用Python 3.6创建SSH隧道

我已经与PuTTY建立了连接,并且连接没有问题。

每当我尝试与paramiko建立连接时,都会出现以下错误:

  

paramiko.ssh_exception.SSHException:base64解码错误:填充错误

我尝试使用=== ===填充RSA密钥...在开头和结尾处都使用(在示例中1 x =是最后%4 == 0 ..

我还尝试传递pem文件和ppk版本。

如何使paramiko接受密钥?

import paramiko
from io import StringIO

KEY = """-----BEGIN RSA PRIVATE KEY-----
AAABAQCHzDV+UbIfYV3IlpW6nTMSutC9IpDw7GYbTwctfaMfdckZLEIvbQY56Q7p
Dd8O0lAt74s6zqylJK8enCKzMKcZG09OZFy0agj7uXONh5vi00sa+vs3YnosxQwZ
QA4fYSVaGfTacHB00v0gYcjMqSaQs13nUM+dtn5xnDpKgS195OXy7XKEOIeip0al
QsTB1NbXualOJMREh+3MZC/uj56znvvTCTIjfpCbJkHIvYdrj4GXAom8e3bw8Uzl
ltWJiFXT2pJ31kkFfAjYyle/NpK01ISxDQP3y9F8YuObNVkSlppAYfj/rxHP+hfZ
LeHBQL9jM/Ok+7vBcwoQ1dynKvStAAAAgQDYGxL2O98peWp34lz0TbK86iGdOy0p
ZpcmuvjkmTgdQC/h16AZI5zwloHCO15jicsKU8Hzxf9SJw46R9jwXwIgWWM09HND
OTFQB+gnJQmVp5mO4Un2CIkVVyrnG9y/M9mXCKyxQY8YU7HGaE11iJCvpr9Q53GX
xjxT86Yk/y+s7QAAAIEAugCHIjSOFoNVssD4K3sDLFoP82cXgM7JaqnmeUZ780aw
rpvOFN1snUdY4sbTl+uln1SjvjHWLbWz4qsylsVwBVEDOqyXGcpJUMdnRJ4Y0Pjn
YOaC9tWO3dB936ITNMzPJuAJENJcRk4E3NdUx0n0ZUEflJwUxRSVXFeQutmTi0I8A
AACBALrWGeM7XrcJFJz+cw8p4nNDSRuZgBlDgSquqkEJ0cma7mGCUnq1gFy282LX
pFIDWuuhyERdSy0x1D5UMx34pbQg51MmjOrcMqfG0HpcR0Q/fHE0LVQc6bTCfCvN
sR9oHllLYJguW/Su2EsajM5PmniW0b+kMA51CPIXdPBDWtAQ=
-----END PRIVATE KEY-----"""

private_key_file = StringIO()
private_key_file.write(KEY)
private_key_file.seek(0)

print('\n\nlen(KEY)%4 ==', len(KEY)%4)
print()

mypkey = paramiko.RSAKey.from_private_key(private_key_file)

完整追溯:

len(KEY)%4 == 0

Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\paramiko\pkey.py", line 306, in _read_private_key
data = decodebytes(b(''.join(lines[start:end])))
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\base64.py", line 553, in decodebytes
return binascii.a2b_base64(s)
binascii.Error: Incorrect padding

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "d:/GIT/Production_apps/deploy_data_files/connect_tosql.py", line 37, in <module>
mypkey = paramiko.RSAKey.from_private_key(private_key_file)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\paramiko\pkey.py", line 227, in from_private_key
key = cls(file_obj=file_obj, password=password)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\paramiko\rsakey.py", line 45, in __init__
self._from_private_key(file_obj, password)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\paramiko\rsakey.py", line 173, in _from_private_key
data = self._read_private_key('RSA', file_obj, password)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\paramiko\pkey.py", line 308, in _read_private_key
raise SSHException('base64 decoding error: ' + str(e))
paramiko.ssh_exception.SSHException: base64 decoding error: Incorrect padding

(出于安全原因,我更改了密钥中的一些随机字节,但长度和错误相同)

这是我与PuTTY一起使用的ppk文件:

PuTTY-User-Key-File-2: ssh-rsa
Encryption: none
Comment: user_1@computer
Public-Lines: 6
AAAAB3NzaC1yc2EAAAABJQAAAQEAnQ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eM7XrcJFJz+cw8p4nNDSRuZgBlDgSquqkEJ0cma7mGCUnq1gFy282LX
pFIDWuuhyERdSy0x1D5UMx34pbQg51MmjOrcMqfG0HpcR0Q/fHE0LVQc6bTCfCvN
sR9oHllLYJguW/Su2EsajM5PmniW0b+kNA51CPIXdPBDWtAQ
Private-MAC: d44d6ad6590c05c04eb78770be16ee5e11f0802e

1 个答案:

答案 0 :(得分:1)

Paramiko仅支持OpenSSH密钥格式。

您不能只获取PuTTY格式的密钥文件并添加OpenSSH“标头”。这不会使密钥采用OpenSSH格式。

使用PuTTYgen将PuTTY密钥转换为OpenSSH密钥(转换>导出OpenSSH密钥)。