from django.conf import settings
from Crypto.Cipher import AES

import base64


def __pad(raw):
    if (len(raw) % 16 == 0):
        return raw
    padding_required = 16 - (len(raw) % 16)
    data = raw + '0' * padding_required
    return data


def encode(raw):
    aes = AES.new(settings.AES_ENCRIPTION_KEY, AES.MODE_ECB)
    encoded = base64.b64encode(aes.encrypt(__pad(raw))).decode("utf-8")
    encoded = encoded.replace( "+", "%2B" );
    return encoded


def decode(raw):
    aes = AES.new(settings.AES_ENCRIPTION_KEY, AES.MODE_ECB)
    missing_padding = len(raw) % 4
    if missing_padding != 0:
        raw += b'0' * (4 - missing_padding)
    raw = aes.decrypt(base64.b64decode(str(raw))).decode('utf-8')
    raw = raw.rstrip("0")
    return raw

@@ -177,16 +177,32 @@
 (raw
+.encode('utf-8')
 ) %25 16)%0A
     
@@ -197,16 +197,16 @@
 ) %25 16)%0A
-
     data
@@ -329,24 +329,60 @@
 S.MODE_ECB)%0A
+    print (aes.encrypt(__pad(raw)))%0A
     encoded 
@@ -390,16 +390,24 @@
  base64.
+urlsafe_
 b64encod
@@ -596,16 +596,18 @@
 ECB)%0A   
+ #
  missing
@@ -628,17 +628,37 @@
 (raw
+.encode('utf-8') 
 ) %25 
-4
+16
 %0A   
+ #
  if 
@@ -678,24 +678,26 @@
 ng != 0:%0A   
+ #
      raw += 
@@ -700,17 +700,17 @@
  += 
-b'0
+'=
 ' * (
-4
+16
  - m
@@ -723,16 +723,17 @@
 _padding
+ 
 )%0A    ra
@@ -740,46 +740,205 @@
 w = 
-aes.decrypt(base64.b64decode(str(raw))
+base64.urlsafe_b64decode(raw )%0A    print (raw)%0A    # if (len(raw) %25 16 != 0):%0A    #    padding_required = 16 - (len(raw) %25 16)%0A    #    raw = raw + b'0' * padding_required%0A    raw = aes.decrypt(raw
 ).de

Fix base64 encode decode