mirror of
https://github.com/noDRM/DeDRM_tools.git
synced 2025-01-01 22:37:39 +06:00
Merge pull request #1383 from ableeker/python3
Python 3 fixes for Barnes&Noble
This commit is contained in:
commit
bdd1c2e474
@ -35,6 +35,7 @@ __version__ = "5.0"
|
|||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import traceback
|
import traceback
|
||||||
|
import base64
|
||||||
import zlib
|
import zlib
|
||||||
import zipfile
|
import zipfile
|
||||||
from zipfile import ZipInfo, ZipFile, ZIP_STORED, ZIP_DEFLATED
|
from zipfile import ZipInfo, ZipFile, ZIP_STORED, ZIP_DEFLATED
|
||||||
@ -151,7 +152,7 @@ def _load_crypto_libcrypto():
|
|||||||
|
|
||||||
def decrypt(self, data):
|
def decrypt(self, data):
|
||||||
out = create_string_buffer(len(data))
|
out = create_string_buffer(len(data))
|
||||||
iv = ("\x00" * self._blocksize)
|
iv = (b'\x00' * self._blocksize)
|
||||||
rv = AES_cbc_encrypt(data, out, len(data), self._key, iv, 0)
|
rv = AES_cbc_encrypt(data, out, len(data), self._key, iv, 0)
|
||||||
if rv == 0:
|
if rv == 0:
|
||||||
raise IGNOBLEError('AES decryption failed')
|
raise IGNOBLEError('AES decryption failed')
|
||||||
@ -164,7 +165,7 @@ def _load_crypto_pycrypto():
|
|||||||
|
|
||||||
class AES(object):
|
class AES(object):
|
||||||
def __init__(self, key):
|
def __init__(self, key):
|
||||||
self._aes = _AES.new(key, _AES.MODE_CBC, '\x00'*16)
|
self._aes = _AES.new(key, _AES.MODE_CBC, b'\x00'*16)
|
||||||
|
|
||||||
def decrypt(self, data):
|
def decrypt(self, data):
|
||||||
return self._aes.decrypt(data)
|
return self._aes.decrypt(data)
|
||||||
@ -207,15 +208,15 @@ class Decryptor(object):
|
|||||||
def decompress(self, bytes):
|
def decompress(self, bytes):
|
||||||
dc = zlib.decompressobj(-15)
|
dc = zlib.decompressobj(-15)
|
||||||
bytes = dc.decompress(bytes)
|
bytes = dc.decompress(bytes)
|
||||||
ex = dc.decompress('Z') + dc.flush()
|
ex = dc.decompress(b'Z') + dc.flush()
|
||||||
if ex:
|
if ex:
|
||||||
bytes = bytes + ex
|
bytes = bytes + ex
|
||||||
return bytes
|
return bytes
|
||||||
|
|
||||||
def decrypt(self, path, data):
|
def decrypt(self, path, data):
|
||||||
if path in self._encrypted:
|
if bytes(path,'utf-8') in self._encrypted:
|
||||||
data = self._aes.decrypt(data)[16:]
|
data = self._aes.decrypt(data)[16:]
|
||||||
data = data[:-ord(data[-1])]
|
data = data[:-data[-1]]
|
||||||
data = self.decompress(data)
|
data = self.decompress(data)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
@ -241,7 +242,7 @@ def ignobleBook(inpath):
|
|||||||
def decryptBook(keyb64, inpath, outpath):
|
def decryptBook(keyb64, inpath, outpath):
|
||||||
if AES is None:
|
if AES is None:
|
||||||
raise IGNOBLEError("PyCrypto or OpenSSL must be installed.")
|
raise IGNOBLEError("PyCrypto or OpenSSL must be installed.")
|
||||||
key = keyb64.decode('base64')[:16]
|
key = base64.b64decode(keyb64)[:16]
|
||||||
aes = AES(key)
|
aes = AES(key)
|
||||||
with closing(ZipFile(open(inpath, 'rb'))) as inf:
|
with closing(ZipFile(open(inpath, 'rb'))) as inf:
|
||||||
namelist = set(inf.namelist())
|
namelist = set(inf.namelist())
|
||||||
@ -259,8 +260,8 @@ def decryptBook(keyb64, inpath, outpath):
|
|||||||
if len(bookkey) != 64:
|
if len(bookkey) != 64:
|
||||||
print("{0:s} is not a secure Barnes & Noble ePub.".format(os.path.basename(inpath)))
|
print("{0:s} is not a secure Barnes & Noble ePub.".format(os.path.basename(inpath)))
|
||||||
return 1
|
return 1
|
||||||
bookkey = aes.decrypt(bookkey.decode('base64'))
|
bookkey = aes.decrypt(base64.b64decode(bookkey))
|
||||||
bookkey = bookkey[:-ord(bookkey[-1])]
|
bookkey = bookkey[:-bookkey[-1]]
|
||||||
encryption = inf.read('META-INF/encryption.xml')
|
encryption = inf.read('META-INF/encryption.xml')
|
||||||
decryptor = Decryptor(bookkey[-16:], encryption)
|
decryptor = Decryptor(bookkey[-16:], encryption)
|
||||||
kwds = dict(compression=ZIP_DEFLATED, allowZip64=False)
|
kwds = dict(compression=ZIP_DEFLATED, allowZip64=False)
|
||||||
|
Loading…
Reference in New Issue
Block a user