Skip to content

Commit

Permalink
Python 3 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ableeker committed Sep 19, 2021
1 parent 4bab58e commit bda647b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
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
8 changes: 7 additions & 1 deletion 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 @@ -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
Expand Down Expand Up @@ -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(" ")
Expand Down

0 comments on commit bda647b

Please sign in to comment.