modernizing ineptepub.

decrypting as python2 work
failing with python3:
  File "ineptepub.py", line 424, in decryptBook
    bookkey = rsa.decrypt(bookkey.decode('base64'))
AttributeError: 'str' object has no attribute 'decode'
Python2
Rémi Vanicat 2020-05-08 17:35:01 +02:00
rodzic 2bb73584f2
commit 3166273622
1 zmienionych plików z 50 dodań i 46 usunięć

Wyświetl plik

@ -2,6 +2,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import with_statement from __future__ import with_statement
from __future__ import absolute_import
from __future__ import print_function
# ineptepub.pyw, version 6.6 # ineptepub.pyw, version 6.6
# Copyright © 2009-2010 by i♥cabbages # Copyright © 2009-2010 by i♥cabbages
@ -51,6 +53,8 @@ Decrypt Adobe Digital Editions encrypted ePub books.
__license__ = 'GPL v3' __license__ = 'GPL v3'
__version__ = "6.6" __version__ = "6.6"
import six
from six.moves import range
import sys import sys
import os import os
import traceback import traceback
@ -70,7 +74,7 @@ class SafeUnbuffered:
if self.encoding == None: if self.encoding == None:
self.encoding = "utf-8" self.encoding = "utf-8"
def write(self, data): def write(self, data):
if isinstance(data,unicode): if isinstance(data,six.text_type):
data = data.encode(self.encoding,"replace") data = data.encode(self.encoding,"replace")
self.stream.write(data) self.stream.write(data)
self.stream.flush() self.stream.flush()
@ -111,13 +115,13 @@ def unicode_argv():
# Remove Python executable and commands if present # Remove Python executable and commands if present
start = argc.value - len(sys.argv) start = argc.value - len(sys.argv)
return [argv[i] for i in return [argv[i] for i in
xrange(start, argc.value)] range(start, argc.value)]
return [u"ineptepub.py"] return [u"ineptepub.py"]
else: else:
argvencoding = sys.stdin.encoding argvencoding = sys.stdin.encoding
if argvencoding == None: if argvencoding == None:
argvencoding = "utf-8" argvencoding = "utf-8"
return [arg if (type(arg) == unicode) else unicode(arg,argvencoding) for arg in sys.argv] return [arg if (type(arg) == six.text_type) else six.text_type(arg,argvencoding) for arg in sys.argv]
class ADEPTError(Exception): class ADEPTError(Exception):
@ -315,12 +319,12 @@ def _load_crypto_pycrypto():
class RSA(object): class RSA(object):
def __init__(self, der): def __init__(self, der):
key = ASN1Parser([ord(x) for x in der]) key = ASN1Parser([ord(x) for x in der])
key = [key.getChild(x).value for x in xrange(1, 4)] key = [key.getChild(x).value for x in range(1, 4)]
key = [self.bytesToNumber(v) for v in key] key = [self.bytesToNumber(v) for v in key]
self._rsa = _RSA.construct(key) self._rsa = _RSA.construct(key)
def bytesToNumber(self, bytes): def bytesToNumber(self, bytes):
total = 0L total = 0
for byte in bytes: for byte in bytes:
total = (total << 8) + byte total = (total << 8) + byte
return total return total
@ -405,7 +409,7 @@ def decryptBook(userkey, inpath, outpath):
namelist = set(inf.namelist()) namelist = set(inf.namelist())
if 'META-INF/rights.xml' not in namelist or \ if 'META-INF/rights.xml' not in namelist or \
'META-INF/encryption.xml' not in namelist: 'META-INF/encryption.xml' not in namelist:
print u"{0:s} is DRM-free.".format(os.path.basename(inpath)) print(u"{0:s} is DRM-free.".format(os.path.basename(inpath)))
return 1 return 1
for name in META_NAMES: for name in META_NAMES:
namelist.remove(name) namelist.remove(name)
@ -415,12 +419,12 @@ def decryptBook(userkey, inpath, outpath):
expr = './/%s' % (adept('encryptedKey'),) expr = './/%s' % (adept('encryptedKey'),)
bookkey = ''.join(rights.findtext(expr)) bookkey = ''.join(rights.findtext(expr))
if len(bookkey) != 172: if len(bookkey) != 172:
print u"{0:s} is not a secure Adobe Adept ePub.".format(os.path.basename(inpath)) print(u"{0:s} is not a secure Adobe Adept ePub.".format(os.path.basename(inpath)))
return 1 return 1
bookkey = rsa.decrypt(bookkey.decode('base64')) bookkey = rsa.decrypt(bookkey.decode('base64'))
# Padded as per RSAES-PKCS1-v1_5 # Padded as per RSAES-PKCS1-v1_5
if bookkey[-17] != '\x00': if bookkey[-17] != '\x00':
print u"Could not decrypt {0:s}. Wrong key".format(os.path.basename(inpath)) print(u"Could not decrypt {0:s}. Wrong key".format(os.path.basename(inpath)))
return 2 return 2
encryption = inf.read('META-INF/encryption.xml') encryption = inf.read('META-INF/encryption.xml')
decryptor = Decryptor(bookkey[-16:], encryption) decryptor = Decryptor(bookkey[-16:], encryption)
@ -461,7 +465,7 @@ def decryptBook(userkey, inpath, outpath):
pass pass
outf.writestr(zi, decryptor.decrypt(path, data)) outf.writestr(zi, decryptor.decrypt(path, data))
except: except:
print u"Could not decrypt {0:s} because of an exception:\n{1:s}".format(os.path.basename(inpath), traceback.format_exc()) print(u"Could not decrypt {0:s} because of an exception:\n{1:s}".format(os.path.basename(inpath), traceback.format_exc()))
return 2 return 2
return 0 return 0
@ -472,90 +476,90 @@ def cli_main():
argv=unicode_argv() argv=unicode_argv()
progname = os.path.basename(argv[0]) progname = os.path.basename(argv[0])
if len(argv) != 4: if len(argv) != 4:
print u"usage: {0} <keyfile.der> <inbook.epub> <outbook.epub>".format(progname) print(u"usage: {0} <keyfile.der> <inbook.epub> <outbook.epub>".format(progname))
return 1 return 1
keypath, inpath, outpath = argv[1:] keypath, inpath, outpath = argv[1:]
userkey = open(keypath,'rb').read() userkey = open(keypath,'rb').read()
result = decryptBook(userkey, inpath, outpath) result = decryptBook(userkey, inpath, outpath)
if result == 0: if result == 0:
print u"Successfully decrypted {0:s} as {1:s}".format(os.path.basename(inpath),os.path.basename(outpath)) print(u"Successfully decrypted {0:s} as {1:s}".format(os.path.basename(inpath),os.path.basename(outpath)))
return result return result
def gui_main(): def gui_main():
try: try:
import Tkinter import six.moves.tkinter
import Tkconstants import six.moves.tkinter_constants
import tkFileDialog import six.moves.tkinter_filedialog
import tkMessageBox import six.moves.tkinter_messagebox
import traceback import traceback
except: except:
return cli_main() return cli_main()
class DecryptionDialog(Tkinter.Frame): class DecryptionDialog(six.moves.tkinter.Frame):
def __init__(self, root): def __init__(self, root):
Tkinter.Frame.__init__(self, root, border=5) six.moves.tkinter.Frame.__init__(self, root, border=5)
self.status = Tkinter.Label(self, text=u"Select files for decryption") self.status = six.moves.tkinter.Label(self, text=u"Select files for decryption")
self.status.pack(fill=Tkconstants.X, expand=1) self.status.pack(fill=six.moves.tkinter_constants.X, expand=1)
body = Tkinter.Frame(self) body = six.moves.tkinter.Frame(self)
body.pack(fill=Tkconstants.X, expand=1) body.pack(fill=six.moves.tkinter_constants.X, expand=1)
sticky = Tkconstants.E + Tkconstants.W sticky = six.moves.tkinter_constants.E + six.moves.tkinter_constants.W
body.grid_columnconfigure(1, weight=2) body.grid_columnconfigure(1, weight=2)
Tkinter.Label(body, text=u"Key file").grid(row=0) six.moves.tkinter.Label(body, text=u"Key file").grid(row=0)
self.keypath = Tkinter.Entry(body, width=30) self.keypath = six.moves.tkinter.Entry(body, width=30)
self.keypath.grid(row=0, column=1, sticky=sticky) self.keypath.grid(row=0, column=1, sticky=sticky)
if os.path.exists(u"adeptkey.der"): if os.path.exists(u"adeptkey.der"):
self.keypath.insert(0, u"adeptkey.der") self.keypath.insert(0, u"adeptkey.der")
button = Tkinter.Button(body, text=u"...", command=self.get_keypath) button = six.moves.tkinter.Button(body, text=u"...", command=self.get_keypath)
button.grid(row=0, column=2) button.grid(row=0, column=2)
Tkinter.Label(body, text=u"Input file").grid(row=1) six.moves.tkinter.Label(body, text=u"Input file").grid(row=1)
self.inpath = Tkinter.Entry(body, width=30) self.inpath = six.moves.tkinter.Entry(body, width=30)
self.inpath.grid(row=1, column=1, sticky=sticky) self.inpath.grid(row=1, column=1, sticky=sticky)
button = Tkinter.Button(body, text=u"...", command=self.get_inpath) button = six.moves.tkinter.Button(body, text=u"...", command=self.get_inpath)
button.grid(row=1, column=2) button.grid(row=1, column=2)
Tkinter.Label(body, text=u"Output file").grid(row=2) six.moves.tkinter.Label(body, text=u"Output file").grid(row=2)
self.outpath = Tkinter.Entry(body, width=30) self.outpath = six.moves.tkinter.Entry(body, width=30)
self.outpath.grid(row=2, column=1, sticky=sticky) self.outpath.grid(row=2, column=1, sticky=sticky)
button = Tkinter.Button(body, text=u"...", command=self.get_outpath) button = six.moves.tkinter.Button(body, text=u"...", command=self.get_outpath)
button.grid(row=2, column=2) button.grid(row=2, column=2)
buttons = Tkinter.Frame(self) buttons = six.moves.tkinter.Frame(self)
buttons.pack() buttons.pack()
botton = Tkinter.Button( botton = six.moves.tkinter.Button(
buttons, text=u"Decrypt", width=10, command=self.decrypt) buttons, text=u"Decrypt", width=10, command=self.decrypt)
botton.pack(side=Tkconstants.LEFT) botton.pack(side=six.moves.tkinter_constants.LEFT)
Tkinter.Frame(buttons, width=10).pack(side=Tkconstants.LEFT) six.moves.tkinter.Frame(buttons, width=10).pack(side=six.moves.tkinter_constants.LEFT)
button = Tkinter.Button( button = six.moves.tkinter.Button(
buttons, text=u"Quit", width=10, command=self.quit) buttons, text=u"Quit", width=10, command=self.quit)
button.pack(side=Tkconstants.RIGHT) button.pack(side=six.moves.tkinter_constants.RIGHT)
def get_keypath(self): def get_keypath(self):
keypath = tkFileDialog.askopenfilename( keypath = six.moves.tkinter_filedialog.askopenfilename(
parent=None, title=u"Select Adobe Adept \'.der\' key file", parent=None, title=u"Select Adobe Adept \'.der\' key file",
defaultextension=u".der", defaultextension=u".der",
filetypes=[('Adobe Adept DER-encoded files', '.der'), filetypes=[('Adobe Adept DER-encoded files', '.der'),
('All Files', '.*')]) ('All Files', '.*')])
if keypath: if keypath:
keypath = os.path.normpath(keypath) keypath = os.path.normpath(keypath)
self.keypath.delete(0, Tkconstants.END) self.keypath.delete(0, six.moves.tkinter_constants.END)
self.keypath.insert(0, keypath) self.keypath.insert(0, keypath)
return return
def get_inpath(self): def get_inpath(self):
inpath = tkFileDialog.askopenfilename( inpath = six.moves.tkinter_filedialog.askopenfilename(
parent=None, title=u"Select ADEPT-encrypted ePub file to decrypt", parent=None, title=u"Select ADEPT-encrypted ePub file to decrypt",
defaultextension=u".epub", filetypes=[('ePub files', '.epub')]) defaultextension=u".epub", filetypes=[('ePub files', '.epub')])
if inpath: if inpath:
inpath = os.path.normpath(inpath) inpath = os.path.normpath(inpath)
self.inpath.delete(0, Tkconstants.END) self.inpath.delete(0, six.moves.tkinter_constants.END)
self.inpath.insert(0, inpath) self.inpath.insert(0, inpath)
return return
def get_outpath(self): def get_outpath(self):
outpath = tkFileDialog.asksaveasfilename( outpath = six.moves.tkinter_filedialog.asksaveasfilename(
parent=None, title=u"Select unencrypted ePub file to produce", parent=None, title=u"Select unencrypted ePub file to produce",
defaultextension=u".epub", filetypes=[('ePub files', '.epub')]) defaultextension=u".epub", filetypes=[('ePub files', '.epub')])
if outpath: if outpath:
outpath = os.path.normpath(outpath) outpath = os.path.normpath(outpath)
self.outpath.delete(0, Tkconstants.END) self.outpath.delete(0, six.moves.tkinter_constants.END)
self.outpath.insert(0, outpath) self.outpath.insert(0, outpath)
return return
@ -579,7 +583,7 @@ def gui_main():
self.status['text'] = u"Decrypting..." self.status['text'] = u"Decrypting..."
try: try:
decrypt_status = decryptBook(userkey, inpath, outpath) decrypt_status = decryptBook(userkey, inpath, outpath)
except Exception, e: except Exception as e:
self.status['text'] = u"Error: {0}".format(e.args[0]) self.status['text'] = u"Error: {0}".format(e.args[0])
return return
if decrypt_status == 0: if decrypt_status == 0:
@ -587,11 +591,11 @@ def gui_main():
else: else:
self.status['text'] = u"The was an error decrypting the file." self.status['text'] = u"The was an error decrypting the file."
root = Tkinter.Tk() root = six.moves.tkinter.Tk()
root.title(u"Adobe Adept ePub Decrypter v.{0}".format(__version__)) root.title(u"Adobe Adept ePub Decrypter v.{0}".format(__version__))
root.resizable(True, False) root.resizable(True, False)
root.minsize(300, 0) root.minsize(300, 0)
DecryptionDialog(root).pack(fill=Tkconstants.X, expand=1) DecryptionDialog(root).pack(fill=six.moves.tkinter_constants.X, expand=1)
root.mainloop() root.mainloop()
return 0 return 0