Obtain Exponent and Modulus from RSA key in PEM Format
August 09, 2020
If you understand the math behind RSA algorithm, you should know that RSA consists of several components: , , and such that
Here’s how you can obtain those values from RSA private and public key in PEM format with python.
from Crypto.PublicKey import RSA
private_key_pem='''-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEA3fJtY+4/TSuZ07iVqGq6Xa9QMFeor5JZwy7IfyyNOHeVFRSS
dYBo8WDTHTe4flJsRspHa1pI72v1yoJQJ3M7DFAtAxiHzqhAyYZ4wjdxTN8YBVl5
qqbRr7OKTvM8s3Z+2O7aG+ayIyeFKf0DX2ajFkOD3/ql6ne1pQUPCSjLNRSRYdCe
e03eKVw6ORSBUe262gEDf/UWHisg3o2JaVLcmR3ps4ESRcdx0AZ9hYWONGmHn7Fs
NiSp1ry8SVTOnfdcuvjVVfKLVaOui7nntU5OxQijz+/kk4vy58qDedyHxB24wMt6
S+7a5P++ZO20K4/mBIFmdyGIuvOYJY0Gs7X/EQIDAQABAoIBAQDSqNNjUjMLECNF
baG+5O5XFZtLByi43HQOak0rSCISQo5iH9CTbnotAPXgeY7Hd3wGBOCc6GjpttLo
j598kkatfTv8AECGyfQUU2ozDWSgze+CxFZSv1uvJP5VyVEIFaR4St0CNolGDLC8
FcYpusV40ERPRxxL26uxIYgP7YRSr5nReJrZU3xVaBIPxRVmPnJD/7MhaiwkZI53
Trwgp7Za3cXMLxetSXV4mRcyejKUYac+OWrf9/gvvFWegHRHJPz5AfS6jo8qPPy0
HO+yW3w9psSSYv7n+P5hHk6uqfEnPjp80ke+MxPtRTT2+OpjypToZiL4nNZmppA2
p5vQf0NxAoGBAPvLQ2YGZsP4uvbLrRMbFzaVlfiXnwGU4gJ4R2nf6OnKWhWqTI6W
TxuUS5VJetgSgSU721LjGvyBjwJWa7mMmfHnmrZqW7AKNKVlpUqx7nsUy9YHfV7o
zZTXBZxgMEiCWxHYl36DdGbEclQDitjFH+ur8TaVIo7xewZ/cgOudqK/AoGBAOGn
h8lpqoP0U/49Bpzn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KLVaOui7nntU5O
xQijz+/kk4vy58qDedyHxB24wMt6S+7a5P++ZO20K4/mBIFmdyGIuvOYJY0Gs7X/
EQIDAQAB
-----END PUBLIC KEY-----
'''
private_key = RSA.importKey(private_key_pem)
print("private key components:")
print("modulus:", private_key.n)
print("decryption key:", private_key.d, "\n")
public_key = RSA.importKey(public_key_pem)
print("public key components:")
print("modulus:", public_key.n)
print("encryption key:", public_key.e)
The code produces the following output.
private key components:
modulus: 28018210757639036701806576653473108663108237654992710644382580670706356521104503590269747243444881816860960886628707561729506054845669087158286487779668877579111045682687132130967542003640620395709760439291791359332971311569833464916696923708100648936544179482515518284015359758063690936170429993787988992366530178656661760648006433502964162950734482834691341055821131012179885041912952708019133191444259147492689058801662179466923034701521378053315755439283332003332125730956606219922314282161592457216832544141605338942735432555226858379903566758713188147527196578404934586961422561713661928243117076396507324088081
decryption key: 26593295115250601019258987963328215378793426844243199611260418512300199902937036198317581786338181920686317820343502527882298467058608112937639084575835373314784373493536023423023711533858143508166836113427630036711295107571141810758475600688751281969708624754419569088284349884654375863917259370028961766347968136242492409216019897338881947171652949409664617568373420518516510028969311296632860124215508429507388883135792513113188476224349460853934378076812541476273510991540179616816870490909273466785702119283308474487346009112179993007498269623263500977729040200031842282261746661152366200714995020265888356975473
public key components:
modulus: 28018210757639036701806576653473108663108237654992710644382580670706356521104503590269747243444881816860960886628707561729506054845669087158286487779668877579111045682687132130967542003640620395709760439291791359332971311569833464916696923708100648936544179482515518284015359758063690936170429993787988992366530178656661760648006433502964162950734482834691341055821131012179885041912952708019133191444259147492689058801662179466923034701521378053315755439283332003332125730956606219922314282161592457216832544141605338942735432555226858379903566758713188147527196578404934586961422561713661928243117076396507324088081
encryption key: 65537