Ideally, the public certificate should be publicly available and the private certificate should be kept private. This message will be encrypted with the public key certificate public_key.pem as per the RSA algorithm. Recall that a key is simply a string of text.
We generate our public and private RSA certificates to disk as public_key.pem and private_key.pem respectively, as usual.We do the usual imports to generate our RSA public/private certificate pair but this time we include an extra method PKCS1_OAEP which will be used to create the cipher object to encrypt our plaintext with the key that is in the certificate.Key = RSA.import_key(open('public_key.pem').read()) Public_key = new_key.publickey().exportKey('PEM') Then you write the following code: from Crypto.Cipher import PKCS1_OAEP Once you are up to speed, you can jump into the following code to encrypt data with an RSA Public Key, But first, ensure you install the pycryptodomex module: pip install pycryptodomex To understand what we’re doing here, please check out our two foundation tutorials on RSA Encryption: Hi! Let’s encrypt some data using an RSA Public Key Certificate in Python.