Skip to content

Commit

Permalink
Add some Python2 compat code I forgot to add earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
noDRM committed Jul 13, 2022
1 parent c12d214 commit fed8bb7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
21 changes: 11 additions & 10 deletions DeDRM_plugin/ineptpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@
# 9.1.0 - Support for decrypting with owner password, support for V=5, R=5 and R=6 PDF files, support for AES256-encrypted PDFs.
# 9.1.1 - Only support PyCryptodome; clean up the code
# 10.0.0 - Add support for "hardened" Adobe DRM (RMSDK >= 10)
# 10.0.2 - Fix some Python2 stuff

"""
Decrypts Adobe ADEPT-encrypted PDF files.
"""

__license__ = 'GPL v3'
__version__ = "10.0.0"
__version__ = "10.0.2"

import codecs
import hashlib
Expand All @@ -65,6 +66,8 @@
import re
import zlib
import struct
import binascii
import base64
from io import BytesIO
from decimal import Decimal
import itertools
Expand Down Expand Up @@ -1369,7 +1372,7 @@ def recover_encryption_key_with_password(self, password, docid, param):
return file_key


def process_with_aes(self, key: bytes, encrypt: bool, data: bytes, repetitions: int = 1, iv: bytes = None):
def process_with_aes(self, key, encrypt, data, repetitions = 1, iv = None):
if iv is None:
keylen = len(key)
iv = bytes([0x00]*keylen)
Expand Down Expand Up @@ -1601,19 +1604,17 @@ def initialize_standard(self, password, docid, param):

def initialize_ebx_ignoble(self, keyb64, docid, param):
self.is_printable = self.is_modifiable = self.is_extractable = True
try:
key = keyb64.decode('base64')[:16]
# This will probably always error, but I'm not 100% sure, so lets leave the old code in.
except AttributeError:
key = codecs.decode(keyb64.encode("ascii"), 'base64')[:16]
key = keyb64.decode('base64')[:16]

length = int_value(param.get('Length', 0)) / 8
rights = str_value(param.get('ADEPT_LICENSE')).decode('base64')
rights = codecs.decode(str_value(param.get('ADEPT_LICENSE')), "base64")
rights = zlib.decompress(rights, -15)
rights = etree.fromstring(rights)
expr = './/{http://ns.adobe.com/adept}encryptedKey'
bookkey = ''.join(rights.findtext(expr)).decode('base64')
bookkey = unpad(AES.new(key, AES.MODE_CBC, b'\x00'*16).decrypt(bookkey), 16) # PKCS#7
bookkey = ''.join(rights.findtext(expr))
bookkey = base64.b64decode(bookkey)
bookkey = AES.new(key, AES.MODE_CBC, b'\x00'*16).decrypt(bookkey)
bookkey = unpad(bookkey, 16) # PKCS#7
if len(bookkey) > 16:
bookkey = bookkey[-16:]
ebx_V = int_value(param.get('V', 4))
Expand Down
10 changes: 8 additions & 2 deletions DeDRM_plugin/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#@@CALIBRE_COMPAT_CODE@@

from ignoblekeyGenPassHash import generate_key
import sys

__license__ = 'GPL v3'

Expand All @@ -21,8 +22,13 @@

def uStrCmp (s1, s2, caseless=False):
import unicodedata as ud
str1 = s1 if isinstance(s1, str) else str(s1)
str2 = s2 if isinstance(s2, str) else str(s2)
if sys.version_info[0] == 2:
str1 = s1 if isinstance(s1, unicode) else unicode(s1)
str2 = s2 if isinstance(s2, unicode) else unicode(s2)
else:
str1 = s1 if isinstance(s1, str) else str(s1)
str2 = s2 if isinstance(s2, str) else str(s2)

if caseless:
return ud.normalize('NFC', str1.lower()) == ud.normalize('NFC', str2.lower())
else:
Expand Down

0 comments on commit fed8bb7

Please sign in to comment.