Generate RSA public key and private key with python

Install dependency

pip install pycryptodome

Code example

from Crypto.PublicKey import RSA  # provided by pycryptodome

key = RSA.generate(2048)
private_key = key.export_key()
public_key = key.publickey().export_key()

print('private key:')
print(private_key.decode())
print('public key:')
print(public_key.decode())

Output example:

private key:
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEApjlvc9QrmaNWye9a95qZh0FiI7zz2d89QIw145XcU0/eJFIt
29Zvza57LIydsFLMvWWPmHrje1631QC3YJA5xl3UMBIOJQJHwvMIdQHEl2LToo6d
2sTfh+jNkgnsb9pQyVigje3L3mKHVTLacJYkUkCvBqK9n9HryIMMNj8/KWRNCbrI
sWOonicR0JzrdDafwbD2/H1qmsrzz/gOZFT+IjjItf6KFHdYuuWlgElwrM7AFhHz
vzQp8nw1L1ZVSpa5CGjMVCPJ+ri/HuF0U/JXWHkKY1+uZ1SyHoJ1XdnaxnZYNE+B
fWrnBRdRNCDkj6kpy/ofcf/fHWHROGmjt2P3SQIDAQABAoIBAB7L2VhOYN2QI446
KFJjdDpY5MPWSB9/equwznMh2rYcv/1etFxceE5VG0D/tF9sl74nSNlpOygqPkja
Xx8m2W1lCOD3F2PH7l+yA0kaUL/zcV5WqexUOU3G1f9Ok12NxxXms6hX/ENxYb1p
NItTZdtPXJ4QHK21iMeXb/XE9gDG5XJrnDOxpe8aETns8J4Z4DItH8SDYxEL2OHM
UWdwBIXAxhWV17sNl8MCevzeH37DhiFkEI4777GT/8UpQ6unbY/9pIFI3UdoK6y/
VeKt8+ST6sK3SheUEtzsOWeyUf5ZCMPxbbnVRBG2xoPxJZpJ90CALPzDgoSx9CKR
kdSPCOECgYEAtmL65wuSqtu+0i/EAECFocqu8OlC9YJACRm+yq9P7TNlYKTMp27h
nsIVhGXm6dvqjCr/zxRcTFPzXfI1HdOtQ6Qk9Lq/9q3DRQYTRi2K4v2HqL+i9sFu
b7Tjysn9kGJEApzQTkXcdz2XjmIVbcVPOvsnS3gXWBS2XC2RQsTd830CgYEA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