import base64

from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import padding


def read_public_key(filename):
    with open(filename, "rb") as key_file:
        public_key = serialization.load_pem_public_key(key_file.read())
    return public_key


def encrypt_data(data, public_key):
    encrypted = public_key.encrypt(data.encode("utf-8"), padding.PKCS1v15())
    return base64.b64encode(encrypted).decode("utf-8")


def encrypt_string(string_to_encrypt, public_key_path):
    public_key = read_public_key(public_key_path)
    encrypted_data = encrypt_data(string_to_encrypt, public_key)

    return encrypted_data
