2020-09-27 12:54:49 +02:00
|
|
|
#!/usr/bin/env python3
|
2010-07-24 13:55:24 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2009-02-19 13:39:43 +01:00
|
|
|
|
2020-09-27 12:54:49 +02:00
|
|
|
# ineptepub.py
|
2022-02-23 00:16:03 +01:00
|
|
|
# Copyright © 2009-2022 by i♥cabbages, Apprentice Harper et al.
|
2009-02-19 13:39:43 +01:00
|
|
|
|
2012-12-19 14:48:11 +01:00
|
|
|
# Released under the terms of the GNU General Public Licence, version 3
|
|
|
|
# <http://www.gnu.org/licenses/>
|
|
|
|
|
2009-02-19 13:39:43 +01:00
|
|
|
|
|
|
|
# Revision history:
|
|
|
|
# 1 - Initial release
|
2013-10-02 20:59:40 +02:00
|
|
|
# 2 - Rename to INEPT, fix exit code
|
|
|
|
# 5 - Version bump to avoid (?) confusion;
|
|
|
|
# Improve OS X support by using OpenSSL when available
|
|
|
|
# 5.1 - Improve OpenSSL error checking
|
|
|
|
# 5.2 - Fix ctypes error causing segfaults on some systems
|
|
|
|
# 5.3 - add support for OpenSSL on Windows, fix bug with some versions of libcrypto 0.9.8 prior to path level o
|
|
|
|
# 5.4 - add support for encoding to 'utf-8' when building up list of files to decrypt from encryption.xml
|
|
|
|
# 5.5 - On Windows try PyCrypto first, OpenSSL next
|
|
|
|
# 5.6 - Modify interface to allow use with import
|
|
|
|
# 5.7 - Fix for potential problem with PyCrypto
|
|
|
|
# 5.8 - Revised to allow use in calibre plugins to eliminate need for duplicate code
|
|
|
|
# 5.9 - Fixed to retain zip file metadata (e.g. file modification date)
|
|
|
|
# 6.0 - moved unicode_argv call inside main for Windows DeDRM compatibility
|
|
|
|
# 6.1 - Work if TkInter is missing
|
2015-03-09 08:38:31 +01:00
|
|
|
# 6.2 - Handle UTF-8 file names inside an ePub, fix by Jose Luis
|
2016-01-11 07:44:44 +01:00
|
|
|
# 6.3 - Add additional check on DER file sanity
|
2016-01-14 18:15:43 +01:00
|
|
|
# 6.4 - Remove erroneous check on DER file sanity
|
2016-01-15 07:30:54 +01:00
|
|
|
# 6.5 - Completely remove erroneous check on DER file sanity
|
2017-06-27 07:50:24 +02:00
|
|
|
# 6.6 - Import tkFileDialog, don't assume something else will import it.
|
2020-09-26 22:22:47 +02:00
|
|
|
# 7.0 - Add Python 3 compatibility for calibre 5.0
|
2021-12-23 11:29:58 +01:00
|
|
|
# 7.1 - Add ignoble support, dropping the dedicated ignobleepub.py script
|
2022-02-23 00:16:03 +01:00
|
|
|
# 7.2 - Only support PyCryptodome; clean up the code
|
2022-02-23 00:47:51 +01:00
|
|
|
# 8.0 - Add support for "hardened" Adobe DRM (RMSDK >= 10)
|
2012-11-07 14:14:25 +01:00
|
|
|
|
2009-02-19 13:39:43 +01:00
|
|
|
"""
|
2013-10-02 20:59:40 +02:00
|
|
|
Decrypt Adobe Digital Editions encrypted ePub books.
|
2009-02-19 13:39:43 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
__license__ = 'GPL v3'
|
2022-02-23 00:47:51 +01:00
|
|
|
__version__ = "8.0"
|
2009-02-19 13:39:43 +01:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
2013-10-02 20:59:40 +02:00
|
|
|
import traceback
|
2021-12-23 11:29:58 +01:00
|
|
|
import base64
|
2009-02-19 13:39:43 +01:00
|
|
|
import zlib
|
2013-10-02 20:59:40 +02:00
|
|
|
import zipfile
|
|
|
|
from zipfile import ZipInfo, ZipFile, ZIP_STORED, ZIP_DEFLATED
|
2022-08-06 13:53:03 +02:00
|
|
|
from zeroedzipinfo import ZeroedZipInfo
|
2013-10-02 20:59:40 +02:00
|
|
|
from contextlib import closing
|
2021-11-15 18:38:34 +01:00
|
|
|
from lxml import etree
|
2022-02-23 00:47:51 +01:00
|
|
|
from uuid import UUID
|
|
|
|
import hashlib
|
2012-12-19 14:48:11 +01:00
|
|
|
|
2022-02-23 00:16:03 +01:00
|
|
|
try:
|
|
|
|
from Cryptodome.Cipher import AES, PKCS1_v1_5
|
|
|
|
from Cryptodome.PublicKey import RSA
|
|
|
|
except ImportError:
|
|
|
|
from Crypto.Cipher import AES, PKCS1_v1_5
|
|
|
|
from Crypto.PublicKey import RSA
|
2022-03-19 10:14:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
def unpad(data, padding=16):
|
|
|
|
if sys.version_info[0] == 2:
|
|
|
|
pad_len = ord(data[-1])
|
|
|
|
else:
|
|
|
|
pad_len = data[-1]
|
|
|
|
|
|
|
|
return data[:-pad_len]
|
2022-02-23 00:16:03 +01:00
|
|
|
|
2023-08-06 15:49:52 +02:00
|
|
|
#@@CALIBRE_COMPAT_CODE@@
|
|
|
|
|
2023-08-03 20:45:06 +02:00
|
|
|
from .utilities import SafeUnbuffered
|
|
|
|
from .argv_utils import unicode_argv
|
2012-12-19 14:48:11 +01:00
|
|
|
|
2009-02-19 13:39:43 +01:00
|
|
|
|
2010-07-24 13:55:24 +02:00
|
|
|
class ADEPTError(Exception):
|
|
|
|
pass
|
2009-02-19 13:39:43 +01:00
|
|
|
|
2021-11-15 19:51:36 +01:00
|
|
|
class ADEPTNewVersionError(Exception):
|
|
|
|
pass
|
|
|
|
|
2021-11-15 18:38:34 +01:00
|
|
|
META_NAMES = ('mimetype', 'META-INF/rights.xml')
|
2013-10-02 20:59:40 +02:00
|
|
|
NSMAP = {'adept': 'http://ns.adobe.com/adept',
|
|
|
|
'enc': 'http://www.w3.org/2001/04/xmlenc#'}
|
|
|
|
|
|
|
|
class Decryptor(object):
|
|
|
|
def __init__(self, bookkey, encryption):
|
|
|
|
enc = lambda tag: '{%s}%s' % (NSMAP['enc'], tag)
|
2022-02-23 00:16:03 +01:00
|
|
|
self._aes = AES.new(bookkey, AES.MODE_CBC, b'\x00'*16)
|
2023-01-06 14:29:56 +01:00
|
|
|
self._encryption = etree.fromstring(encryption)
|
2013-10-02 20:59:40 +02:00
|
|
|
self._encrypted = encrypted = set()
|
2023-01-06 14:32:25 +01:00
|
|
|
self._encryptedForceNoDecomp = encryptedForceNoDecomp = set()
|
2021-11-15 18:38:34 +01:00
|
|
|
self._otherData = otherData = set()
|
|
|
|
|
|
|
|
self._json_elements_to_remove = json_elements_to_remove = set()
|
|
|
|
self._has_remaining_xml = False
|
2013-10-02 20:59:40 +02:00
|
|
|
expr = './%s/%s/%s' % (enc('EncryptedData'), enc('CipherData'),
|
|
|
|
enc('CipherReference'))
|
2023-01-06 14:29:56 +01:00
|
|
|
for elem in self._encryption.findall(expr):
|
2013-10-02 20:59:40 +02:00
|
|
|
path = elem.get('URI', None)
|
2021-11-15 18:38:34 +01:00
|
|
|
encryption_type_url = (elem.getparent().getparent().find("./%s" % (enc('EncryptionMethod'))).get('Algorithm', None))
|
2013-10-02 20:59:40 +02:00
|
|
|
if path is not None:
|
2021-11-15 18:38:34 +01:00
|
|
|
if (encryption_type_url == "http://www.w3.org/2001/04/xmlenc#aes128-cbc"):
|
|
|
|
# Adobe
|
|
|
|
path = path.encode('utf-8')
|
|
|
|
encrypted.add(path)
|
|
|
|
json_elements_to_remove.add(elem.getparent().getparent())
|
2023-01-06 14:32:25 +01:00
|
|
|
elif (encryption_type_url == "http://ns.adobe.com/adept/xmlenc#aes128-cbc-uncompressed"):
|
|
|
|
# Adobe uncompressed, for stuff like video files
|
|
|
|
path = path.encode('utf-8')
|
|
|
|
encryptedForceNoDecomp.add(path)
|
|
|
|
json_elements_to_remove.add(elem.getparent().getparent())
|
2022-02-23 00:16:03 +01:00
|
|
|
else:
|
2021-11-15 18:38:34 +01:00
|
|
|
path = path.encode('utf-8')
|
|
|
|
otherData.add(path)
|
|
|
|
self._has_remaining_xml = True
|
2022-02-23 00:16:03 +01:00
|
|
|
|
2021-11-15 18:38:34 +01:00
|
|
|
for elem in json_elements_to_remove:
|
|
|
|
elem.getparent().remove(elem)
|
|
|
|
|
|
|
|
def check_if_remaining(self):
|
|
|
|
return self._has_remaining_xml
|
|
|
|
|
|
|
|
def get_xml(self):
|
|
|
|
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + etree.tostring(self._encryption, encoding="utf-8", pretty_print=True, xml_declaration=False).decode("utf-8")
|
|
|
|
|
2013-10-02 20:59:40 +02:00
|
|
|
|
|
|
|
def decompress(self, bytes):
|
|
|
|
dc = zlib.decompressobj(-15)
|
2020-12-27 13:16:11 +01:00
|
|
|
try:
|
|
|
|
decompressed_bytes = dc.decompress(bytes)
|
|
|
|
ex = dc.decompress(b'Z') + dc.flush()
|
|
|
|
if ex:
|
|
|
|
decompressed_bytes = decompressed_bytes + ex
|
|
|
|
except:
|
|
|
|
# possibly not compressed by zip - just return bytes
|
|
|
|
return bytes
|
2022-02-23 00:16:03 +01:00
|
|
|
return decompressed_bytes
|
|
|
|
|
2013-10-02 20:59:40 +02:00
|
|
|
def decrypt(self, path, data):
|
2023-01-06 14:32:25 +01:00
|
|
|
if path.encode('utf-8') in self._encrypted or path.encode('utf-8') in self._encryptedForceNoDecomp:
|
2013-10-02 20:59:40 +02:00
|
|
|
data = self._aes.decrypt(data)[16:]
|
2020-05-08 17:57:28 +02:00
|
|
|
if type(data[-1]) != int:
|
|
|
|
place = ord(data[-1])
|
|
|
|
else:
|
|
|
|
place = data[-1]
|
|
|
|
data = data[:-place]
|
2023-01-06 14:32:25 +01:00
|
|
|
if not path.encode('utf-8') in self._encryptedForceNoDecomp:
|
|
|
|
data = self.decompress(data)
|
2013-04-05 18:44:48 +02:00
|
|
|
return data
|
2012-12-19 14:48:11 +01:00
|
|
|
|
2013-10-02 20:59:40 +02:00
|
|
|
# check file to make check whether it's probably an Adobe Adept encrypted ePub
|
|
|
|
def adeptBook(inpath):
|
|
|
|
with closing(ZipFile(open(inpath, 'rb'))) as inf:
|
|
|
|
namelist = set(inf.namelist())
|
|
|
|
if 'META-INF/rights.xml' not in namelist or \
|
|
|
|
'META-INF/encryption.xml' not in namelist:
|
|
|
|
return False
|
2012-12-19 14:48:11 +01:00
|
|
|
try:
|
2013-10-02 20:59:40 +02:00
|
|
|
rights = etree.fromstring(inf.read('META-INF/rights.xml'))
|
|
|
|
adept = lambda tag: '{%s}%s' % (NSMAP['adept'], tag)
|
|
|
|
expr = './/%s' % (adept('encryptedKey'),)
|
|
|
|
bookkey = ''.join(rights.findtext(expr))
|
2021-12-23 11:29:58 +01:00
|
|
|
if len(bookkey) in [192, 172, 64]:
|
2013-10-02 20:59:40 +02:00
|
|
|
return True
|
|
|
|
except:
|
|
|
|
# if we couldn't check, assume it is
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2021-12-23 11:29:58 +01:00
|
|
|
def isPassHashBook(inpath):
|
|
|
|
# If this is an Adobe book, check if it's a PassHash-encrypted book (B&N)
|
|
|
|
with closing(ZipFile(open(inpath, 'rb'))) as inf:
|
|
|
|
namelist = set(inf.namelist())
|
|
|
|
if 'META-INF/rights.xml' not in namelist or \
|
|
|
|
'META-INF/encryption.xml' not in namelist:
|
|
|
|
return False
|
|
|
|
try:
|
|
|
|
rights = etree.fromstring(inf.read('META-INF/rights.xml'))
|
|
|
|
adept = lambda tag: '{%s}%s' % (NSMAP['adept'], tag)
|
|
|
|
expr = './/%s' % (adept('encryptedKey'),)
|
|
|
|
bookkey = ''.join(rights.findtext(expr))
|
|
|
|
if len(bookkey) == 64:
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
pass
|
2022-02-23 00:16:03 +01:00
|
|
|
|
2021-12-23 11:29:58 +01:00
|
|
|
return False
|
|
|
|
|
2022-02-23 00:16:03 +01:00
|
|
|
# Checks the license file and returns the UUID the book is licensed for.
|
2021-11-15 11:59:56 +01:00
|
|
|
# This is used so that the Calibre plugin can pick the correct decryption key
|
|
|
|
# first try without having to loop through all possible keys.
|
2022-02-23 00:16:03 +01:00
|
|
|
def adeptGetUserUUID(inpath):
|
2021-11-15 11:59:56 +01:00
|
|
|
with closing(ZipFile(open(inpath, 'rb'))) as inf:
|
|
|
|
try:
|
|
|
|
rights = etree.fromstring(inf.read('META-INF/rights.xml'))
|
|
|
|
adept = lambda tag: '{%s}%s' % (NSMAP['adept'], tag)
|
|
|
|
expr = './/%s' % (adept('user'),)
|
|
|
|
user_uuid = ''.join(rights.findtext(expr))
|
2022-02-23 00:16:03 +01:00
|
|
|
if user_uuid[:9] != "urn:uuid:":
|
2021-11-15 11:59:56 +01:00
|
|
|
return None
|
|
|
|
return user_uuid[9:]
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
2022-02-23 00:47:51 +01:00
|
|
|
def removeHardening(rights, keytype, keydata):
|
|
|
|
adept = lambda tag: '{%s}%s' % (NSMAP['adept'], tag)
|
|
|
|
textGetter = lambda name: ''.join(rights.findtext('.//%s' % (adept(name),)))
|
|
|
|
|
|
|
|
# Gather what we need, and generate the IV
|
|
|
|
resourceuuid = UUID(textGetter("resource"))
|
|
|
|
deviceuuid = UUID(textGetter("device"))
|
|
|
|
fullfillmentuuid = UUID(textGetter("fulfillment")[:36])
|
|
|
|
kekiv = UUID(int=resourceuuid.int ^ deviceuuid.int ^ fullfillmentuuid.int).bytes
|
|
|
|
|
|
|
|
# Derive kek from just "keytype"
|
|
|
|
rem = int(keytype, 10) % 16
|
|
|
|
H = hashlib.sha256(keytype.encode("ascii")).digest()
|
|
|
|
kek = H[2*rem : 16 + rem] + H[rem : 2*rem]
|
|
|
|
|
|
|
|
return unpad(AES.new(kek, AES.MODE_CBC, kekiv).decrypt(keydata), 16) # PKCS#7
|
|
|
|
|
2013-10-02 20:59:40 +02:00
|
|
|
def decryptBook(userkey, inpath, outpath):
|
|
|
|
with closing(ZipFile(open(inpath, 'rb'))) as inf:
|
2021-11-15 18:38:34 +01:00
|
|
|
namelist = inf.namelist()
|
2013-10-02 20:59:40 +02:00
|
|
|
if 'META-INF/rights.xml' not in namelist or \
|
|
|
|
'META-INF/encryption.xml' not in namelist:
|
2020-09-27 12:54:49 +02:00
|
|
|
print("{0:s} is DRM-free.".format(os.path.basename(inpath)))
|
2013-10-02 20:59:40 +02:00
|
|
|
return 1
|
|
|
|
for name in META_NAMES:
|
|
|
|
namelist.remove(name)
|
2013-04-05 18:44:48 +02:00
|
|
|
try:
|
2013-10-02 20:59:40 +02:00
|
|
|
rights = etree.fromstring(inf.read('META-INF/rights.xml'))
|
|
|
|
adept = lambda tag: '{%s}%s' % (NSMAP['adept'], tag)
|
|
|
|
expr = './/%s' % (adept('encryptedKey'),)
|
2022-02-23 00:47:51 +01:00
|
|
|
bookkeyelem = rights.find(expr)
|
|
|
|
bookkey = bookkeyelem.text
|
|
|
|
keytype = bookkeyelem.attrib.get('keyType', '0')
|
|
|
|
if len(bookkey) >= 172 and int(keytype, 10) > 2:
|
|
|
|
print("{0:s} is a secure Adobe Adept ePub with hardening.".format(os.path.basename(inpath)))
|
|
|
|
elif len(bookkey) == 172:
|
2021-12-23 11:29:58 +01:00
|
|
|
print("{0:s} is a secure Adobe Adept ePub.".format(os.path.basename(inpath)))
|
|
|
|
elif len(bookkey) == 64:
|
|
|
|
print("{0:s} is a secure Adobe PassHash (B&N) ePub.".format(os.path.basename(inpath)))
|
|
|
|
else:
|
|
|
|
print("{0:s} is not an Adobe-protected ePub!".format(os.path.basename(inpath)))
|
2013-10-02 20:59:40 +02:00
|
|
|
return 1
|
2021-12-23 11:29:58 +01:00
|
|
|
|
|
|
|
if len(bookkey) != 64:
|
2022-02-23 00:47:51 +01:00
|
|
|
# Normal or "hardened" Adobe ADEPT
|
2022-07-16 09:48:27 +02:00
|
|
|
rsakey = RSA.importKey(userkey) # parses the ASN1 structure
|
2022-02-23 00:16:03 +01:00
|
|
|
bookkey = base64.b64decode(bookkey)
|
2022-02-23 00:47:51 +01:00
|
|
|
if int(keytype, 10) > 2:
|
|
|
|
bookkey = removeHardening(rights, keytype, bookkey)
|
2022-02-23 00:16:03 +01:00
|
|
|
try:
|
|
|
|
bookkey = PKCS1_v1_5.new(rsakey).decrypt(bookkey, None) # automatically unpads
|
|
|
|
except ValueError:
|
|
|
|
bookkey = None
|
|
|
|
|
|
|
|
if bookkey is None:
|
|
|
|
print("Could not decrypt {0:s}. Wrong key".format(os.path.basename(inpath)))
|
|
|
|
return 2
|
|
|
|
else:
|
2021-12-23 11:29:58 +01:00
|
|
|
# Adobe PassHash / B&N
|
|
|
|
key = base64.b64decode(userkey)[:16]
|
2022-02-23 00:16:03 +01:00
|
|
|
bookkey = base64.b64decode(bookkey)
|
|
|
|
bookkey = unpad(AES.new(key, AES.MODE_CBC, b'\x00'*16).decrypt(bookkey), 16) # PKCS#7
|
2021-12-23 11:29:58 +01:00
|
|
|
|
2022-01-02 16:23:36 +01:00
|
|
|
if len(bookkey) > 16:
|
2021-01-28 13:06:59 +01:00
|
|
|
bookkey = bookkey[-16:]
|
2021-12-23 11:29:58 +01:00
|
|
|
|
2013-10-02 20:59:40 +02:00
|
|
|
encryption = inf.read('META-INF/encryption.xml')
|
2020-11-22 16:03:45 +01:00
|
|
|
decryptor = Decryptor(bookkey, encryption)
|
2013-10-02 20:59:40 +02:00
|
|
|
kwds = dict(compression=ZIP_DEFLATED, allowZip64=False)
|
|
|
|
with closing(ZipFile(open(outpath, 'wb'), 'w', **kwds)) as outf:
|
2021-11-15 18:38:34 +01:00
|
|
|
|
|
|
|
for path in (["mimetype"] + namelist):
|
2013-10-02 20:59:40 +02:00
|
|
|
data = inf.read(path)
|
|
|
|
zi = ZipInfo(path)
|
|
|
|
zi.compress_type=ZIP_DEFLATED
|
2021-11-15 18:38:34 +01:00
|
|
|
|
|
|
|
if path == "mimetype":
|
|
|
|
zi.compress_type = ZIP_STORED
|
|
|
|
|
|
|
|
elif path == "META-INF/encryption.xml":
|
|
|
|
# Check if there's still something in there
|
|
|
|
if (decryptor.check_if_remaining()):
|
|
|
|
data = decryptor.get_xml()
|
|
|
|
print("Adding encryption.xml for the remaining embedded files.")
|
|
|
|
# We removed DRM, but there's still stuff like obfuscated fonts.
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
2013-03-20 11:23:54 +01:00
|
|
|
try:
|
2013-10-02 20:59:40 +02:00
|
|
|
# get the file info, including time-stamp
|
|
|
|
oldzi = inf.getinfo(path)
|
|
|
|
# copy across useful fields
|
|
|
|
zi.date_time = oldzi.date_time
|
|
|
|
zi.comment = oldzi.comment
|
|
|
|
zi.extra = oldzi.extra
|
|
|
|
zi.internal_attr = oldzi.internal_attr
|
|
|
|
# external attributes are dependent on the create system, so copy both.
|
|
|
|
zi.external_attr = oldzi.external_attr
|
2022-08-06 13:53:03 +02:00
|
|
|
|
|
|
|
zi.volume = oldzi.volume
|
2013-10-02 20:59:40 +02:00
|
|
|
zi.create_system = oldzi.create_system
|
2022-08-06 13:53:03 +02:00
|
|
|
zi.create_version = oldzi.create_version
|
|
|
|
|
2021-11-15 18:38:34 +01:00
|
|
|
if any(ord(c) >= 128 for c in path) or any(ord(c) >= 128 for c in zi.comment):
|
|
|
|
# If the file name or the comment contains any non-ASCII char, set the UTF8-flag
|
|
|
|
zi.flag_bits |= 0x800
|
2013-10-02 20:59:40 +02:00
|
|
|
except:
|
2013-03-20 11:23:54 +01:00
|
|
|
pass
|
2022-08-06 13:53:03 +02:00
|
|
|
|
|
|
|
# Python 3 has a bug where the external_attr is reset to `0o600 << 16`
|
|
|
|
# if it's NULL, so we need a workaround:
|
|
|
|
if zi.external_attr == 0:
|
|
|
|
zi = ZeroedZipInfo(zi)
|
|
|
|
|
|
|
|
|
2021-11-15 18:38:34 +01:00
|
|
|
if path == "META-INF/encryption.xml":
|
|
|
|
outf.writestr(zi, data)
|
|
|
|
else:
|
|
|
|
outf.writestr(zi, decryptor.decrypt(path, data))
|
2012-12-19 14:48:11 +01:00
|
|
|
except:
|
2020-09-27 12:54:49 +02:00
|
|
|
print("Could not decrypt {0:s} because of an exception:\n{1:s}".format(os.path.basename(inpath), traceback.format_exc()))
|
2012-12-19 14:48:11 +01:00
|
|
|
return 2
|
2011-01-17 08:24:53 +01:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2013-03-26 17:38:18 +01:00
|
|
|
def cli_main():
|
2013-04-05 18:44:48 +02:00
|
|
|
sys.stdout=SafeUnbuffered(sys.stdout)
|
|
|
|
sys.stderr=SafeUnbuffered(sys.stderr)
|
2022-08-06 20:19:18 +02:00
|
|
|
argv=unicode_argv("ineptepub.py")
|
2011-01-17 08:24:53 +01:00
|
|
|
progname = os.path.basename(argv[0])
|
|
|
|
if len(argv) != 4:
|
2020-09-27 12:54:49 +02:00
|
|
|
print("usage: {0} <keyfile.der> <inbook.epub> <outbook.epub>".format(progname))
|
2011-01-17 08:24:53 +01:00
|
|
|
return 1
|
|
|
|
keypath, inpath, outpath = argv[1:]
|
2012-12-19 14:48:11 +01:00
|
|
|
userkey = open(keypath,'rb').read()
|
|
|
|
result = decryptBook(userkey, inpath, outpath)
|
|
|
|
if result == 0:
|
2020-09-27 12:54:49 +02:00
|
|
|
print("Successfully decrypted {0:s} as {1:s}".format(os.path.basename(inpath),os.path.basename(outpath)))
|
2012-12-19 14:48:11 +01:00
|
|
|
return result
|
2011-01-17 08:24:53 +01:00
|
|
|
|
2009-02-19 13:39:43 +01:00
|
|
|
def gui_main():
|
2013-04-05 18:44:48 +02:00
|
|
|
try:
|
2020-10-14 17:23:49 +02:00
|
|
|
import tkinter
|
2020-11-22 16:03:45 +01:00
|
|
|
import tkinter.constants
|
|
|
|
import tkinter.filedialog
|
|
|
|
import tkinter.messagebox
|
2013-04-05 18:44:48 +02:00
|
|
|
import traceback
|
|
|
|
except:
|
|
|
|
return cli_main()
|
2012-12-19 14:48:11 +01:00
|
|
|
|
2020-10-14 17:23:49 +02:00
|
|
|
class DecryptionDialog(tkinter.Frame):
|
2012-12-19 14:48:11 +01:00
|
|
|
def __init__(self, root):
|
2020-10-14 17:23:49 +02:00
|
|
|
tkinter.Frame.__init__(self, root, border=5)
|
|
|
|
self.status = tkinter.Label(self, text="Select files for decryption")
|
2020-11-22 16:03:45 +01:00
|
|
|
self.status.pack(fill=tkinter.constants.X, expand=1)
|
2020-10-14 17:23:49 +02:00
|
|
|
body = tkinter.Frame(self)
|
2020-11-22 16:03:45 +01:00
|
|
|
body.pack(fill=tkinter.constants.X, expand=1)
|
|
|
|
sticky = tkinter.constants.E + tkinter.constants.W
|
2012-12-19 14:48:11 +01:00
|
|
|
body.grid_columnconfigure(1, weight=2)
|
2020-10-14 17:23:49 +02:00
|
|
|
tkinter.Label(body, text="Key file").grid(row=0)
|
|
|
|
self.keypath = tkinter.Entry(body, width=30)
|
2012-12-19 14:48:11 +01:00
|
|
|
self.keypath.grid(row=0, column=1, sticky=sticky)
|
2020-09-27 12:54:49 +02:00
|
|
|
if os.path.exists("adeptkey.der"):
|
|
|
|
self.keypath.insert(0, "adeptkey.der")
|
2020-10-14 17:23:49 +02:00
|
|
|
button = tkinter.Button(body, text="...", command=self.get_keypath)
|
2012-12-19 14:48:11 +01:00
|
|
|
button.grid(row=0, column=2)
|
2020-10-14 17:23:49 +02:00
|
|
|
tkinter.Label(body, text="Input file").grid(row=1)
|
|
|
|
self.inpath = tkinter.Entry(body, width=30)
|
2012-12-19 14:48:11 +01:00
|
|
|
self.inpath.grid(row=1, column=1, sticky=sticky)
|
2020-10-14 17:23:49 +02:00
|
|
|
button = tkinter.Button(body, text="...", command=self.get_inpath)
|
2012-12-19 14:48:11 +01:00
|
|
|
button.grid(row=1, column=2)
|
2020-10-14 17:23:49 +02:00
|
|
|
tkinter.Label(body, text="Output file").grid(row=2)
|
|
|
|
self.outpath = tkinter.Entry(body, width=30)
|
2012-12-19 14:48:11 +01:00
|
|
|
self.outpath.grid(row=2, column=1, sticky=sticky)
|
2020-10-14 17:23:49 +02:00
|
|
|
button = tkinter.Button(body, text="...", command=self.get_outpath)
|
2012-12-19 14:48:11 +01:00
|
|
|
button.grid(row=2, column=2)
|
2020-10-14 17:23:49 +02:00
|
|
|
buttons = tkinter.Frame(self)
|
2012-12-19 14:48:11 +01:00
|
|
|
buttons.pack()
|
2020-10-14 17:23:49 +02:00
|
|
|
botton = tkinter.Button(
|
2020-09-27 12:54:49 +02:00
|
|
|
buttons, text="Decrypt", width=10, command=self.decrypt)
|
2020-11-22 16:03:45 +01:00
|
|
|
botton.pack(side=tkinter.constants.LEFT)
|
|
|
|
tkinter.Frame(buttons, width=10).pack(side=tkinter.constants.LEFT)
|
2020-10-14 17:23:49 +02:00
|
|
|
button = tkinter.Button(
|
2020-09-27 12:54:49 +02:00
|
|
|
buttons, text="Quit", width=10, command=self.quit)
|
2020-11-22 16:03:45 +01:00
|
|
|
button.pack(side=tkinter.constants.RIGHT)
|
2012-12-19 14:48:11 +01:00
|
|
|
|
|
|
|
def get_keypath(self):
|
2020-11-22 16:03:45 +01:00
|
|
|
keypath = tkinter.filedialog.askopenfilename(
|
2020-09-27 12:54:49 +02:00
|
|
|
parent=None, title="Select Adobe Adept \'.der\' key file",
|
|
|
|
defaultextension=".der",
|
2012-12-19 14:48:11 +01:00
|
|
|
filetypes=[('Adobe Adept DER-encoded files', '.der'),
|
|
|
|
('All Files', '.*')])
|
|
|
|
if keypath:
|
|
|
|
keypath = os.path.normpath(keypath)
|
2020-11-22 16:03:45 +01:00
|
|
|
self.keypath.delete(0, tkinter.constants.END)
|
2012-12-19 14:48:11 +01:00
|
|
|
self.keypath.insert(0, keypath)
|
|
|
|
return
|
|
|
|
|
|
|
|
def get_inpath(self):
|
2020-11-22 16:03:45 +01:00
|
|
|
inpath = tkinter.filedialog.askopenfilename(
|
2020-09-27 12:54:49 +02:00
|
|
|
parent=None, title="Select ADEPT-encrypted ePub file to decrypt",
|
|
|
|
defaultextension=".epub", filetypes=[('ePub files', '.epub')])
|
2012-12-19 14:48:11 +01:00
|
|
|
if inpath:
|
|
|
|
inpath = os.path.normpath(inpath)
|
2020-11-22 16:03:45 +01:00
|
|
|
self.inpath.delete(0, tkinter.constants.END)
|
2012-12-19 14:48:11 +01:00
|
|
|
self.inpath.insert(0, inpath)
|
|
|
|
return
|
|
|
|
|
|
|
|
def get_outpath(self):
|
2020-11-22 16:03:45 +01:00
|
|
|
outpath = tkinter.filedialog.asksaveasfilename(
|
2020-09-27 12:54:49 +02:00
|
|
|
parent=None, title="Select unencrypted ePub file to produce",
|
|
|
|
defaultextension=".epub", filetypes=[('ePub files', '.epub')])
|
2012-12-19 14:48:11 +01:00
|
|
|
if outpath:
|
|
|
|
outpath = os.path.normpath(outpath)
|
2020-11-22 16:03:45 +01:00
|
|
|
self.outpath.delete(0, tkinter.constants.END)
|
2012-12-19 14:48:11 +01:00
|
|
|
self.outpath.insert(0, outpath)
|
|
|
|
return
|
|
|
|
|
|
|
|
def decrypt(self):
|
|
|
|
keypath = self.keypath.get()
|
|
|
|
inpath = self.inpath.get()
|
|
|
|
outpath = self.outpath.get()
|
|
|
|
if not keypath or not os.path.exists(keypath):
|
2020-09-27 12:54:49 +02:00
|
|
|
self.status['text'] = "Specified key file does not exist"
|
2012-12-19 14:48:11 +01:00
|
|
|
return
|
|
|
|
if not inpath or not os.path.exists(inpath):
|
2020-09-27 12:54:49 +02:00
|
|
|
self.status['text'] = "Specified input file does not exist"
|
2012-12-19 14:48:11 +01:00
|
|
|
return
|
|
|
|
if not outpath:
|
2020-09-27 12:54:49 +02:00
|
|
|
self.status['text'] = "Output file not specified"
|
2012-12-19 14:48:11 +01:00
|
|
|
return
|
|
|
|
if inpath == outpath:
|
2020-09-27 12:54:49 +02:00
|
|
|
self.status['text'] = "Must have different input and output files"
|
2012-12-19 14:48:11 +01:00
|
|
|
return
|
|
|
|
userkey = open(keypath,'rb').read()
|
2020-09-27 12:54:49 +02:00
|
|
|
self.status['text'] = "Decrypting..."
|
2012-12-19 14:48:11 +01:00
|
|
|
try:
|
|
|
|
decrypt_status = decryptBook(userkey, inpath, outpath)
|
2020-05-08 17:35:01 +02:00
|
|
|
except Exception as e:
|
2020-09-27 12:54:49 +02:00
|
|
|
self.status['text'] = "Error: {0}".format(e.args[0])
|
2012-12-19 14:48:11 +01:00
|
|
|
return
|
|
|
|
if decrypt_status == 0:
|
2020-09-27 12:54:49 +02:00
|
|
|
self.status['text'] = "File successfully decrypted"
|
2012-12-19 14:48:11 +01:00
|
|
|
else:
|
2020-11-22 16:03:45 +01:00
|
|
|
self.status['text'] = "There was an error decrypting the file."
|
2012-12-19 14:48:11 +01:00
|
|
|
|
2020-10-14 17:23:49 +02:00
|
|
|
root = tkinter.Tk()
|
2020-09-27 12:54:49 +02:00
|
|
|
root.title("Adobe Adept ePub Decrypter v.{0}".format(__version__))
|
2009-02-19 13:39:43 +01:00
|
|
|
root.resizable(True, False)
|
2013-10-02 20:59:40 +02:00
|
|
|
root.minsize(300, 0)
|
2020-11-22 16:03:45 +01:00
|
|
|
DecryptionDialog(root).pack(fill=tkinter.constants.X, expand=1)
|
2009-02-19 13:39:43 +01:00
|
|
|
root.mainloop()
|
2009-09-01 18:02:35 +02:00
|
|
|
return 0
|
2009-02-19 13:39:43 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2010-02-14 16:47:48 +01:00
|
|
|
if len(sys.argv) > 1:
|
|
|
|
sys.exit(cli_main())
|
2009-02-19 13:39:43 +01:00
|
|
|
sys.exit(gui_main())
|