def METHOD_NAME(self, data, key, encode_base64):
    """
    encrypts data
    set encode_base64 to True to base64 encode data before writing
    """
    assert isinstance(key, str)
    _data = data
    self.encoding = "original"
    if encode_base64:
        _data = base64.b64encode(data.encode())
        self.encoding = "base64"
    else:
        # To guarantee _data is bytes
        if isinstance(data, str):
            _data = data.encode()
    try:
        ciphertext = ""
        # Mocking encrypted response for tests
        if key == "mock":
            ciphertext = base64.b64encode(_data)
        else:
            request = azkms_obj(key).encrypt(EncryptionAlgorithm.rsa_oaep_256, _data)
            ciphertext = request.ciphertext
        self.data = ciphertext
        self.key = key
    except Exception as e:
        raise AzureKMSError(e)