From 4d8b5cfa333086b332e18a9220f0cacd74fbad95 Mon Sep 17 00:00:00 2001 From: Aldo Bleeker Date: Mon, 28 Jun 2021 16:59:15 +0200 Subject: [PATCH 1/5] Fix for decryption check --- Obok_plugin/action.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Obok_plugin/action.py b/Obok_plugin/action.py index 1a4515b4..fb8f26ef 100644 --- a/Obok_plugin/action.py +++ b/Obok_plugin/action.py @@ -375,7 +375,6 @@ def decryptBook(self, book): #print ('Kobo library filename: {0}'.format(book.filename)) for userkey in self.userkeys: print (_('Trying key: '), codecs.encode(userkey, 'hex')) - check = True try: fileout = PersistentTemporaryFile('.epub', dir=self.tdir) #print ('Temp file: {0}'.format(fileout.name)) @@ -396,8 +395,7 @@ def decryptBook(self, book): file = book.encryptedfiles[filename] contents = file.decrypt(userkey, contents) # Parse failures mean the key is probably wrong. - if check: - check = not file.check(contents) + file.check(contents) zout.writestr(filename, contents) zout.close() zin.close() From 4bab58e0fa9e5b3e3da47d5fa819cdc9e4d2d0c3 Mon Sep 17 00:00:00 2001 From: Aldo Bleeker <2095835+ableeker@users.noreply.github.com> Date: Sun, 12 Sep 2021 17:14:51 +0200 Subject: [PATCH 2/5] Fix for Python 3 --- DeDRM_plugin/flatxml2html.py | 18 ++++++++++++++---- DeDRM_plugin/topazextract.py | 2 ++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/DeDRM_plugin/flatxml2html.py b/DeDRM_plugin/flatxml2html.py index 2fe80c36..9e80927c 100644 --- a/DeDRM_plugin/flatxml2html.py +++ b/DeDRM_plugin/flatxml2html.py @@ -473,8 +473,12 @@ def buildParagraph(self, pclass, pdesc, type, regtype) : if (link > 0): linktype = self.link_type[link-1] title = self.link_title[link-1] - if (title == b"") or (parares.rfind(title.decode('utf-8')) < 0): - title=parares[lstart:].encode('utf-8') + if isinstance(title, bytes): + if (title == b"") or (parares.rfind(title.decode('utf-8')) < 0): + title=parares[lstart:].encode('utf-8') + else: + if (title == "") or (parares.rfind(title) < 0): + title=parares[lstart:] if linktype == 'external' : linkhref = self.link_href[link-1] linkhtml = '' % linkhref @@ -485,9 +489,15 @@ def buildParagraph(self, pclass, pdesc, type, regtype) : else : # just link to the current page linkhtml = '' - linkhtml += title.decode('utf-8') + if isinstance(title, bytes): + linkhtml += title.decode('utf-8') + else: + linkhtml += title linkhtml += '' - pos = parares.rfind(title.decode('utf-8')) + if isinstance(title, bytes): + pos = parares.rfind(title.decode('utf-8')) + else: + pos = parares.rfind(title) if pos >= 0: parares = parares[0:pos] + linkhtml + parares[pos+len(title):] else : diff --git a/DeDRM_plugin/topazextract.py b/DeDRM_plugin/topazextract.py index 5125d623..bdbf1eb3 100644 --- a/DeDRM_plugin/topazextract.py +++ b/DeDRM_plugin/topazextract.py @@ -345,6 +345,8 @@ def processBook(self, pidlst): for pid in pidlst: # use 8 digit pids here pid = pid[0:8] + if isinstance(pid, str): + pid = pid.encode('utf-8') print("Trying: {0}".format(pid)) bookKeys = [] data = keydata 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 3/5] 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 5c197a72..5a31d095 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 bdbf1eb3..66f09fd2 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(" ") From ceeb2e3c6144c45780a9b557b7a5ea3df41f8658 Mon Sep 17 00:00:00 2001 From: Aldo Bleeker <2095835+ableeker@users.noreply.github.com> Date: Sun, 19 Sep 2021 23:45:22 +0200 Subject: [PATCH 4/5] Python 3 fix --- DeDRM_plugin/topazextract.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DeDRM_plugin/topazextract.py b/DeDRM_plugin/topazextract.py index 66f09fd2..d7e608ed 100644 --- a/DeDRM_plugin/topazextract.py +++ b/DeDRM_plugin/topazextract.py @@ -350,7 +350,7 @@ def processBook(self, pidlst): # use 8 digit pids here pid = pid[0:8] if isinstance(pid, str): - pid = pid.encode('latin-1') + pid = pid.encode('utf-8') print("Trying: {0}".format(pid)) bookKeys = [] data = keydata From 64e5478ad10fad93bf7bdc032f36ecbb8ee95250 Mon Sep 17 00:00:00 2001 From: Aldo Bleeker <2095835+ableeker@users.noreply.github.com> Date: Mon, 20 Sep 2021 00:13:09 +0200 Subject: [PATCH 5/5] Fix --- DeDRM_plugin/topazextract.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DeDRM_plugin/topazextract.py b/DeDRM_plugin/topazextract.py index d7e608ed..66f09fd2 100644 --- a/DeDRM_plugin/topazextract.py +++ b/DeDRM_plugin/topazextract.py @@ -350,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