<NME> cryptutils.py
<BEF> """Crypto related utilities."""

import base64
import time

from cryptography.fernet import Fernet
import random
import string

from django.utils.encoding import force_bytes

from Crypto.Cipher import AES

from django.utils.encoding import smart_bytes, smart_text


def random_key(length=16):
    """Generate a new key used to encrypt user passwords in session storage."""
    chars = "abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)"
    return get_random_string(length, chars)


def random_hex_key(length=16):
    """Generate a new hexadecimal key."""
    chars = "abcdefABCDEF0123456789"
    return get_random_string(length, chars)


def _get_fernet():
    """Create a Fernet instance."""
    secret_key = base64.urlsafe_b64encode(smart_bytes(settings.SECRET_KEY[:32]))
    return Fernet(secret_key)


def encrypt(clear_value):
    """Encrypt a value using secret_key."""
    return smart_text(_get_fernet().encrypt(smart_bytes(clear_value)))

        )
    ciph = obj.encrypt(clear)
    ciph = base64.b64encode(ciph)
    return ciph


def decrypt(ciph):
    """
    Retrieve and decrypt the users password from session storage.

    This is used by modoboa-webmail to allow the user to send/receive e-mails
    without having to ask the user for a password on each connection to the
    postfix/dovecot server.
    """
    try:
        return decrypt(request.session["password"])
    except KeyError:
        return None

<MSG> final fix ?

<DFF> @@ -7,7 +7,7 @@ import base64
 import random
 import string
 
-from django.utils.encoding import force_bytes
+from django.utils.encoding import force_bytes, force_text
 
 from Crypto.Cipher import AES
 
@@ -39,7 +39,7 @@ def encrypt(clear):
         )
     ciph = obj.encrypt(clear)
     ciph = base64.b64encode(ciph)
-    return ciph
+    return force_text(ciph)
 
 
 def decrypt(ciph):

final fix ?