Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for decryption check #1753

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions DeDRM_plugin/alfcrypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)
Expand Down
18 changes: 14 additions & 4 deletions DeDRM_plugin/flatxml2html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<a href="%s">' % linkhref
Expand All @@ -485,9 +489,15 @@ def buildParagraph(self, pclass, pdesc, type, regtype) :
else :
# just link to the current page
linkhtml = '<a href="#' + self.id + '">'
linkhtml += title.decode('utf-8')
if isinstance(title, bytes):
linkhtml += title.decode('utf-8')
else:
linkhtml += title
linkhtml += '</a>'
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 :
Expand Down
8 changes: 8 additions & 0 deletions DeDRM_plugin/topazextract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -345,6 +349,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('latin-1')
print("Trying: {0}".format(pid))
bookKeys = []
data = keydata
Expand Down Expand Up @@ -415,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(" ")
Expand Down
4 changes: 1 addition & 3 deletions Obok_plugin/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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()
Expand Down