How do you extract N and E from RSA public key in python?

I have an RSA public key that looks like

-----BEGIN PUBLIC KEY-----
MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEAvm0WYXg6mJc5GOWJ+5jk
htbBOe0gyTlujRER++cvKOxbIdg8So3mV1eASEHxqSnp5lGa8R9Pyxz3iaZpBCBB
vDB7Fbbe5koVTmt+K06o96ki1/4NbHGyRVL/x5fFiVuTVfmk+GZNakH5dXDq0fwv
JyVmUtGYAiMJWPni2hGpAsbyjzLix9UNX5XiYIIrIr55IHtD5u1XNkmYLOdVQ98r
6hez3t2eaE0pP2k+mjRach+2tD93PBZmreHgVZtejumi+ZWLMqpd++AY0AzH0m8E
6sa8JFUAiYZbVtmrcGTCUCkzC2Es1/knSeZ41xki1qD0V3uw/APP8Q+BgbX3SJp0
EQIBAw==
-----END PUBLIC KEY-----

I want to know what from this key, modulo N and exponent E, in python?

Using the pycrypto package, I can load the key as such:

from Crypto.PublicKey import RSA

# read the public key in:
public_key = RSA.importKey(open('key.pub', 'r').read())

but after the documentation pycrypto rsa module It’s not clear how to extract the smaller components. How to do it?

+4
source share
2 answers

After an hour or so, to play around and search on Google and not find a solution here, this is a really simple solution. This is due to how python objects work.

, keydata

,

pub_key = RSA.importKey()

RSA.

['n', 'e', 'd', 'p', 'q', 'u']

, :

print pub_key.n
print pub_key.e

.. .

+5

, alicepublic.pem :

>>>from Crypto.PublicKey import RSA
>>>f = open("alicepublic.pem", "r")
>>>key = RSA.importKey(f.read())
>>>print key.n #displays n
>>>print key.e #displays e

.

0

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


All Articles