-
-
Couldn't load subscription status.
- Fork 38
Open
Labels
documentationImprovements or additions to documentationImprovements or additions to documentationskip-staleSkip issue or pull request to be marked as staleSkip issue or pull request to be marked as stalestale
Description
I have this java script which encrypts a strigified JSON object.
Amazon apps using this string as the "frc" cookie. My app creates the frc cookie from custom bytes. Amazon still accepts this frc cookie. But I will be prepared for the future and build the frc cookie the same way as Amazon does.
I've tried to rebuild the java script to python
import base64
import hmac
import hashlib
import json
import gzip
from Crypto.Cipher import AES
from Crypto.Util.py3compat import bchr
def check_sig(pw, iv, data, sig):
key = get_key_from_password(pw, b'HmacSHA256')
new_sig = hmac.new(key, iv + data, hashlib.sha256).digest()
new_sig = new_sig[:len(sig)]
assert sig == new_sig, 'Signature check failed'
def decrypt_frc(pw, iv, data):
key = get_key_from_password(pw, b"AES/CBC/PKCS7Padding")
crypter = AES.new(key, AES.MODE_CBC, iv)
decrypted_data = crypter.decrypt(data)
return decrypted_data
def split_frc(frc):
frc += (len(frc) % 4) * '='
frc = base64.b64decode(frc)
sig = frc[1:9]
iv = frc[9:25]
data = frc[25:]
return sig, iv, data
def get_key_from_password(pw, salt):
return hashlib.pbkdf2_hmac('sha1', pw, salt, 1000, 16)
def pkcs7unpad(msg, blocklen):
assert len(msg) % blocklen == 0
paddinglen = msg[-1]
assert paddinglen > 0 and paddinglen <= blocklen, "Incorrect padding - Wrong key"
assert msg[-paddinglen:] == bchr(paddinglen) * paddinglen, "Incorrect padding - Wrong key"
return msg[:-paddinglen]
if __name__ == '__main__':
frc_cookie = ''
device_serial = ''
device_serial = device_serial.encode()
sig, iv, data = split_frc(frc_cookie)
check_sig(device_serial, iv, data, sig)
decrypted = decrypt_frc(device_serial, iv, data)
decrypted = pkcs7unpad(decrypted, 16)
decompressed = gzip.decompress(decrypted)
decrypted_data = json.loads(decompressed)Can someone with JAVA and Python knowledge take a look on my code if I have take a misstake?
Metadata
Metadata
Assignees
Labels
documentationImprovements or additions to documentationImprovements or additions to documentationskip-staleSkip issue or pull request to be marked as staleSkip issue or pull request to be marked as stalestale