From bda647bd5bbffb5c746310b3dfb8e7b4fe4a50b7 Mon Sep 17 00:00:00 2001 From: Aldo Bleeker <2095835+ableeker@users.noreply.github.com> Date: Sun, 19 Sep 2021 23:35:45 +0200 Subject: [PATCH] Python 3 fix --- DeDRM_plugin/alfcrypto.py | 10 ++++++---- DeDRM_plugin/topazextract.py | 8 +++++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/DeDRM_plugin/alfcrypto.py b/DeDRM_plugin/alfcrypto.py index 5c197a7..5a31d09 100644 --- a/DeDRM_plugin/alfcrypto.py +++ b/DeDRM_plugin/alfcrypto.py @@ -207,8 +207,9 @@ def __init__(self): def ctx_init(self, key): ctx1 = 0x0CAFFE19E - for keyChar in key: - keyByte = ord(keyChar) + if isinstance(key, str): + key = key.encode('latin-1') + for keyByte in key: ctx2 = ctx1 ctx1 = ((((ctx1 >>2) * (ctx1 >>7))&0xFFFFFFFF) ^ (keyByte * keyByte * 0x0F902007)& 0xFFFFFFFF ) self._ctx = [ctx1, ctx2] @@ -220,8 +221,9 @@ def decrypt(self, data, ctx=None): ctx1 = ctx[0] ctx2 = ctx[1] plainText = "" - for dataChar in data: - dataByte = ord(dataChar) + if isinstance(data, str): + data = data.encode('latin-1') + for dataByte in data: m = (dataByte ^ ((ctx1 >> 3) &0xFF) ^ ((ctx2<<3) & 0xFF)) &0xFF ctx2 = ctx1 ctx1 = (((ctx1 >> 2) * (ctx1 >> 7)) &0xFFFFFFFF) ^((m * m * 0x0F902007) &0xFFFFFFFF) diff --git a/DeDRM_plugin/topazextract.py b/DeDRM_plugin/topazextract.py index bdbf1eb..66f09fd 100644 --- a/DeDRM_plugin/topazextract.py +++ b/DeDRM_plugin/topazextract.py @@ -172,6 +172,8 @@ def decryptRecord(data,PID): # Try to decrypt a dkey record (contains the bookPID) def decryptDkeyRecord(data,PID): record = decryptRecord(data,PID) + if isinstance(record, str): + record = record.encode('latin-1') fields = unpack('3sB8sB8s3s',record) if fields[0] != b'PID' or fields[5] != b'pid' : raise DrmException("Didn't find PID magic numbers in record") @@ -315,6 +317,8 @@ def getBookPayloadRecord(self, name, index): raise DrmException("Error: Attempt to decrypt without bookKey") if compressed: + if isinstance(record, str): + record = bytes(record, 'latin-1') record = zlib.decompress(record) return record @@ -346,7 +350,7 @@ def processBook(self, pidlst): # use 8 digit pids here pid = pid[0:8] if isinstance(pid, str): - pid = pid.encode('utf-8') + pid = pid.encode('latin-1') print("Trying: {0}".format(pid)) bookKeys = [] data = keydata @@ -417,6 +421,8 @@ def extractFiles(self): outputFile = os.path.join(destdir,fname) print(".", end=' ') record = self.getBookPayloadRecord(name,index) + if isinstance(record, str): + record=bytes(record, 'latin-1') if record != b'': open(outputFile, 'wb').write(record) print(" ")