Skip to content

Instantly share code, notes, and snippets.

@ricardojba
Forked from orian/crypt.py
Created April 18, 2017 08:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ricardojba/5d317dcf0c1792700cbe2616ec5e8a64 to your computer and use it in GitHub Desktop.
Save ricardojba/5d317dcf0c1792700cbe2616ec5e8a64 to your computer and use it in GitHub Desktop.
Decrypt Laravel-encrypted value
# Python3 code below
import os
import base64
import json
from Crypto.Cipher import AES
from phpserialize import loads
import hashlib
import hmac
def decrypt(payload):
data = json.loads(base64.b64decode(payload))
key=os.environ['APP_KEY']
if not valid_mac(key, data):
return None
value = base64.b64decode(data['value'])
iv = base64.b64decode(data['iv'])
return unserialize(mcrypt_decrypt(value, iv, key))
def mcrypt_decrypt(value, iv, key):
AES.key_size=128
crypt_object=AES.new(key=key,mode=AES.MODE_CBC,IV=iv)
return crypt_object.decrypt(value)
def unserialize(serialized):
return loads(serialized)
def valid_mac(key, payload):
dig = hmac.new(key, digestmod=hashlib.sha256)
dig.update(data['iv'].encode('utf8'))
dig.update(data['value'].encode('utf8'))
dig = dig.hexdigest()
return dig==payload['mac']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment