From 33b9630ca5c116be35a0ae3aec7e29fba22cb46c Mon Sep 17 00:00:00 2001 From: Aldo Bleeker Date: Thu, 28 Jan 2021 13:06:59 +0100 Subject: [PATCH] Fix for broken book keys --- DeDRM_plugin/ineptepub.py | 8 ++++---- DeDRM_plugin/ineptpdf.py | 9 ++++----- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/DeDRM_plugin/ineptepub.py b/DeDRM_plugin/ineptepub.py index fb67cb4..8bab717 100644 --- a/DeDRM_plugin/ineptepub.py +++ b/DeDRM_plugin/ineptepub.py @@ -415,12 +415,12 @@ def decryptBook(userkey, inpath, outpath): return 1 bookkey = rsa.decrypt(codecs.decode(bookkey.encode('ascii'), 'base64')) # Padded as per RSAES-PKCS1-v1_5 - if len(bookkey) != 16: - if bookkey[-17] != '\x00' and bookkey[-17] != 0: + if len(bookkey) > 16: + if bookkey[-17] == '\x00' or bookkey[-17] == 0: + bookkey = bookkey[-16:] + else: print("Could not decrypt {0:s}. Wrong key".format(os.path.basename(inpath))) return 2 - else: - bookkey = bookkey[-16:] encryption = inf.read('META-INF/encryption.xml') decryptor = Decryptor(bookkey, encryption) kwds = dict(compression=ZIP_DEFLATED, allowZip64=False) diff --git a/DeDRM_plugin/ineptpdf.py b/DeDRM_plugin/ineptpdf.py index 17ccdcf..669856c 100755 --- a/DeDRM_plugin/ineptpdf.py +++ b/DeDRM_plugin/ineptpdf.py @@ -1599,11 +1599,10 @@ class PDFDocument(object): bookkey = rsa.decrypt(bookkey) #if bookkey[0] != 2: # raise ADEPTError('error decrypting book session key') - try: - index = bookkey.index(b'\0') + 1 - bookkey = bookkey[index:] - except ValueError: - pass + if len(bookkey) > 16: + if bookkey[-17] == '\x00' or bookkey[-17] == 0: + bookkey = bookkey[-16:] + length = 16 ebx_V = int_value(param.get('V', 4)) ebx_type = int_value(param.get('EBX_ENCRYPTIONTYPE', 6)) # added because of improper booktype / decryption book session key errors