Search code examples
rsapythonkeypublic-key

Is my RSA key correctly generated


I use python pycryptodome rto generate my private and public key. This is one example of a public key that I am generating:

(n=0xf56efd1888065abb3ef6e6b1e6e0039ff1fa0b09e1f1ab9869ad933e1f48527c182957f9a767110524c362ac4c460f752d2011ef827ed9680fe58a078f3e8355d9bea6e1285a7a51b3f5022c66f72331a1053fb8a0033b28838026fadfa11da8b0bcb98476ef1c80f53f9814762c1191965733f883ee1d686624123b28140b5b91fcadf33bfe02e196e0f10bdd80cd5babbd6c10179a84d9325424d2b365f7a8274ce6c454b582dba3289c3cea83ae117c5567dba33a1434b9276dc4ab88fbeec68483913fe6c70424aa19165f19d6d7f158eafedbd81046a7bcdd46be0b4ef8f86069a28a98c78e38ff0f413b1cf76aac674f1c04c7b5ff23540f12398c3927, e=0x10001)

I generate it using this piece of code:

keyPair = RSA.generate(bits=2048)
publicKey = f"(n={hex(keyPair.n)}, e={hex(keyPair.e)})"

Somehow, this doesn't look correct to me, because usually, when I create a key using mac os or other method, I have something like

---BEGIN PRIVATE KEY aksdjbvioasv.....(key itself here....) ---END PUBLIC KEY

What am I msising so that my RSA key is generated correctly?


Solution

  • I solved it by doing

    pubKey = keyPair.publickey()
    pubKeyPEM = pubKey.exportKey()
    print("pubKey PEM is:")
    print(pubKeyPEM)
    

    Now I get the proper format, that I can now write into a file!