import base64 import rsa with open("pubkey.pem", 'rb') as f: pubKey = rsa.PublicKey.load_pkcs1(f.read()) def encrypt(data): for key, value in data.items(): value_bytes = value.encode("utf-8") encrypted_value = rsa.encrypt(value_bytes, pubKey) encoded_value = base64.b64encode(encrypted_value) data[key] = encoded_value