Python asymmetric encryption: using pre-created prv / pub keys

Ok first yes, I searched google and stackoverflow and did some reading (more than 4 hours, ONLY in this meeting) did not find what I need for these reasons:

  • Many of them suggest just running exe, for example gpg.exe (http://stackoverflow.com/questions/1020320)

  • Some suggested using PyCrypto or other libraries and looking at them: a) I cannot find how to use any of their APIs, b) I cannot find how to import pre-existing prv / pub key or c) they use unsafe RandomPool (and I'm trying to update it just by asking about problems)

  • Some mention this in passing, but I could not find what they were connected with (or there was no reference at all.

So, I know that I am asking you, stackoverflow user colleagues, how I can do this, take the public key line (or path (I could just write it to a temporary file) (I plan to just have it as a pub_key = "line. .. "))) and use it to sign and protect a string (which will be posted on the forum (JSON to update my application)?

They are also RSA keys (Putty Gen 4096 bits SSH-2-RSA), which are generated using PuttyGen (can be in any format (OpenSSH, ssh.com, ppk)

This is what the public key looks like

---- BEGIN SSH2 PUBLIC KEY ----

Comment: "rsa-key-20101003"

AAAAB3NzaC1yc2EAAAABJQAAAgEAi + 91fFsxZ7k1UuudSe5gZoavwARUyZScCtdf WQ0ROoJC + XIqW5vVJfgmr + A1jLS5m4wNsrCqeyoX2B22T6iEwqVXrXt3QcbccKMu WkLKFK1h67q6Coc + 3eOTmKrOuZbWc19YQgybdkR/GxF7XAbq4NCGNaCDtMOqX8Q2 /a 9fAYqVdTwg9trpcz3whNmdLk/B0edOABKuVX51UdLV + ZggK503 + uAb1JiIIj0 mARwR/HNo4oRLMLf2PjuZsGVYYjJDdVJBU6AN4PUQSRRRPL4 + YmsrLJb/TpfJeXA vj4KZMNJv15YXz7/iMZMKznDtr2RJX5wbSpuTUBNZveA7YiIHxvvvis38b/lX9SJ SYPfZ9CeQY6MvQgG2zwDTOOvKgOIB4sTGMXfcoxB8AF/QXOcxWFJkZoj36rvMd9n Po6szLjHXwcEUOUvvQfG4VvdQA0H5gGLHqYL1EehRsgi5qcCoFPaZW2K09ErKcS0 MbrLFjBkQ9KmqAM38bvM8UhCWAMA9VXOGHMxUHBV4Bir9alGS4VX0B8Y0b3dZ + 7I MKkHMCwdEUJf7QVdGxGuSQtVsq8RZbIpk3g7wtv8f6I/iEC58ekdrH35tq5 + 1ilW dkk9 + rrhUy4qrZ + HFi7AeemybpiumbSnebvnkMaIPAOo23V8C9BQ0iuxx4gIZf10 O + TPSK8 =

---- END SSH2 PUBLIC KEY ----

→ , , PKCS1, M2Crypto ( PEM)

, SSH Public Key File Format (RFC: http://www.ietf.org/rfc/rfc4716.txt)

, , , SSH Public Key File Format: (

Twisted,

http://www.java2s.com/Open-Source/Python/Network/Twisted/Twisted-1.0.3/Twisted-1.0.3/twisted/conch/ssh/keys.py.htm

SO ?

+3
2

, ,

from twisted.conch.ssh import keys as Keys
import base64

public_key = """\
---- BEGIN SSH2 PUBLIC KEY ----
Comment: "rsa-key-20101003"
AAAAB3NzaC1yc2EAAAABJQAAAgEAi+91fFsxZ7k1UuudSe5gZoavwARUyZScCtdf
WQ0ROoJC+XIqW5vVJfgmr+A1jLS5m4wNsrCqeyoX2B22T6iEwqVXrXt3QcbccKMu
WkLKFK1h67q6Coc+3eOTmKrOuZbWc19YQgybdkR/GxF7XAbq4NCGNaCDtMOqX8Q2
L/a9fAYqVdTwg9trpcz3whNmdLk/B0edOABKuVX51UdLV+ZggK503+uAb1JiIIj0
mARwR/HNo4oRLMLf2PjuZsGVYYjJDdVJBU6AN4PUQSRRRPL4+YmsrLJb/TpfJeXA
vj4KZMNJv15YXz7/iMZMKznDtr2RJX5wbSpuTUBNZveA7YiIHxvvvis38b/lX9SJ
SYPfZ9CeQY6MvQgG2zwDTOOvKgOIB4sTGMXfcoxB8AF/QXOcxWFJkZoj36rvMd9n
Po6szLjHXwcEUOUvvQfG4VvdQA0H5gGLHqYL1EehRsgi5qcCoFPaZW2K09ErKcS0
MbrLFjBkQ9KmqAM38bvM8UhCWAMA9VXOGHMxUHBV4Bir9alGS4VX0B8Y0b3dZ+7I
MKkHMCwdEUJf7QVdGxGuSQtVsq8RZbIpk3g7wtv8f6I/iEC58ekdrH35tq5+1ilW
dkk9+rrhUy4qrZ+HFi7AeemybpiumbSnebvnkMaIPAOo23V8C9BQ0iuxx4gIZf10
o+TPSK8=
---- END SSH2 PUBLIC KEY ----"""

key_data = ''.join(public_key.splitlines()[2:-1])# remove begin, end tags and comment
blob = base64.decodestring(key_data)
key = Keys.Key._fromString_BLOB(blob)
+2

  • OpenSSL ( pyOpenSSL) BER PEM
  • paramiko, SSH python .
0

Source: https://habr.com/ru/post/1767808/


All Articles