I have a function that encodes an encrypted string (using hazmat Cipher) in base64 representation, but when I try to decrypt it later using a different function I am getting an error:

```
web_1  |   File "/code/cart/models.py", line 58, in decrypt_hash_id
web_1  |     int_id = int(padded_id.decode('utf-8').lstrip(' '))
web_1  | UnicodeDecodeError: 'utf-8' codec can't decode byte 0x86 in position 0: invalid start byte

```

My encryption function is:

```
from django.conf import settings
from base64 import urlsafe_b64encode, urlsafe_b64decode
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend

def url_safe_encrypted_id(self):
    backend = default_backend()
    key = settings.URL_ENCRYPTION_KEY
    iv = settings.URL_ENCRYPTION_IV
    cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
    encryptor = cipher.encryptor()
    ct = encryptor.update(str(self.id).rjust(16).encode()) + encryptor.finalize()
    return urlsafe_b64encode(ct)

```

However when I try to decrypt this base64 string I am sometimes getting an error. My method for decrypting is:

```
def decrypt_hash_id(hash):
    # Decode order_id
    backend = default_backend()
    key = settings.URL_ENCRYPTION_KEY
    iv = settings.URL_ENCRYPTION_IV
    cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
    decryptor = cipher.decryptor()
    padded_id = decryptor.update(urlsafe_b64decode((hash + '===').encode())) + decryptor.finalize()
    int_id = int(padded_id.decode().lstrip(' '))

    return int_id

```

Can anyone help to see where the problem might lie? Probably 99% of the time it's working correctly, but occasionally I will get the error shown above.