from Crypto.PublicKey import RSA code = 'noonekowns' key = RSA.generate(2048) encrypted_key =key.exportKey(passphrase=code, pkcs=8,protection="scryptAndAES128-CBC") print(encrypted_key) exit() with open('/path_to_public_key/my_rsa_public.pem', 'wb') as f: f.write(key.publickey().exportKey())