# Attempt to decrypt epub with each encryption key (generated or provided).
for keyname, userkey in dedrmprefs['bandnkeys'].items():
- keyname_masked = u"".join(("X" if (x.isdigit()) else x) for x in keyname)
+ keyname_masked = "".join(("X" if (x.isdigit()) else x) for x in keyname)
print("{0} v{1}: Trying Encryption key {2:s}".format(PLUGIN_NAME, PLUGIN_VERSION, keyname_masked))
of = self.temporary_file(".epub")
except:
print("{0} v{1}: Exception when getting default Adobe Key after {2:.1f} seconds".format(PLUGIN_NAME, PLUGIN_VERSION, time.time()-self.starttime))
traceback.print_exc()
- self.default_key = u""
+ self.default_key = ""
newkeys = []
for keyvalue in defaultkeys:
except:
print("{0} v{1}: Exception when getting default Adobe Key after {2:.1f} seconds".format(PLUGIN_NAME, PLUGIN_VERSION, time.time()-self.starttime))
traceback.print_exc()
- self.default_key = u""
+ self.default_key = ""
newkeys = []
for keyvalue in defaultkeys:
dedrmprefs = prefs.DeDRM_Prefs()
# Attempt to decrypt epub with each encryption key (generated or provided).
for keyname, userkey in dedrmprefs['ereaderkeys'].items():
- keyname_masked = u"".join(("X" if (x.isdigit()) else x) for x in keyname)
+ keyname_masked = "".join(("X" if (x.isdigit()) else x) for x in keyname)
print("{0} v{1}: Trying Encryption key {2:s}".format(PLUGIN_NAME, PLUGIN_VERSION, keyname_masked))
of = self.temporary_file(".pmlz")
import sys
-import Tkinter
-import Tkconstants
+import tkinter
+import tkinter.constants
-class ActivityBar(Tkinter.Frame):
+class ActivityBar(tkinter.Frame):
def __init__(self, master, length=300, height=20, barwidth=15, interval=50, bg='white', fillcolor='orchid1',\
- bd=2, relief=Tkconstants.GROOVE, *args, **kw):
- Tkinter.Frame.__init__(self, master, bg=bg, width=length, height=height, *args, **kw)
+ bd=2, relief=tkinter.constants.GROOVE, *args, **kw):
+ tkinter.Frame.__init__(self, master, bg=bg, width=length, height=height, *args, **kw)
self._master = master
self._interval = interval
self._maximum = length
stopx = self._maximum
# self._canv = Tkinter.Canvas(self, bg=self['bg'], width=self['width'], height=self['height'],\
# highlightthickness=0, relief='flat', bd=0)
- self._canv = Tkinter.Canvas(self, bg=self['bg'], width=self['width'], height=self['height'],\
+ self._canv = tkinter.Canvas(self, bg=self['bg'], width=self['width'], height=self['height'],\
highlightthickness=0, relief=relief, bd=bd)
self._canv.pack(fill='both', expand=1)
self._rect = self._canv.create_rectangle(0, 0, self._canv.winfo_reqwidth(), self._canv.winfo_reqheight(), fill=fillcolor, width=0)
# save to the specified file or directory
outpath = args[0]
if not os.path.isabs(outpath):
- outpath = os.path.abspath(outpath)
+ outpath = os.path.abspath(outpath)
else:
# save to the same directory as the script
outpath = os.path.dirname(argv[0])
self.blockSize = blockSize # blockSize is in bytes
self.padding = padding # change default to noPadding() to get normal ECB behavior
- assert( keySize%4==0 and NrTable[4].has_key(keySize/4)),'key size must be 16,20,24,29 or 32 bytes'
- assert( blockSize%4==0 and NrTable.has_key(blockSize/4)), 'block size must be 16,20,24,29 or 32 bytes'
+ assert( keySize%4==0 and keySize/4 in NrTable[4]),'key size must be 16,20,24,29 or 32 bytes'
+ assert( blockSize%4==0 and blockSize/4 in NrTable), 'block size must be 16,20,24,29 or 32 bytes'
self.Nb = self.blockSize/4 # Nb is number of columns of 32 bit words
self.Nk = keySize/4 # Nk is the key length in 32-bit words
print("Get backup.ab file using adb backup com.amazon.kindle for Android 4.0+.")
print("Otherwise extract AmazonSecureStorage.xml from /data/data/com.amazon.kindle/shared_prefs/AmazonSecureStorage.xml")
print("Or map_data_storage.db from /data/data/com.amazon.kindle/databases/map_data_storage.db")
- print(u"")
+ print("")
print("Usage:")
print(" {0:s} [-h] [-b <backup.ab>] [<outfile.k4a>]".format(progname))
import sys, os
import locale
import codecs
+import importlib
# get sys.argv arguments and encode them into utf-8
def unicode_argv():
# Remove Python executable and commands if present
start = argc.value - len(sys.argv)
return [argv[i] for i in
- xrange(start, argc.value)]
+ range(start, argc.value)]
# if we don't have any arguments at all, just pass back script name
# this should never happen
return ["DeDRM.py"]
else:
- argvencoding = sys.stdin.encoding
- if argvencoding == None:
- argvencoding = "utf-8"
- return arg
+ argvencoding = sys.stdin.encoding or "utf-8"
+ return [arg if isinstance(arg, str) else str(arg, argvencoding) for arg in sys.argv]
def add_cp65001_codec():
return
# Regenerate setdefaultencoding.
- reload(sys)
+ importlib.reload(sys)
sys.setdefaultencoding('utf-8')
for attr in dir(locale):
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
-#
+#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
-#
+#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
def BrowseCallback(hwnd, uMsg, lParam, lpData):
if uMsg == BFFM_INITIALIZED:
if actionButtonLabel:
- label = actionButtonLabel.decode('utf-8', 'replace')
+ label = str(actionButtonLabel, errors='replace')
user32.SendMessageW(hwnd, BFFM_SETOKTEXT, 0, label)
if cancelButtonLabel:
- label = cancelButtonLabel.decode('utf-8', 'replace')
+ label = str(cancelButtonLabel, errors='replace')
cancelButton = user32.GetDlgItem(hwnd, IDCANCEL)
if cancelButton:
user32.SetWindowTextW(cancelButton, label)
if windowTitle:
- title = windowTitle.decode('utf-8', 'replace')
+ title = str(windowTitle, errors='replace')
user32.SetWindowTextW(hwnd, title)
if defaultLocation:
user32.SendMessageW(hwnd, BFFM_SETSELECTIONW, 1, defaultLocation.replace('/', '\\'))
# Copy the HTML helpfile to the plugin directory each time the
# link is clicked in case the helpfile is updated in newer plugins.
file_path = os.path.join(config_dir, "plugins", "DeDRM", "help", help_file_name)
- with open(file_path,'wb') as f:
+ with open(file_path,'w') as f:
f.write(self.load_resource(help_file_name))
return file_path
url = 'file:///' + get_help_file_resource()
class ManageKeysDialog(QDialog):
- def __init__(self, parent, key_type_name, plugin_keys, create_key, keyfile_ext = u"", wineprefix = None):
+ def __init__(self, parent, key_type_name, plugin_keys, create_key, keyfile_ext = "", wineprefix = None):
QDialog.__init__(self,parent)
self.parent = parent
self.key_type_name = key_type_name
self.plugin_keys = plugin_keys
self.create_key = create_key
self.keyfile_ext = keyfile_ext
- self.import_key = (keyfile_ext != u"")
+ self.import_key = (keyfile_ext != "")
self.binary_file = (keyfile_ext == "der")
self.json_file = (keyfile_ext == "k4i")
self.android_file = (keyfile_ext == "k4a")
def getwineprefix(self):
if self.wineprefix is not None:
- return self.wp_lineedit.text().strip()
- return u""
+ return str(self.wp_lineedit.text()).strip()
+ return ""
def populate_list(self):
if type(self.plugin_keys) == dict:
new_key_value = d.key_value
if type(self.plugin_keys) == dict:
if new_key_value in self.plugin_keys.values():
- old_key_name = [name for name, value in self.plugin_keys.iteritems() if value == new_key_value][0]
+ old_key_name = [name for name, value in self.plugin_keys.items() if value == new_key_value][0]
info_dialog(None, "{0} {1}: Duplicate {2}".format(PLUGIN_NAME, PLUGIN_VERSION,self.key_type_name),
"The new {1} is the same as the existing {1} named <strong>{0}</strong> and has not been added.".format(old_key_name,self.key_type_name), show=True)
return
if d.result() != d.Accepted:
# rename cancelled or moot.
return
- keyname = self.listy.currentItem().text()
+ keyname = str(self.listy.currentItem().text())
if not question_dialog(self, "{0} {1}: Confirm Rename".format(PLUGIN_NAME, PLUGIN_VERSION), "Do you really want to rename the {2} named <strong>{0}</strong> to <strong>{1}</strong>?".format(keyname,d.key_name,self.key_type_name), show_copy_button=False, default_yes=False):
return
self.plugin_keys[d.key_name] = self.plugin_keys[keyname]
def delete_key(self):
if not self.listy.currentItem():
return
- keyname = self.listy.currentItem().text()
+ keyname = str(self.listy.currentItem().text())
if not question_dialog(self, "{0} {1}: Confirm Delete".format(PLUGIN_NAME, PLUGIN_VERSION), "Do you really want to delete the {1} <strong>{0}</strong>?".format(keyname, self.key_type_name), show_copy_button=False, default_yes=False):
return
if type(self.plugin_keys) == dict:
# link is clicked in case the helpfile is updated in newer plugins.
help_file_name = "{0}_{1}_Help.htm".format(PLUGIN_NAME, self.key_type_name)
file_path = os.path.join(config_dir, "plugins", "DeDRM", "help", help_file_name)
- with open(file_path,'wb') as f:
+ with open(file_path,'w') as f:
f.write(self.parent.load_resource(help_file_name))
return file_path
url = 'file:///' + get_help_file_resource()
with open(fpath,'rb') as keyfile:
new_key_value = keyfile.read()
if self.binary_file:
- new_key_value = new_key_value.hex()
+ new_key_value = new_key_value.encode('hex')
elif self.json_file:
new_key_value = json.loads(new_key_value)
elif self.android_file:
break
if not match:
if new_key_value in self.plugin_keys.values():
- old_key_name = [name for name, value in self.plugin_keys.iteritems() if value == new_key_value][0]
+ old_key_name = [name for name, value in self.plugin_keys.items() if value == new_key_value][0]
skipped += 1
info_dialog(None, "{0} {1}".format(PLUGIN_NAME, PLUGIN_VERSION),
"The key in file {0} is the same as the existing key <strong>{1}</strong> and has been skipped.".format(filename,old_key_name), show_copy_button=False, show=True)
counter += 1
self.plugin_keys[new_key_name] = new_key_value
- msg = u""
+ msg = ""
if counter+skipped > 1:
if counter > 0:
msg += "Imported <strong>{0:d}</strong> key {1}. ".format(counter, "file" if counter == 1 else "files")
r = error_dialog(None, "{0} {1}".format(PLUGIN_NAME, PLUGIN_VERSION),
_(errmsg), show=True, show_copy_button=False)
return
- keyname = self.listy.currentItem().text()
+ keyname = str(self.listy.currentItem().text())
unique_dlg_name = PLUGIN_NAME + "export {0} keys".format(self.key_type_name).replace(' ', '_') #takes care of automatically remembering last directory
caption = "Save {0} File as...".format(self.key_type_name)
filters = [("{0} Files".format(self.key_type_name), ["{0}".format(self.keyfile_ext)])]
defaultname = "{0}.{1}".format(keyname, self.keyfile_ext)
filename = choose_save_file(self, unique_dlg_name, caption, filters, all_files=False, initial_filename=defaultname)
if filename:
- with open(filename, 'wb') as fname:
+ with file(filename, 'wb') as fname:
if self.binary_file:
fname.write(self.plugin_keys[keyname].decode('hex'))
elif self.json_file:
- fname.write(json.dumps(self.plugin_keys[keyname]).encode())
+ fname.write(json.dumps(self.plugin_keys[keyname]))
elif self.android_file:
for key in self.plugin_keys[keyname]:
- fname.write(key.encode())
- fname.write(b"\n")
+ fname.write(key)
+ fname.write("\n")
else:
fname.write(self.plugin_keys[keyname])
self.resize(self.sizeHint())
def accept(self):
- if not self.key_ledit.text() or self.key_ledit.text().isspace():
+ if not str(self.key_ledit.text()) or str(self.key_ledit.text()).isspace():
errmsg = "Key name field cannot be empty!"
return error_dialog(None, "{0} {1}".format(PLUGIN_NAME, PLUGIN_VERSION),
_(errmsg), show=True, show_copy_button=False)
@property
def key_name(self):
- return self.key_ledit.text().strip()
+ return str(self.key_ledit.text()).strip()
layout = QVBoxLayout(self)
self.setLayout(layout)
- data_group_box = QGroupBox(u"", self)
+ data_group_box = QGroupBox("", self)
layout.addWidget(data_group_box)
data_group_box_layout = QVBoxLayout()
data_group_box.setLayout(data_group_box_layout)
name_group = QHBoxLayout()
data_group_box_layout.addLayout(name_group)
name_group.addWidget(QLabel("B&N/nook account email address:", self))
- self.name_ledit = QLineEdit(u"", self)
+ self.name_ledit = QLineEdit("", self)
self.name_ledit.setToolTip(_("<p>Enter your email address as it appears in your B&N " +
"account.</p>" +
"<p>It will only be used to generate this " +
ccn_group = QHBoxLayout()
data_group_box_layout.addLayout(ccn_group)
ccn_group.addWidget(QLabel("B&N/nook account password:", self))
- self.cc_ledit = QLineEdit(u"", self)
+ self.cc_ledit = QLineEdit("", self)
self.cc_ledit.setToolTip(_("<p>Enter the password " +
"for your B&N account.</p>" +
"<p>The password will only be used to generate this " +
key_group = QHBoxLayout()
data_group_box_layout.addLayout(key_group)
key_group.addWidget(QLabel("Retrieved key:", self))
- self.key_display = QLabel(u"", self)
+ self.key_display = QLabel("", self)
self.key_display.setToolTip(_("Click the Retrieve Key button to fetch your B&N encryption key from the B&N servers"))
key_group.addWidget(self.key_display)
self.retrieve_button = QtGui.QPushButton(self)
@property
def key_name(self):
- return self.key_ledit.text().strip()
+ return str(self.key_ledit.text()).strip()
@property
def key_value(self):
- return self.key_display.text().strip()
+ return str(self.key_display.text()).strip()
@property
def user_name(self):
- return self.name_ledit.text().strip().lower().replace(' ','')
+ return str(self.name_ledit.text()).strip().lower().replace(' ','')
@property
def cc_number(self):
- return self.cc_ledit.text().strip()
+ return str(self.cc_ledit.text()).strip()
def retrieve_key(self):
from calibre_plugins.dedrm.ignoblekeyfetch import fetch_key as fetch_bandn_key
layout = QVBoxLayout(self)
self.setLayout(layout)
- data_group_box = QGroupBox(u"", self)
+ data_group_box = QGroupBox("", self)
layout.addWidget(data_group_box)
data_group_box_layout = QVBoxLayout()
data_group_box.setLayout(data_group_box_layout)
name_group = QHBoxLayout()
data_group_box_layout.addLayout(name_group)
name_group.addWidget(QLabel("Your Name:", self))
- self.name_ledit = QLineEdit(u"", self)
+ self.name_ledit = QLineEdit("", self)
self.name_ledit.setToolTip("Enter the name for this eReader key, usually the name on your credit card.\nIt will only be used to generate this one-time key and won\'t be stored anywhere in calibre or on your computer.\n(ex: Mr Jonathan Q Smith)")
name_group.addWidget(self.name_ledit)
name_disclaimer_label = QLabel(_("(Will not be saved in configuration data)"), self)
ccn_group = QHBoxLayout()
data_group_box_layout.addLayout(ccn_group)
ccn_group.addWidget(QLabel("Credit Card#:", self))
- self.cc_ledit = QLineEdit(u"", self)
+ self.cc_ledit = QLineEdit("", self)
self.cc_ledit.setToolTip("<p>Enter the last 8 digits of credit card number for this eReader key.\nThey will only be used to generate this one-time key and won\'t be stored anywhere in calibre or on your computer.")
ccn_group.addWidget(self.cc_ledit)
ccn_disclaimer_label = QLabel(_('(Will not be saved in configuration data)'), self)
@property
def key_name(self):
- return self.key_ledit.text().strip()
+ return str(self.key_ledit.text()).strip()
@property
def key_value(self):
@property
def user_name(self):
- return self.name_ledit.text().strip().lower().replace(' ','')
+ return str(self.name_ledit.text()).strip().lower().replace(' ','')
@property
def cc_number(self):
- return self.cc_ledit.text().strip().replace(' ', '').replace('-','')
+ return str(self.cc_ledit.text()).strip().replace(' ', '').replace('-','')
def accept(self):
defaultkeys = adeptkeys()
else: # linux
- from wineutils import WineGetKeys
+ from .wineutils import WineGetKeys
scriptpath = os.path.join(parent.parent.alfdir,"adobekey.py")
defaultkeys = WineGetKeys(scriptpath, ".der",parent.getwineprefix())
self.default_key = defaultkeys[0]
except:
traceback.print_exc()
- self.default_key = u""
+ self.default_key = ""
self.button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
if len(self.default_key)>0:
- data_group_box = QGroupBox(u"", self)
+ data_group_box = QGroupBox("", self)
layout.addWidget(data_group_box)
data_group_box_layout = QVBoxLayout()
data_group_box.setLayout(data_group_box_layout)
@property
def key_name(self):
- return self.key_ledit.text().strip()
+ return str(self.key_ledit.text()).strip()
@property
def key_value(self):
defaultkeys = kindlekeys()
else: # linux
- from wineutils import WineGetKeys
+ from .wineutils import WineGetKeys
scriptpath = os.path.join(parent.parent.alfdir,"kindlekey.py")
defaultkeys = WineGetKeys(scriptpath, ".k4i",parent.getwineprefix())
self.default_key = defaultkeys[0]
except:
traceback.print_exc()
- self.default_key = u""
+ self.default_key = ""
self.button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
if len(self.default_key)>0:
- data_group_box = QGroupBox(u"", self)
+ data_group_box = QGroupBox("", self)
layout.addWidget(data_group_box)
data_group_box_layout = QVBoxLayout()
data_group_box.setLayout(data_group_box_layout)
@property
def key_name(self):
- return self.key_ledit.text().strip()
+ return str(self.key_ledit.text()).strip()
@property
def key_value(self):
layout = QVBoxLayout(self)
self.setLayout(layout)
- data_group_box = QGroupBox(u"", self)
+ data_group_box = QGroupBox("", self)
layout.addWidget(data_group_box)
data_group_box_layout = QVBoxLayout()
data_group_box.setLayout(data_group_box_layout)
@property
def key_name(self):
- return self.key_ledit.text().strip()
+ return str(self.key_ledit.text()).strip()
@property
def key_value(self):
- return self.key_ledit.text().replace(' ', '')
+ return str(self.key_ledit.text()).replace(' ', '')
def accept(self):
if len(self.key_name) == 0 or self.key_name.isspace():
self.setLayout(layout)
self.button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
- data_group_box = QGroupBox(u"", self)
+ data_group_box = QGroupBox("", self)
layout.addWidget(data_group_box)
data_group_box_layout = QVBoxLayout()
data_group_box.setLayout(data_group_box_layout)
add_btn.setToolTip("Import Kindle for Android backup file.")
add_btn.clicked.connect(self.get_android_file)
file_group.addWidget(add_btn)
- self.selected_file_name = QLabel(u"",self)
+ self.selected_file_name = QLabel("",self)
self.selected_file_name.setAlignment(Qt.AlignHCenter)
file_group.addWidget(self.selected_file_name)
key_group = QHBoxLayout()
data_group_box_layout.addLayout(key_group)
key_group.addWidget(QLabel("Unique Key Name:", self))
- self.key_ledit = QLineEdit(u"", self)
+ self.key_ledit = QLineEdit("", self)
self.key_ledit.setToolTip("<p>Enter an identifying name for the Android for Kindle key.")
key_group.addWidget(self.key_ledit)
#key_label = QLabel(_(''), self)
@property
def key_name(self):
- return self.key_ledit.text().strip()
+ return str(self.key_ledit.text()).strip()
@property
def file_name(self):
- return self.selected_file_name.text().strip()
+ return str(self.selected_file_name.text()).strip()
@property
def key_value(self):
filters = [("Kindle for Android backup files", ['db','ab','xml'])]
files = choose_files(self, unique_dlg_name, caption, filters, all_files=False)
self.serials_from_file = []
- file_name = u""
+ file_name = ""
if files:
# find the first selected file that yields some serial numbers
for filename in files:
layout = QVBoxLayout(self)
self.setLayout(layout)
- data_group_box = QGroupBox(u"", self)
+ data_group_box = QGroupBox("", self)
layout.addWidget(data_group_box)
data_group_box_layout = QVBoxLayout()
data_group_box.setLayout(data_group_box_layout)
@property
def key_name(self):
- return self.key_ledit.text().strip()
+ return str(self.key_ledit.text()).strip()
@property
def key_value(self):
- return self.key_ledit.text().strip()
+ return str(self.key_ledit.text()).strip()
def accept(self):
if len(self.key_name) == 0 or self.key_name.isspace():
if self.encoding == None:
self.encoding = "utf-8"
def write(self, data):
- if isinstance(data,unicode):
+ if isinstance(data, str):
data = data.encode(self.encoding,"replace")
- self.stream.write(data)
- self.stream.flush()
+ self.stream.buffer.write(data)
+ self.stream.buffer.flush()
+
def __getattr__(self, attr):
return getattr(self.stream, attr)
# Remove Python executable and commands if present
start = argc.value - len(sys.argv)
return [argv[i] for i in
- xrange(start, argc.value)]
+ range(start, argc.value)]
# if we don't have any arguments at all, just pass back script name
# this should never happen
return ["epubtest.py"]
else:
- argvencoding = sys.stdin.encoding
- if argvencoding == None:
- argvencoding = "utf-8"
- return arg
+ argvencoding = sys.stdin.encoding or "utf-8"
+ return [arg if isinstance(arg, str) else str(arg, argvencoding) for arg in sys.argv]
_FILENAME_LEN_OFFSET = 26
_EXTRA_LEN_OFFSET = 28
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
# For use with Topaz Scripts Version 2.6
-from __future__ import print_function
import sys
import csv
import os
# change the origin to minx, miny and calc max height and width
maxw = maxws[0] + xs[0] - minx
maxh = maxhs[0] + ys[0] - miny
- for j in xrange(0, len(xs)):
+ for j in range(0, len(xs)):
xs[j] = xs[j] - minx
ys[j] = ys[j] - miny
maxw = max( maxw, (maxws[j] + xs[j]) )
ifile.write('<!DOCTYPE svg PUBLIC "-//W3C/DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n')
ifile.write('<svg width="%dpx" height="%dpx" viewBox="0 0 %d %d" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">\n' % (math.floor(maxw/10), math.floor(maxh/10), maxw, maxh))
ifile.write('<defs>\n')
- for j in xrange(0,len(gdefs)):
+ for j in range(0,len(gdefs)):
ifile.write(gdefs[j])
ifile.write('</defs>\n')
- for j in xrange(0,len(gids)):
+ for j in range(0,len(gids)):
ifile.write('<use xlink:href="#gl%d" x="%d" y="%d" />\n' % (gids[j], xs[j], ys[j]))
ifile.write('</svg>')
ifile.close()
else:
end = min(self.docSize, end)
foundat = -1
- for j in xrange(pos, end):
+ for j in range(pos, end):
item = self.docList[j]
if item.find('=') >= 0:
(name, argres) = item.split('=',1)
if not makeImage :
# standard all word paragraph
- for wordnum in xrange(first, last):
+ for wordnum in range(first, last):
result.append(('ocr', wordnum))
return pclass, result
# by reverting to text based paragraph
if firstGlyph >= lastGlyph:
# revert to standard text based paragraph
- for wordnum in xrange(first, last):
+ for wordnum in range(first, last):
result.append(('ocr', wordnum))
return pclass, result
- for glyphnum in xrange(firstGlyph, lastGlyph):
+ for glyphnum in range(firstGlyph, lastGlyph):
glyphList.append(glyphnum)
# include any extratokens if they exist
(pos, sfg) = self.findinDoc('extratokens.firstGlyph',start,end)
(pos, slg) = self.findinDoc('extratokens.lastGlyph',start,end)
if (sfg != None) and (slg != None):
- for glyphnum in xrange(int(sfg), int(slg)):
+ for glyphnum in range(int(sfg), int(slg)):
glyphList.append(glyphnum)
num = self.svgcount
self.glyphs_to_image(glyphList)
result.append(('img' + word_class, int(argres)))
if (sp_first != -1) and (sp_last != -1):
- for wordnum in xrange(sp_first, sp_last):
+ for wordnum in range(sp_first, sp_last):
result.append(('ocr', wordnum))
sp_first = -1
sp_last = -1
if (gl_first != -1) and (gl_last != -1):
glyphList = []
- for glyphnum in xrange(gl_first, gl_last):
+ for glyphnum in range(gl_first, gl_last):
glyphList.append(glyphnum)
num = self.svgcount
self.glyphs_to_image(glyphList)
gl_last = -1
if (ws_first != -1) and (ws_last != -1):
- for wordnum in xrange(ws_first, ws_last):
+ for wordnum in range(ws_first, ws_last):
result.append(('ocr', wordnum))
ws_first = -1
ws_last = -1
cnt = len(pdesc)
- for j in xrange( 0, cnt) :
+ for j in range( 0, cnt) :
(wtype, num) = pdesc[j]
lstart = 0
cnt = len(pdesc)
- for j in xrange( 0, cnt) :
+ for j in range( 0, cnt) :
(wtype, num) = pdesc[j]
# process each region on the page and convert what you can to html
- for j in xrange(regcnt):
+ for j in range(regcnt):
(etype, start) = pageDesc[j]
(ntype, end) = pageDesc[j+1]
else:
end = min(self.docSize, end)
foundat = -1
- for j in xrange(pos, end):
+ for j in range(pos, end):
item = self.flatdoc[j]
if item.find('=') >= 0:
(name, argres) = item.split('=',1)
def getData(self, path):
result = None
cnt = len(self.flatdoc)
- for j in xrange(cnt):
+ for j in range(cnt):
item = self.flatdoc[j]
if item.find('=') >= 0:
(name, argt) = item.split('=')
result = argres
break
if (len(argres) > 0) :
- for j in xrange(0,len(argres)):
+ for j in range(0,len(argres)):
argres[j] = int(argres[j])
return result
name = item
argres = []
if (len(argres) > 0) :
- for j in xrange(0,len(argres)):
+ for j in range(0,len(argres)):
argres[j] = int(argres[j])
if (name.endswith(path)):
result = argres
def getDataTemp(self, path):
result = None
cnt = len(self.temp)
- for j in xrange(cnt):
+ for j in range(cnt):
item = self.temp[j]
if item.find('=') >= 0:
(name, argt) = item.split('=')
self.temp.pop(j)
break
if (len(argres) > 0) :
- for j in xrange(0,len(argres)):
+ for j in range(0,len(argres)):
argres[j] = int(argres[j])
return result
if (pp.gid != None):
mlst.append('<defs>\n')
gdefs = pp.getGlyphs()
- for j in xrange(0,len(gdefs)):
+ for j in range(0,len(gdefs)):
mlst.append(gdefs[j])
mlst.append('</defs>\n')
img = pp.getImages()
if (img != None):
- for j in xrange(0,len(img)):
+ for j in range(0,len(img)):
mlst.append(img[j])
if (pp.gid != None):
- for j in xrange(0,len(pp.gid)):
+ for j in range(0,len(pp.gid)):
mlst.append('<use xlink:href="#gl%d" x="%d" y="%d" />\n' % (pp.gid[j], pp.gx[j], pp.gy[j]))
if (img == None or len(img) == 0) and (pp.gid == None or len(pp.gid) == 0):
xpos = "%d" % (pp.pw // 3)
from calibre_plugins.dedrm import flatxml2svg
from calibre_plugins.dedrm import stylexml2css
else :
- import convert2xml
- import flatxml2html
- import flatxml2svg
- import stylexml2css
+ from . import convert2xml
+ from . import flatxml2html
+ from . import flatxml2svg
+ from . import stylexml2css
# global switch
buildXML = False
def gui_main():
try:
- import Tkinter
- import Tkconstants
- import tkFileDialog
- import tkMessageBox
+ import tkinter
+ import tkinter.constants
+ import tkinter.filedialog
+ import tkinter.messagebox
import traceback
except:
return cli_main()
- class DecryptionDialog(Tkinter.Frame):
+ class DecryptionDialog(tkinter.Frame):
def __init__(self, root):
- Tkinter.Frame.__init__(self, root, border=5)
- self.status = Tkinter.Label(self, text="Select files for decryption")
- self.status.pack(fill=Tkconstants.X, expand=1)
- body = Tkinter.Frame(self)
- body.pack(fill=Tkconstants.X, expand=1)
- sticky = Tkconstants.E + Tkconstants.W
+ tkinter.Frame.__init__(self, root, border=5)
+ self.status = tkinter.Label(self, text="Select files for decryption")
+ self.status.pack(fill=tkinter.constants.X, expand=1)
+ body = tkinter.Frame(self)
+ body.pack(fill=tkinter.constants.X, expand=1)
+ sticky = tkinter.constants.E + tkinter.constants.W
body.grid_columnconfigure(1, weight=2)
- Tkinter.Label(body, text="Key file").grid(row=0)
- self.keypath = Tkinter.Entry(body, width=30)
+ tkinter.Label(body, text="Key file").grid(row=0)
+ self.keypath = tkinter.Entry(body, width=30)
self.keypath.grid(row=0, column=1, sticky=sticky)
if os.path.exists("bnepubkey.b64"):
self.keypath.insert(0, "bnepubkey.b64")
- button = Tkinter.Button(body, text="...", command=self.get_keypath)
+ button = tkinter.Button(body, text="...", command=self.get_keypath)
button.grid(row=0, column=2)
- Tkinter.Label(body, text="Input file").grid(row=1)
- self.inpath = Tkinter.Entry(body, width=30)
+ tkinter.Label(body, text="Input file").grid(row=1)
+ self.inpath = tkinter.Entry(body, width=30)
self.inpath.grid(row=1, column=1, sticky=sticky)
- button = Tkinter.Button(body, text="...", command=self.get_inpath)
+ button = tkinter.Button(body, text="...", command=self.get_inpath)
button.grid(row=1, column=2)
- Tkinter.Label(body, text="Output file").grid(row=2)
- self.outpath = Tkinter.Entry(body, width=30)
+ tkinter.Label(body, text="Output file").grid(row=2)
+ self.outpath = tkinter.Entry(body, width=30)
self.outpath.grid(row=2, column=1, sticky=sticky)
- button = Tkinter.Button(body, text="...", command=self.get_outpath)
+ button = tkinter.Button(body, text="...", command=self.get_outpath)
button.grid(row=2, column=2)
- buttons = Tkinter.Frame(self)
+ buttons = tkinter.Frame(self)
buttons.pack()
- botton = Tkinter.Button(
+ botton = tkinter.Button(
buttons, text="Decrypt", width=10, command=self.decrypt)
- botton.pack(side=Tkconstants.LEFT)
- Tkinter.Frame(buttons, width=10).pack(side=Tkconstants.LEFT)
- button = Tkinter.Button(
+ botton.pack(side=tkinter.constants.LEFT)
+ tkinter.Frame(buttons, width=10).pack(side=tkinter.constants.LEFT)
+ button = tkinter.Button(
buttons, text="Quit", width=10, command=self.quit)
- button.pack(side=Tkconstants.RIGHT)
+ button.pack(side=tkinter.constants.RIGHT)
def get_keypath(self):
- keypath = tkFileDialog.askopenfilename(
+ keypath = tkinter.filedialog.askopenfilename(
parent=None, title="Select Barnes & Noble \'.b64\' key file",
defaultextension=".b64",
filetypes=[('base64-encoded files', '.b64'),
('All Files', '.*')])
if keypath:
keypath = os.path.normpath(keypath)
- self.keypath.delete(0, Tkconstants.END)
+ self.keypath.delete(0, tkinter.constants.END)
self.keypath.insert(0, keypath)
return
def get_inpath(self):
- inpath = tkFileDialog.askopenfilename(
+ inpath = tkinter.filedialog.askopenfilename(
parent=None, title="Select B&N-encrypted ePub file to decrypt",
defaultextension=".epub", filetypes=[('ePub files', '.epub')])
if inpath:
inpath = os.path.normpath(inpath)
- self.inpath.delete(0, Tkconstants.END)
+ self.inpath.delete(0, tkinter.constants.END)
self.inpath.insert(0, inpath)
return
def get_outpath(self):
- outpath = tkFileDialog.asksaveasfilename(
+ outpath = tkinter.filedialog.asksaveasfilename(
parent=None, title="Select unencrypted ePub file to produce",
defaultextension=".epub", filetypes=[('ePub files', '.epub')])
if outpath:
outpath = os.path.normpath(outpath)
- self.outpath.delete(0, Tkconstants.END)
+ self.outpath.delete(0, tkinter.constants.END)
self.outpath.insert(0, outpath)
return
else:
self.status['text'] = "The was an error decrypting the file."
- root = Tkinter.Tk()
+ root = tkinter.Tk()
root.title("Barnes & Noble ePub Decrypter v.{0}".format(__version__))
root.resizable(True, False)
root.minsize(300, 0)
- DecryptionDialog(root).pack(fill=Tkconstants.X, expand=1)
+ DecryptionDialog(root).pack(fill=tkinter.constants.X, expand=1)
root.mainloop()
return 0
"""
__license__ = 'GPL v3'
-__version__ = "1.1"
+__version__ = "2.0"
import sys
import os
if self.encoding == None:
self.encoding = "utf-8"
def write(self, data):
- if isinstance(data,str):
+ if isinstance(data, str):
data = data.encode(self.encoding,"replace")
self.stream.buffer.write(data)
self.stream.buffer.flush()
+
def __getattr__(self, attr):
return getattr(self.stream, attr)
def gui_main():
try:
- import Tkinter
- import Tkconstants
- import tkMessageBox
+ import tkinter
+ import tkinter.constants
+ import tkinter.messagebox
import traceback
except:
return cli_main()
- class ExceptionDialog(Tkinter.Frame):
+ class ExceptionDialog(tkinter.Frame):
def __init__(self, root, text):
- Tkinter.Frame.__init__(self, root, border=5)
- label = Tkinter.Label(self, text="Unexpected error:",
- anchor=Tkconstants.W, justify=Tkconstants.LEFT)
- label.pack(fill=Tkconstants.X, expand=0)
- self.text = Tkinter.Text(self)
- self.text.pack(fill=Tkconstants.BOTH, expand=1)
+ tkinter.Frame.__init__(self, root, border=5)
+ label = tkinter.Label(self, text="Unexpected error:",
+ anchor=tkinter.constants.W, justify=tkinter.constants.LEFT)
+ label.pack(fill=tkinter.constants.X, expand=0)
+ self.text = tkinter.Text(self)
+ self.text.pack(fill=tkinter.constants.BOTH, expand=1)
- self.text.insert(Tkconstants.END, text)
+ self.text.insert(tkinter.constants.END, text)
argv=unicode_argv()
- root = Tkinter.Tk()
+ root = tkinter.Tk()
root.withdraw()
progpath, progname = os.path.split(argv[0])
success = False
with open(outfile, 'w') as keyfileout:
keyfileout.write(key)
success = True
- tkMessageBox.showinfo(progname, "Key successfully retrieved to {0}".format(outfile))
+ tkinter.messagebox.showinfo(progname, "Key successfully retrieved to {0}".format(outfile))
except DrmException as e:
- tkMessageBox.showerror(progname, "Error: {0}".format(str(e)))
+ tkinter.messagebox.showerror(progname, "Error: {0}".format(str(e)))
except Exception:
root.wm_state('normal')
root.title(progname)
text = traceback.format_exc()
- ExceptionDialog(root, text).pack(fill=Tkconstants.BOTH, expand=1)
+ ExceptionDialog(root, text).pack(fill=tkinter.constants.BOTH, expand=1)
root.mainloop()
if not success:
return 1
if self.encoding == None:
self.encoding = "utf-8"
def write(self, data):
- if isinstance(data,bytes):
+ if isinstance(data, str):
data = data.encode(self.encoding,"replace")
- self.stream.write(data)
- self.stream.flush()
+ self.stream.buffer.write(data)
+ self.stream.buffer.flush()
+
def __getattr__(self, attr):
return getattr(self.stream, attr)
def fetch_key(email, password):
# change email and password to utf-8 if unicode
- if type(email)==bytes:
+ if type(email)==str:
email = email.encode('utf-8')
- if type(password)==bytes:
+ if type(password)==str:
password = password.encode('utf-8')
import random
def gui_main():
try:
- import Tkinter
- import tkFileDialog
- import Tkconstants
- import tkMessageBox
+ import tkinter
+ import tkinter.filedialog
+ import tkinter.constants
+ import tkinter.messagebox
import traceback
except:
return cli_main()
- class DecryptionDialog(Tkinter.Frame):
+ class DecryptionDialog(tkinter.Frame):
def __init__(self, root):
- Tkinter.Frame.__init__(self, root, border=5)
- self.status = Tkinter.Label(self, text="Enter parameters")
- self.status.pack(fill=Tkconstants.X, expand=1)
- body = Tkinter.Frame(self)
- body.pack(fill=Tkconstants.X, expand=1)
- sticky = Tkconstants.E + Tkconstants.W
+ tkinter.Frame.__init__(self, root, border=5)
+ self.status = tkinter.Label(self, text="Enter parameters")
+ self.status.pack(fill=tkinter.constants.X, expand=1)
+ body = tkinter.Frame(self)
+ body.pack(fill=tkinter.constants.X, expand=1)
+ sticky = tkinter.constants.E + tkinter.constants.W
body.grid_columnconfigure(1, weight=2)
- Tkinter.Label(body, text="Account email address").grid(row=0)
- self.name = Tkinter.Entry(body, width=40)
+ tkinter.Label(body, text="Account email address").grid(row=0)
+ self.name = tkinter.Entry(body, width=40)
self.name.grid(row=0, column=1, sticky=sticky)
- Tkinter.Label(body, text="Account password").grid(row=1)
- self.ccn = Tkinter.Entry(body, width=40)
+ tkinter.Label(body, text="Account password").grid(row=1)
+ self.ccn = tkinter.Entry(body, width=40)
self.ccn.grid(row=1, column=1, sticky=sticky)
- Tkinter.Label(body, text="Output file").grid(row=2)
- self.keypath = Tkinter.Entry(body, width=40)
+ tkinter.Label(body, text="Output file").grid(row=2)
+ self.keypath = tkinter.Entry(body, width=40)
self.keypath.grid(row=2, column=1, sticky=sticky)
self.keypath.insert(2, "bnepubkey.b64")
- button = Tkinter.Button(body, text="...", command=self.get_keypath)
+ button = tkinter.Button(body, text="...", command=self.get_keypath)
button.grid(row=2, column=2)
- buttons = Tkinter.Frame(self)
+ buttons = tkinter.Frame(self)
buttons.pack()
- botton = Tkinter.Button(
+ botton = tkinter.Button(
buttons, text="Fetch", width=10, command=self.generate)
- botton.pack(side=Tkconstants.LEFT)
- Tkinter.Frame(buttons, width=10).pack(side=Tkconstants.LEFT)
- button = Tkinter.Button(
+ botton.pack(side=tkinter.constants.LEFT)
+ tkinter.Frame(buttons, width=10).pack(side=tkinter.constants.LEFT)
+ button = tkinter.Button(
buttons, text="Quit", width=10, command=self.quit)
- button.pack(side=Tkconstants.RIGHT)
+ button.pack(side=tkinter.constants.RIGHT)
def get_keypath(self):
- keypath = tkFileDialog.asksaveasfilename(
+ keypath = tkinter.filedialog.asksaveasfilename(
parent=None, title="Select B&N ePub key file to produce",
defaultextension=".b64",
filetypes=[('base64-encoded files', '.b64'),
('All Files', '.*')])
if keypath:
keypath = os.path.normpath(keypath)
- self.keypath.delete(0, Tkconstants.END)
+ self.keypath.delete(0, tkinter.constants.END)
self.keypath.insert(0, keypath)
return
else:
self.status['text'] = "Keyfile fetch failed."
- root = Tkinter.Tk()
+ root = tkinter.Tk()
root.title("Barnes & Noble ePub Keyfile Fetch v.{0}".format(__version__))
root.resizable(True, False)
root.minsize(300, 0)
- DecryptionDialog(root).pack(fill=Tkconstants.X, expand=1)
+ DecryptionDialog(root).pack(fill=tkinter.constants.X, expand=1)
root.mainloop()
return 0
if self.encoding == None:
self.encoding = "utf-8"
def write(self, data):
- if isinstance(data,bytes):
+ if isinstance(data, str):
data = data.encode(self.encoding,"replace")
- self.stream.write(data)
- self.stream.flush()
+ self.stream.buffer.write(data)
+ self.stream.buffer.flush()
+
def __getattr__(self, attr):
return getattr(self.stream, attr)
def gui_main():
try:
- import Tkinter
- import Tkconstants
- import tkMessageBox
- import tkFileDialog
+ import tkinter
+ import tkinter.constants
+ import tkinter.messagebox
+ import tkinter.filedialog
import traceback
except:
return cli_main()
- class DecryptionDialog(Tkinter.Frame):
+ class DecryptionDialog(tkinter.Frame):
def __init__(self, root):
- Tkinter.Frame.__init__(self, root, border=5)
- self.status = Tkinter.Label(self, text="Enter parameters")
- self.status.pack(fill=Tkconstants.X, expand=1)
- body = Tkinter.Frame(self)
- body.pack(fill=Tkconstants.X, expand=1)
- sticky = Tkconstants.E + Tkconstants.W
+ tkinter.Frame.__init__(self, root, border=5)
+ self.status = tkinter.Label(self, text="Enter parameters")
+ self.status.pack(fill=tkinter.constants.X, expand=1)
+ body = tkinter.Frame(self)
+ body.pack(fill=tkinter.constants.X, expand=1)
+ sticky = tkinter.constants.E + tkinter.constants.W
body.grid_columnconfigure(1, weight=2)
- Tkinter.Label(body, text="Account Name").grid(row=0)
- self.name = Tkinter.Entry(body, width=40)
+ tkinter.Label(body, text="Account Name").grid(row=0)
+ self.name = tkinter.Entry(body, width=40)
self.name.grid(row=0, column=1, sticky=sticky)
- Tkinter.Label(body, text="CC#").grid(row=1)
- self.ccn = Tkinter.Entry(body, width=40)
+ tkinter.Label(body, text="CC#").grid(row=1)
+ self.ccn = tkinter.Entry(body, width=40)
self.ccn.grid(row=1, column=1, sticky=sticky)
- Tkinter.Label(body, text="Output file").grid(row=2)
- self.keypath = Tkinter.Entry(body, width=40)
+ tkinter.Label(body, text="Output file").grid(row=2)
+ self.keypath = tkinter.Entry(body, width=40)
self.keypath.grid(row=2, column=1, sticky=sticky)
self.keypath.insert(2, "bnepubkey.b64")
- button = Tkinter.Button(body, text="...", command=self.get_keypath)
+ button = tkinter.Button(body, text="...", command=self.get_keypath)
button.grid(row=2, column=2)
- buttons = Tkinter.Frame(self)
+ buttons = tkinter.Frame(self)
buttons.pack()
- botton = Tkinter.Button(
+ botton = tkinter.Button(
buttons, text="Generate", width=10, command=self.generate)
- botton.pack(side=Tkconstants.LEFT)
- Tkinter.Frame(buttons, width=10).pack(side=Tkconstants.LEFT)
- button = Tkinter.Button(
+ botton.pack(side=tkinter.constants.LEFT)
+ tkinter.Frame(buttons, width=10).pack(side=tkinter.constants.LEFT)
+ button = tkinter.Button(
buttons, text="Quit", width=10, command=self.quit)
- button.pack(side=Tkconstants.RIGHT)
+ button.pack(side=tkinter.constants.RIGHT)
def get_keypath(self):
- keypath = tkFileDialog.asksaveasfilename(
+ keypath = tkinter.filedialog.asksaveasfilename(
parent=None, title="Select B&N ePub key file to produce",
defaultextension=".b64",
filetypes=[('base64-encoded files', '.b64'),
('All Files', '.*')])
if keypath:
keypath = os.path.normpath(keypath)
- self.keypath.delete(0, Tkconstants.END)
+ self.keypath.delete(0, tkinter.constants.END)
self.keypath.insert(0, keypath)
return
open(keypath,'wb').write(userkey)
self.status['text'] = "Keyfile successfully generated"
- root = Tkinter.Tk()
+ root = tkinter.Tk()
if AES is None:
root.withdraw()
- tkMessageBox.showerror(
+ tkinter.messagebox.showerror(
"Ignoble EPUB Keyfile Generator",
"This script requires OpenSSL or PyCrypto, which must be installed "
"separately. Read the top-of-script comment for details.")
root.title("Barnes & Noble ePub Keyfile Generator v.{0}".format(__version__))
root.resizable(True, False)
root.minsize(300, 0)
- DecryptionDialog(root).pack(fill=Tkconstants.X, expand=1)
+ DecryptionDialog(root).pack(fill=tkinter.constants.X, expand=1)
root.mainloop()
return 0
"""
Decrypts Barnes & Noble encrypted PDF files.
"""
-from __future__ import print_function
__license__ = 'GPL v3'
-__version__ = "0.1"
+__version__ = "0.2"
import sys
import os
if self.encoding == None:
self.encoding = "utf-8"
def write(self, data):
- if isinstance(data,unicode):
+ if isinstance(data, str):
data = data.encode(self.encoding,"replace")
- self.stream.write(data)
- self.stream.flush()
+ self.stream.buffer.write(data)
+ self.stream.buffer.flush()
+
def __getattr__(self, attr):
return getattr(self.stream, attr)
# Remove Python executable and commands if present
start = argc.value - len(sys.argv)
return [argv[i] for i in
- xrange(start, argc.value)]
+ range(start, argc.value)]
return ["ignoblepdf.py"]
else:
argvencoding = sys.stdin.encoding or "utf-8"
ARC4, AES = _load_crypto()
-try:
- from cStringIO import StringIO
-except ImportError:
- try:
- from StringIO import StringIO
- except ImportError:
- from io import StringIO
+from io import BytesIO
# Do we generate cross reference streams on output?
# will get errors if the document is encrypted.
data = zlib.decompress(data)
elif f in LITERALS_LZW_DECODE:
- data = ''.join(LZWDecoder(StringIO(data)).run())
+ data = ''.join(LZWDecoder(BytesIO(data)).run())
elif f in LITERALS_ASCII85_DECODE:
data = ascii85decode(data)
elif f == LITERAL_CRYPT:
columns = int_value(params['Columns'])
buf = ''
ent0 = '\x00' * columns
- for i in xrange(0, len(data), columns+1):
+ for i in range(0, len(data), columns+1):
pred = data[i]
ent1 = data[i+1:i+1+columns]
if pred == '\x02':
(start, nobjs) = map(int, f)
except ValueError:
raise PDFNoValidXRef('Invalid line: %r: line=%r' % (parser, line))
- for objid in xrange(start, start+nobjs):
+ for objid in range(start, start+nobjs):
try:
(_, line) = parser.nextline()
except PSEOF:
def objids(self):
for first, size in self.index:
- for objid in xrange(first, first + size):
+ for objid in range(first, first + size):
yield objid
def load(self, parser, debug=0):
hash.update('ffffffff'.decode('hex'))
if 5 <= R:
# 8
- for _ in xrange(50):
+ for _ in range(50):
hash = hashlib.md5(hash.digest()[:length/8])
key = hash.digest()[:length/8]
if R == 2:
hash = hashlib.md5(self.PASSWORD_PADDING) # 2
hash.update(docid[0]) # 3
x = ARC4.new(key).decrypt(hash.digest()[:16]) # 4
- for i in xrange(1,19+1):
+ for i in range(1,19+1):
k = ''.join( chr(ord(c) ^ i) for c in key )
x = ARC4.new(k).decrypt(x)
u1 = x+x # 32bytes total
class PDFObjStrmParser(PDFParser):
def __init__(self, data, doc):
- PSStackParser.__init__(self, StringIO(data))
+ PSStackParser.__init__(self, BytesIO(data))
self.doc = doc
return
if not gen_xref_stm:
self.write('xref\n')
self.write('0 %d\n' % (maxobj + 1,))
- for objid in xrange(0, maxobj + 1):
+ for objid in range(0, maxobj + 1):
if objid in xrefs:
# force the genno to be 0
self.write("%010d 00000 n \n" % xrefs[objid][0])
def gui_main():
try:
- import Tkinter
- import Tkconstants
- import tkFileDialog
- import tkMessageBox
+ import tkinter
+ import tkinter.constants
+ import tkinter.filedialog
+ import tkinter.messagebox
import traceback
except:
return cli_main()
- class DecryptionDialog(Tkinter.Frame):
+ class DecryptionDialog(tkinter.Frame):
def __init__(self, root):
- Tkinter.Frame.__init__(self, root, border=5)
- self.status = Tkinter.Label(self, text="Select files for decryption")
- self.status.pack(fill=Tkconstants.X, expand=1)
- body = Tkinter.Frame(self)
- body.pack(fill=Tkconstants.X, expand=1)
- sticky = Tkconstants.E + Tkconstants.W
+ tkinter.Frame.__init__(self, root, border=5)
+ self.status = tkinter.Label(self, text="Select files for decryption")
+ self.status.pack(fill=tkinter.constants.X, expand=1)
+ body = tkinter.Frame(self)
+ body.pack(fill=tkinter.constants.X, expand=1)
+ sticky = tkinter.constants.E + tkinter.constants.W
body.grid_columnconfigure(1, weight=2)
- Tkinter.Label(body, text="Key file").grid(row=0)
- self.keypath = Tkinter.Entry(body, width=30)
+ tkinter.Label(body, text="Key file").grid(row=0)
+ self.keypath = tkinter.Entry(body, width=30)
self.keypath.grid(row=0, column=1, sticky=sticky)
if os.path.exists("bnpdfkey.b64"):
self.keypath.insert(0, "bnpdfkey.b64")
- button = Tkinter.Button(body, text="...", command=self.get_keypath)
+ button = tkinter.Button(body, text="...", command=self.get_keypath)
button.grid(row=0, column=2)
- Tkinter.Label(body, text="Input file").grid(row=1)
- self.inpath = Tkinter.Entry(body, width=30)
+ tkinter.Label(body, text="Input file").grid(row=1)
+ self.inpath = tkinter.Entry(body, width=30)
self.inpath.grid(row=1, column=1, sticky=sticky)
- button = Tkinter.Button(body, text="...", command=self.get_inpath)
+ button = tkinter.Button(body, text="...", command=self.get_inpath)
button.grid(row=1, column=2)
- Tkinter.Label(body, text="Output file").grid(row=2)
- self.outpath = Tkinter.Entry(body, width=30)
+ tkinter.Label(body, text="Output file").grid(row=2)
+ self.outpath = tkinter.Entry(body, width=30)
self.outpath.grid(row=2, column=1, sticky=sticky)
- button = Tkinter.Button(body, text="...", command=self.get_outpath)
+ button = tkinter.Button(body, text="...", command=self.get_outpath)
button.grid(row=2, column=2)
- buttons = Tkinter.Frame(self)
+ buttons = tkinter.Frame(self)
buttons.pack()
- botton = Tkinter.Button(
+ botton = tkinter.Button(
buttons, text="Decrypt", width=10, command=self.decrypt)
- botton.pack(side=Tkconstants.LEFT)
- Tkinter.Frame(buttons, width=10).pack(side=Tkconstants.LEFT)
- button = Tkinter.Button(
+ botton.pack(side=tkinter.constants.LEFT)
+ tkinter.Frame(buttons, width=10).pack(side=tkinter.constants.LEFT)
+ button = tkinter.Button(
buttons, text="Quit", width=10, command=self.quit)
- button.pack(side=Tkconstants.RIGHT)
+ button.pack(side=tkinter.constants.RIGHT)
def get_keypath(self):
- keypath = tkFileDialog.askopenfilename(
+ keypath = tkinter.filedialog.askopenfilename(
parent=None, title="Select Barnes & Noble \'.b64\' key file",
defaultextension=".b64",
filetypes=[('base64-encoded files', '.b64'),
('All Files', '.*')])
if keypath:
keypath = os.path.normpath(keypath)
- self.keypath.delete(0, Tkconstants.END)
+ self.keypath.delete(0, tkinter.constants.END)
self.keypath.insert(0, keypath)
return
def get_inpath(self):
- inpath = tkFileDialog.askopenfilename(
+ inpath = tkinter.filedialog.askopenfilename(
parent=None, title="Select B&N-encrypted PDF file to decrypt",
defaultextension=".pdf", filetypes=[('PDF files', '.pdf')])
if inpath:
inpath = os.path.normpath(inpath)
- self.inpath.delete(0, Tkconstants.END)
+ self.inpath.delete(0, tkinter.constants.END)
self.inpath.insert(0, inpath)
return
def get_outpath(self):
- outpath = tkFileDialog.asksaveasfilename(
+ outpath = tkinter.filedialog.asksaveasfilename(
parent=None, title="Select unencrypted PDF file to produce",
defaultextension=".pdf", filetypes=[('PDF files', '.pdf')])
if outpath:
outpath = os.path.normpath(outpath)
- self.outpath.delete(0, Tkconstants.END)
+ self.outpath.delete(0, tkinter.constants.END)
self.outpath.insert(0, outpath)
return
self.status['text'] = "The was an error decrypting the file."
- root = Tkinter.Tk()
+ root = tkinter.Tk()
if AES is None:
root.withdraw()
- tkMessageBox.showerror(
+ tkinter.messagebox.showerror(
"IGNOBLE PDF",
"This script requires OpenSSL or PyCrypto, which must be installed "
"separately. Read the top-of-script comment for details.")
root.title("Barnes & Noble PDF Decrypter v.{0}".format(__version__))
root.resizable(True, False)
root.minsize(370, 0)
- DecryptionDialog(root).pack(fill=Tkconstants.X, expand=1)
+ DecryptionDialog(root).pack(fill=tkinter.constants.X, expand=1)
root.mainloop()
return 0
if self.encoding == None:
self.encoding = "utf-8"
def write(self, data):
- if isinstance(data,bytes):
+ if isinstance(data, str):
data = data.encode(self.encoding,"replace")
- self.stream.write(data)
- self.stream.flush()
+ self.stream.buffer.write(data)
+ self.stream.buffer.flush()
+
def __getattr__(self, attr):
return getattr(self.stream, attr)
def gui_main():
try:
- import Tkinter
- import Tkconstants
- import tkFileDialog
- import tkMessageBox
+ import tkinter
+ import tkinter_constants
+ import tkinter_filedialog
+ import tkinter_messagebox
import traceback
except:
return cli_main()
- class DecryptionDialog(Tkinter.Frame):
+ class DecryptionDialog(tkinter.Frame):
def __init__(self, root):
- Tkinter.Frame.__init__(self, root, border=5)
- self.status = Tkinter.Label(self, text="Select files for decryption")
- self.status.pack(fill=Tkconstants.X, expand=1)
- body = Tkinter.Frame(self)
- body.pack(fill=Tkconstants.X, expand=1)
- sticky = Tkconstants.E + Tkconstants.W
+ tkinter.Frame.__init__(self, root, border=5)
+ self.status = tkinter.Label(self, text="Select files for decryption")
+ self.status.pack(fill=tkinter_constants.X, expand=1)
+ body = tkinter.Frame(self)
+ body.pack(fill=tkinter_constants.X, expand=1)
+ sticky = tkinter_constants.E + tkinter_constants.W
body.grid_columnconfigure(1, weight=2)
- Tkinter.Label(body, text="Key file").grid(row=0)
- self.keypath = Tkinter.Entry(body, width=30)
+ tkinter.Label(body, text="Key file").grid(row=0)
+ self.keypath = tkinter.Entry(body, width=30)
self.keypath.grid(row=0, column=1, sticky=sticky)
if os.path.exists("adeptkey.der"):
self.keypath.insert(0, "adeptkey.der")
- button = Tkinter.Button(body, text="...", command=self.get_keypath)
+ button = tkinter.Button(body, text="...", command=self.get_keypath)
button.grid(row=0, column=2)
- Tkinter.Label(body, text="Input file").grid(row=1)
- self.inpath = Tkinter.Entry(body, width=30)
+ tkinter.Label(body, text="Input file").grid(row=1)
+ self.inpath = tkinter.Entry(body, width=30)
self.inpath.grid(row=1, column=1, sticky=sticky)
- button = Tkinter.Button(body, text="...", command=self.get_inpath)
+ button = tkinter.Button(body, text="...", command=self.get_inpath)
button.grid(row=1, column=2)
- Tkinter.Label(body, text="Output file").grid(row=2)
- self.outpath = Tkinter.Entry(body, width=30)
+ tkinter.Label(body, text="Output file").grid(row=2)
+ self.outpath = tkinter.Entry(body, width=30)
self.outpath.grid(row=2, column=1, sticky=sticky)
- button = Tkinter.Button(body, text="...", command=self.get_outpath)
+ button = tkinter.Button(body, text="...", command=self.get_outpath)
button.grid(row=2, column=2)
- buttons = Tkinter.Frame(self)
+ buttons = tkinter.Frame(self)
buttons.pack()
- botton = Tkinter.Button(
+ botton = tkinter.Button(
buttons, text="Decrypt", width=10, command=self.decrypt)
- botton.pack(side=Tkconstants.LEFT)
- Tkinter.Frame(buttons, width=10).pack(side=Tkconstants.LEFT)
- button = Tkinter.Button(
+ botton.pack(side=tkinter_constants.LEFT)
+ tkinter.Frame(buttons, width=10).pack(side=tkinter_constants.LEFT)
+ button = tkinter.Button(
buttons, text="Quit", width=10, command=self.quit)
- button.pack(side=Tkconstants.RIGHT)
+ button.pack(side=tkinter_constants.RIGHT)
def get_keypath(self):
- keypath = tkFileDialog.askopenfilename(
+ keypath = tkinter_filedialog.askopenfilename(
parent=None, title="Select Adobe Adept \'.der\' key file",
defaultextension=".der",
filetypes=[('Adobe Adept DER-encoded files', '.der'),
('All Files', '.*')])
if keypath:
keypath = os.path.normpath(keypath)
- self.keypath.delete(0, Tkconstants.END)
+ self.keypath.delete(0, tkinter_constants.END)
self.keypath.insert(0, keypath)
return
def get_inpath(self):
- inpath = tkFileDialog.askopenfilename(
+ inpath = tkinter_filedialog.askopenfilename(
parent=None, title="Select ADEPT-encrypted ePub file to decrypt",
defaultextension=".epub", filetypes=[('ePub files', '.epub')])
if inpath:
inpath = os.path.normpath(inpath)
- self.inpath.delete(0, Tkconstants.END)
+ self.inpath.delete(0, tkinter_constants.END)
self.inpath.insert(0, inpath)
return
def get_outpath(self):
- outpath = tkFileDialog.asksaveasfilename(
+ outpath = tkinter_filedialog.asksaveasfilename(
parent=None, title="Select unencrypted ePub file to produce",
defaultextension=".epub", filetypes=[('ePub files', '.epub')])
if outpath:
outpath = os.path.normpath(outpath)
- self.outpath.delete(0, Tkconstants.END)
+ self.outpath.delete(0, tkinter_constants.END)
self.outpath.insert(0, outpath)
return
else:
self.status['text'] = "The was an error decrypting the file."
- root = Tkinter.Tk()
+ root = tkinter.Tk()
root.title("Adobe Adept ePub Decrypter v.{0}".format(__version__))
root.resizable(True, False)
root.minsize(300, 0)
- DecryptionDialog(root).pack(fill=Tkconstants.X, expand=1)
+ DecryptionDialog(root).pack(fill=tkinter_constants.X, expand=1)
root.mainloop()
return 0
def __init__(self, stream):
self.stream = stream
self.encoding = stream.encoding
- if self.encoding is None:
+ if self.encoding == None:
self.encoding = "utf-8"
def write(self, data):
- if isinstance(data,bytes):
+ if isinstance(data, str):
data = data.encode(self.encoding,"replace")
- self.stream.write(data)
- self.stream.flush()
+ self.stream.buffer.write(data)
+ self.stream.buffer.flush()
+
def __getattr__(self, attr):
return getattr(self.stream, attr)
range(start, argc.value)]
return ["ineptpdf.py"]
else:
- argvencoding = sys.stdin.encoding
- if argvencoding is None:
- argvencoding = "utf-8"
- return sys.argv
+ argvencoding = sys.stdin.encoding or "utf-8"
+ return [arg if isinstance(arg, str) else str(arg, argvencoding) for arg in sys.argv]
class ADEPTError(Exception):
ARC4, RSA, AES = _load_crypto()
-try:
- from cStringIO import StringIO
-except ImportError:
- try:
- from StringIO import StringIO
- except ImportError:
- from io import StringIO
+from io import BytesIO
# Do we generate cross reference streams on output?
if not l:
return default
elif l == 1:
- return s
+ return ord(s)
elif l == 2:
return struct.unpack('>H', s)[0]
elif l == 3:
- return struct.unpack('>L', b'\x00'+s)[0]
+ return struct.unpack('>L', '\x00'+s)[0]
elif l == 4:
return struct.unpack('>L', s)[0]
else:
name = []
for char in self.name:
if not char.isalnum():
- char = b'#%02x' % char
+ char = b'#%02x' % ord(char)
name.append(char)
return b'/%s' % ''.join(name)
# will get errors if the document is encrypted.
data = zlib.decompress(data)
elif f in LITERALS_LZW_DECODE:
- data = ''.join(LZWDecoder(StringIO(data)).run())
+ data = ''.join(LZWDecoder(BytesIO(data)).run())
elif f in LITERALS_ASCII85_DECODE:
data = ascii85decode(data)
elif f == LITERAL_CRYPT:
class PDFObjStrmParser(PDFParser):
def __init__(self, data, doc):
- PSStackParser.__init__(self, StringIO(data))
+ PSStackParser.__init__(self, BytesIO(data))
self.doc = doc
return
def gui_main():
try:
- import Tkinter
- import Tkconstants
- import tkFileDialog
- import tkMessageBox
+ import tkinter
+ import tkinter.constants
+ import tkinter.filedialog
+ import tkinter.messagebox
import traceback
except:
return cli_main()
- class DecryptionDialog(Tkinter.Frame):
+ class DecryptionDialog(tkinter.Frame):
def __init__(self, root):
- Tkinter.Frame.__init__(self, root, border=5)
- self.status = Tkinter.Label(self, text="Select files for decryption")
- self.status.pack(fill=Tkconstants.X, expand=1)
- body = Tkinter.Frame(self)
- body.pack(fill=Tkconstants.X, expand=1)
- sticky = Tkconstants.E + Tkconstants.W
+ tkinter.Frame.__init__(self, root, border=5)
+ self.status = tkinter.Label(self, text="Select files for decryption")
+ self.status.pack(fill=tkinter.constants.X, expand=1)
+ body = tkinter.Frame(self)
+ body.pack(fill=tkinter.constants.X, expand=1)
+ sticky = tkinter.constants.E + tkinter.constants.W
body.grid_columnconfigure(1, weight=2)
- Tkinter.Label(body, text="Key file").grid(row=0)
- self.keypath = Tkinter.Entry(body, width=30)
+ tkinter.Label(body, text="Key file").grid(row=0)
+ self.keypath = tkinter.Entry(body, width=30)
self.keypath.grid(row=0, column=1, sticky=sticky)
if os.path.exists("adeptkey.der"):
self.keypath.insert(0, "adeptkey.der")
- button = Tkinter.Button(body, text="...", command=self.get_keypath)
+ button = tkinter.Button(body, text="...", command=self.get_keypath)
button.grid(row=0, column=2)
- Tkinter.Label(body, text="Input file").grid(row=1)
- self.inpath = Tkinter.Entry(body, width=30)
+ tkinter.Label(body, text="Input file").grid(row=1)
+ self.inpath = tkinter.Entry(body, width=30)
self.inpath.grid(row=1, column=1, sticky=sticky)
- button = Tkinter.Button(body, text="...", command=self.get_inpath)
+ button = tkinter.Button(body, text="...", command=self.get_inpath)
button.grid(row=1, column=2)
- Tkinter.Label(body, text="Output file").grid(row=2)
- self.outpath = Tkinter.Entry(body, width=30)
+ tkinter.Label(body, text="Output file").grid(row=2)
+ self.outpath = tkinter.Entry(body, width=30)
self.outpath.grid(row=2, column=1, sticky=sticky)
- button = Tkinter.Button(body, text="...", command=self.get_outpath)
+ button = tkinter.Button(body, text="...", command=self.get_outpath)
button.grid(row=2, column=2)
- buttons = Tkinter.Frame(self)
+ buttons = tkinter.Frame(self)
buttons.pack()
- botton = Tkinter.Button(
+ botton = tkinter.Button(
buttons, text="Decrypt", width=10, command=self.decrypt)
- botton.pack(side=Tkconstants.LEFT)
- Tkinter.Frame(buttons, width=10).pack(side=Tkconstants.LEFT)
- button = Tkinter.Button(
+ botton.pack(side=tkinter.constants.LEFT)
+ tkinter.Frame(buttons, width=10).pack(side=tkinter.constants.LEFT)
+ button = tkinter.Button(
buttons, text="Quit", width=10, command=self.quit)
- button.pack(side=Tkconstants.RIGHT)
+ button.pack(side=tkinter.constants.RIGHT)
def get_keypath(self):
- keypath = tkFileDialog.askopenfilename(
+ keypath = tkinter.filedialog.askopenfilename(
parent=None, title="Select Adobe Adept \'.der\' key file",
defaultextension=".der",
filetypes=[('Adobe Adept DER-encoded files', '.der'),
('All Files', '.*')])
if keypath:
keypath = os.path.normpath(keypath)
- self.keypath.delete(0, Tkconstants.END)
+ self.keypath.delete(0, tkinter.constants.END)
self.keypath.insert(0, keypath)
return
def get_inpath(self):
- inpath = tkFileDialog.askopenfilename(
+ inpath = tkinter.filedialog.askopenfilename(
parent=None, title="Select ADEPT-encrypted PDF file to decrypt",
defaultextension=".pdf", filetypes=[('PDF files', '.pdf')])
if inpath:
inpath = os.path.normpath(inpath)
- self.inpath.delete(0, Tkconstants.END)
+ self.inpath.delete(0, tkinter.constants.END)
self.inpath.insert(0, inpath)
return
def get_outpath(self):
- outpath = tkFileDialog.asksaveasfilename(
+ outpath = tkinter.filedialog.asksaveasfilename(
parent=None, title="Select unencrypted PDF file to produce",
defaultextension=".pdf", filetypes=[('PDF files', '.pdf')])
if outpath:
outpath = os.path.normpath(outpath)
- self.outpath.delete(0, Tkconstants.END)
+ self.outpath.delete(0, tkinter.constants.END)
self.outpath.insert(0, outpath)
return
self.status['text'] = "The was an error decrypting the file."
- root = Tkinter.Tk()
+ root = tkinter.Tk()
if RSA is None:
root.withdraw()
- tkMessageBox.showerror(
+ tkinter.messagebox.showerror(
"INEPT PDF",
"This script requires OpenSSL or PyCrypto, which must be installed "
"separately. Read the top-of-script comment for details.")
root.title("Adobe Adept PDF Decrypter v.{0}".format(__version__))
root.resizable(True, False)
root.minsize(370, 0)
- DecryptionDialog(root).pack(fill=Tkconstants.X, expand=1)
+ DecryptionDialog(root).pack(fill=tkinter.constants.X, expand=1)
root.mainloop()
return 0
import os.path
import struct
-try:
- from cStringIO import StringIO
-except ImportError:
- try:
- from StringIO import StringIO
- except ImportError:
- from io import StringIO
+from io import BytesIO
from Crypto.Cipher import AES
from Crypto.Util.py3compat import bchr, bord
b = aes.decrypt(self.ciphertext)
b = pkcs7unpad(b, 16)
- self.drmkey = BinaryIonParser(StringIO(b))
+ self.drmkey = BinaryIonParser(BytesIO(b))
addprottable(self.drmkey)
_assert(self.drmkey.hasnext() and self.drmkey.next() == TID_LIST and self.drmkey.gettypename() == "com.amazon.drm.KeySet@1.0",
self.envelope.next()
field = self.envelope.getfieldname()
if field == "voucher":
- self.voucher = BinaryIonParser(StringIO(self.envelope.lobvalue()))
+ self.voucher = BinaryIonParser(BytesIO(self.envelope.lobvalue()))
addprottable(self.voucher)
continue
elif field != "strategy":
from calibre_plugins.dedrm import androidkindlekey
from calibre_plugins.dedrm import kfxdedrm
else:
- import mobidedrm
- import topazextract
- import kgenpids
- import androidkindlekey
- import kfxdedrm
+ from . import mobidedrm
+ from . import topazextract
+ from . import kgenpids
+ from . import androidkindlekey
+ from . import kfxdedrm
# Wrap a stream so that output gets flushed immediately
# and also make sure that any unicode strings get
if self.encoding == None:
self.encoding = "utf-8"
def write(self, data):
- if isinstance(data,bytes):
+ if isinstance(data, str):
data = data.encode(self.encoding,"replace")
- self.stream.write(data)
- self.stream.flush()
+ self.stream.buffer.write(data)
+ self.stream.buffer.flush()
+
def __getattr__(self, attr):
return getattr(self.stream, attr)
# and some improvements suggested by jhaisley
def cleanup_name(name):
# substitute filename unfriendly characters
- name = name.replace("<","[").replace(">","]").replace(" : "," – ").replace(": "," – ").replace(":","—").replace("/","_").replace("\\","_").replace("|","_").replace("\"","\'").replace("*","_").replace("?",u"")
+ name = name.replace("<","[").replace(">","]").replace(" : "," – ").replace(": "," – ").replace(":","—").replace("/","_").replace("\\","_").replace("|","_").replace("\"","\'").replace("*","_").replace("?","")
# white space to single space, delete leading and trailing while space
name = re.sub(r"\s", " ", name).strip()
# delete control characters
- name = u"".join(char for char in name if ord(char)>=32)
+ name = "".join(char for char in name if ord(char)>=32)
# delete non-ascii characters
- name = u"".join(char for char in name if ord(char)<=126)
+ name = "".join(char for char in name if ord(char)<=126)
# remove leading dots
while len(name)>0 and name[0] == ".":
name = name[1:]
import shutil
import zipfile
-try:
- from cStringIO import StringIO
-except ImportError:
- try:
- from StringIO import StringIO
- except ImportError:
- from io import StringIO
+from io import BytesIO
__license__ = 'GPL v3'
if self.voucher is None:
self.decrypt_voucher(totalpids)
print("Decrypting KFX DRMION: {0}".format(filename))
- outfile = StringIO()
- ion.DrmIon(StringIO(data[8:-8]), lambda name: self.voucher).parse(outfile)
+ outfile = BytesIO()
+ ion.DrmIon(BytesIO(data[8:-8]), lambda name: self.voucher).parse(outfile)
self.decrypted[filename] = outfile.getvalue()
if not self.decrypted:
continue
try:
- voucher = ion.DrmIonVoucher(StringIO(data), pid[:dsn_len], pid[dsn_len:])
+ voucher = ion.DrmIonVoucher(BytesIO(data), pid[:dsn_len], pid[dsn_len:])
voucher.parse()
voucher.decryptvoucher()
break
def encode(data, map):
result = ''
for char in data:
- value = char
+ value = ord(char)
Q = (value ^ 0x80) // len(map)
R = value % len(map)
- result += chr(map[Q])
- result += chr(map[R])
+ result += map[Q]
+ result += map[R]
return result
# Hash the bytes in data and then encode the digest with the characters in map
return primeList
# Encode the bytes in data with the characters in map
+# data and map should be byte arrays
def encode(data, map):
result = b''
for char in data:
value = char
Q = (value ^ 0x80) // len(map)
R = value % len(map)
- result += bytes(map[Q])
- result += bytes(map[R])
+ result += bytes([map[Q]])
+ result += bytes([map[R]])
return result
# Hash the bytes in data and then encode the digest with the characters in map
CryptUnprotectData = CryptUnprotectData()
# Returns Environmental Variables that contain unicode
+ # name must be unicode string, not byte string.
def getEnvironmentVariable(name):
import ctypes
n = ctypes.windll.kernel32.GetEnvironmentVariableW(name, None, 0)
if version == 5: # .kinf2011
added_entropy = build + guid
elif version == 6: # .kinf2018
- salt = str(0x6d8 * int(build)) + guid
+ salt = str(0x6d8 * int(build)).encode('utf-8') + guid
sp = GetUserName() + '+@#$%+' + GetIDString()
passwd = encode(SHA256(sp), charMap5)
key = KeyIVGen().pbkdf2(passwd, salt, 10000, 0x400)[:32] # this is very slow
if len(DB)>6:
# store values used in decryption
- DB['IDString'] = GetIDString()
- DB['UserName'] = GetUserName()
+ DB[b'IDString'] = GetIDString()
+ DB[b'UserName'] = GetUserName()
print("Decrypted key file using IDString '{0:s}' and UserName '{1:s}'".format(GetIDString(), GetUserName().encode('hex')))
else:
print("Couldn't decrypt file.")
b'krx.notebookexportplugin.data.encryption_key',\
b'proxy.http.password',\
b'proxy.http.username'
- ]
+ ]
with open(kInfoFile, 'rb') as infoReader:
filedata = infoReader.read()
data = filedata[:-1]
items = data.split(b'/')
IDStrings = GetIDStrings()
- print ("trying username ", GetUserName())
+ print ("trying username ", GetUserName(), " on file ", kInfoFile)
for IDString in IDStrings:
print ("trying IDString:",IDString)
try:
encryptedValue = decode(headerblob, charMap1)
#print ("encryptedvalue: ",encryptedValue)
cleartext = UnprotectHeaderData(encryptedValue)
- print ("cleartext: ",cleartext)
+ #print ("cleartext: ",cleartext)
# now extract the pieces in the same way
pattern = re.compile(rb'''\[Version:(\d+)\]\[Build:(\d+)\]\[Cksum:([^\]]+)\]\[Guid:([\{\}a-z0-9\-]+)\]''', re.IGNORECASE)
build = m.group(2)
guid = m.group(4)
- print ("version",version)
- print ("build",build)
- print ("guid",guid,"\n")
+ #print ("version",version)
+ #print ("build",build)
+ #print ("guid",guid,"\n")
if version == 5: # .kinf2011: identical to K4PC, except the build number gets multiplied
- entropy = bytes(0x2df * int(build)) + guid
+ entropy = str(0x2df * int(build)).encode('utf-8') + guid
cud = CryptUnprotectData(entropy,IDString)
- print ("entropy",entropy)
- print ("cud",cud)
+ #print ("entropy",entropy)
+ #print ("cud",cud)
elif version == 6: # .kinf2018: identical to K4PC
- salt = bytes(0x6d8 * int(build)) + guid
+ salt = str(0x6d8 * int(build)).encode('utf-8') + guid
sp = GetUserName() + b'+@#$%+' + IDString
passwd = encode(SHA256(sp), charMap5)
key = LibCrypto().keyivgen(passwd, salt, 10000, 0x400)[:32]
- print ("salt",salt)
- print ("sp",sp)
- print ("passwd",passwd)
- print ("key",key)
+ #print ("salt",salt)
+ #print ("sp",sp)
+ #print ("passwd",passwd)
+ #print ("key",key)
# loop through the item records until all are processed
while len(items) > 0:
pass
if len(DB)>6:
# store values used in decryption
- print("Decrypted key file using IDString '{0:s}' and UserName '{1:s}'".format(IDString, GetUserName()))
- DB['IDString'] = IDString
- DB['UserName'] = GetUserName()
+ print("Decrypted key file using IDString '{0:s}' and UserName '{1:s}'".format(IDString.decode('utf-8'), GetUserName().decode('utf-8')))
+ DB[b'IDString'] = IDString
+ DB[b'UserName'] = GetUserName()
else:
print("Couldn't decrypt file.")
DB = {}
if key:
# convert all values to hex, just in case.
for keyname in key:
- key[keyname]=key[keyname].encode('hex')
+ key[keyname]=key[keyname].hex().encode('utf-8')
keys.append(key)
return keys
if len(keys) > 0:
if not os.path.isdir(outpath):
outfile = outpath
- with file(outfile, 'w') as keyfileout:
+ with open(outfile, 'w') as keyfileout:
keyfileout.write(json.dumps(keys[0]))
print("Saved a key to {0}".format(outfile))
else:
outfile = os.path.join(outpath,"kindlekey{0:d}.k4i".format(keycount))
if not os.path.exists(outfile):
break
- with file(outfile, 'w') as keyfileout:
- keyfileout.write(json.dumps(key))
+ unikey = {k.decode("utf-8"):v.decode("utf-8") for k,v in key.items()}
+ with open(outfile, 'w') as keyfileout:
+ keyfileout.write(json.dumps(unikey))
print("Saved a key to {0}".format(outfile))
return True
return False
def gui_main():
try:
- import Tkinter
- import Tkconstants
- import tkMessageBox
+ import tkinter
+ import tkinter.constants
+ import tkinter.messagebox
import traceback
except:
return cli_main()
- class ExceptionDialog(Tkinter.Frame):
+ class ExceptionDialog(tkinter.Frame):
def __init__(self, root, text):
- Tkinter.Frame.__init__(self, root, border=5)
- label = Tkinter.Label(self, text="Unexpected error:",
- anchor=Tkconstants.W, justify=Tkconstants.LEFT)
- label.pack(fill=Tkconstants.X, expand=0)
- self.text = Tkinter.Text(self)
- self.text.pack(fill=Tkconstants.BOTH, expand=1)
+ tkinter.Frame.__init__(self, root, border=5)
+ label = tkinter.Label(self, text="Unexpected error:",
+ anchor=tkinter.constants.W, justify=tkinter.constants.LEFT)
+ label.pack(fill=tkinter.constants.X, expand=0)
+ self.text = tkinter.Text(self)
+ self.text.pack(fill=tkinter.constants.BOTH, expand=1)
- self.text.insert(Tkconstants.END, text)
+ self.text.insert(tkinter.constants.END, text)
argv=unicode_argv()
- root = Tkinter.Tk()
+ root = tkinter.Tk()
root.withdraw()
progpath, progname = os.path.split(argv[0])
success = False
if not os.path.exists(outfile):
break
- with file(outfile, 'w') as keyfileout:
+ with open(outfile, 'w') as keyfileout:
keyfileout.write(json.dumps(key))
success = True
- tkMessageBox.showinfo(progname, "Key successfully retrieved to {0}".format(outfile))
+ tkinter.messagebox.showinfo(progname, "Key successfully retrieved to {0}".format(outfile))
except DrmException as e:
- tkMessageBox.showerror(progname, "Error: {0}".format(str(e)))
+ tkinter.messagebox.showerror(progname, "Error: {0}".format(str(e)))
except Exception:
root.wm_state('normal')
root.title(progname)
text = traceback.format_exc()
- ExceptionDialog(root, text).pack(fill=Tkconstants.BOTH, expand=1)
+ ExceptionDialog(root, text).pack(fill=tkinter.constants.BOTH, expand=1)
root.mainloop()
if not success:
return 1
return 0
if __name__ == '__main__':
- print ("here")
if len(sys.argv) > 1:
sys.exit(cli_main())
sys.exit(gui_main())
# 0.5 moved unicode_argv call inside main for Windows DeDRM compatibility
# 1.0 Python 3 for calibre 5.0
-from __future__ import print_function
+
import sys
import binascii
if self.encoding == None:
self.encoding = "utf-8"
def write(self, data):
- if isinstance(data,bytes):
+ if isinstance(data, str):
data = data.encode(self.encoding,"replace")
- self.stream.write(data)
- self.stream.flush()
+ self.stream.buffer.write(data)
+ self.stream.buffer.flush()
+
def __getattr__(self, attr):
return getattr(self.stream, attr)
# this should never happen
return ["kindlepid.py"]
else:
- argvencoding = sys.stdin.encoding
- if argvencoding == None:
- argvencoding = "utf-8"
- return sys.argv
+ argvencoding = sys.stdin.encoding or "utf-8"
+ return [arg if isinstance(arg, str) else str(arg, argvencoding) for arg in sys.argv]
letters = 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789'
if __name__ == "__main__":
- #sys.stdout=SafeUnbuffered(sys.stdout)
- #sys.stderr=SafeUnbuffered(sys.stderr)
+ sys.stdout=SafeUnbuffered(sys.stdout)
+ sys.stderr=SafeUnbuffered(sys.stderr)
sys.exit(cli_main())
# 0.40 - moved unicode_argv call inside main for Windows DeDRM compatibility
# 0.41 - Fixed potential unicode problem in command line calls
# 0.42 - Added GPL v3 licence. updated/removed some print statements
-# 3.00 - Added Python 3 compatibility for calibre 5.0
+# 1.00 - Python 3 compatibility for calibre 5.0
import sys
import os
if self.encoding == None:
self.encoding = "utf-8"
def write(self, data):
- if isinstance(data,bytes):
+ if isinstance(data, str):
data = data.encode(self.encoding,"replace")
- self.stream.write(data)
- self.stream.flush()
+ self.stream.buffer.write(data)
+ self.stream.buffer.flush()
+
def __getattr__(self, attr):
return getattr(self.stream, attr)
# Implementation of Pukall Cipher 1
def PC1(key, src, decryption=True):
# if we can get it from alfcrypto, use that
- #try:
- # return Pukall_Cipher().PC1(key,src,decryption)
- #except NameError:
- # pass
- #except TypeError:
- # pass
+ try:
+ return Pukall_Cipher().PC1(key,src,decryption)
+ except NameError:
+ pass
+ except TypeError:
+ pass
# use slow python version, since Pukall_Cipher didn't load
sum1 = 0;
# -*- coding: utf-8 -*-
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
-from __future__ import print_function
+
import sys
import os
import re
+import traceback
import calibre_plugins.dedrm.ineptepub
import calibre_plugins.dedrm.ignobleepub
import calibre_plugins.dedrm.epubtest
import calibre_plugins.dedrm.ineptpdf
import calibre_plugins.dedrm.erdr2pml
import calibre_plugins.dedrm.k4mobidedrm
-import traceback
def decryptepub(infile, outdir, rscpath):
errlog = ''
#!/usr/bin/env python
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
-import Tkinter
-import Tkconstants
+import tkinter
+import tkinter.constants
# basic scrolled text widget
-class ScrolledText(Tkinter.Text):
+class ScrolledText(tkinter.Text):
def __init__(self, master=None, **kw):
- self.frame = Tkinter.Frame(master)
- self.vbar = Tkinter.Scrollbar(self.frame)
- self.vbar.pack(side=Tkconstants.RIGHT, fill=Tkconstants.Y)
+ self.frame = tkinter.Frame(master)
+ self.vbar = tkinter.Scrollbar(self.frame)
+ self.vbar.pack(side=tkinter.constants.RIGHT, fill=tkinter.constants.Y)
kw.update({'yscrollcommand': self.vbar.set})
- Tkinter.Text.__init__(self, self.frame, **kw)
- self.pack(side=Tkconstants.LEFT, fill=Tkconstants.BOTH, expand=True)
+ tkinter.Text.__init__(self, self.frame, **kw)
+ self.pack(side=tkinter.constants.LEFT, fill=tkinter.constants.BOTH, expand=True)
self.vbar['command'] = self.yview
# Copy geometry methods of self.frame without overriding Text
# methods = hack!
- text_meths = vars(Tkinter.Text).keys()
- methods = vars(Tkinter.Pack).keys() + vars(Tkinter.Grid).keys() + vars(Tkinter.Place).keys()
+ text_meths = list(vars(tkinter.Text).keys())
+ methods = list(vars(tkinter.Pack).keys()) + list(vars(tkinter.Grid).keys()) + list(vars(tkinter.Place).keys())
methods = set(methods).difference(text_meths)
for m in methods:
if m[0] != '_' and m != 'config' and m != 'configure':
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
# For use with Topaz Scripts Version 2.6
-from __future__ import print_function
+
import csv
import sys
import os
else:
end = min(cnt,end)
foundat = -1
- for j in xrange(pos, end):
+ for j in range(pos, end):
item = docList[j]
if item.find('=') >= 0:
(name, argres) = item.split('=',1)
# process each style converting what you can
if debug: print(' ', 'Processing styles.')
- for j in xrange(stylecnt):
+ for j in range(stylecnt):
if debug: print(' ', 'Processing style %d' %(j))
start = styleList[j]
end = styleList[j+1]
# 5.0 - Fixed potential unicode problem with command line interface
# 6.0 - Added Python 3 compatibility for calibre 5.0
-from __future__ import print_function
__version__ = '6.0'
import sys
except:
from alfcrypto import Topaz_Cipher
+# Wrap a stream so that output gets flushed immediately
+# and also make sure that any unicode strings get
+# encoded using "replace" before writing them.
class SafeUnbuffered:
def __init__(self, stream):
self.stream = stream
if self.encoding == None:
self.encoding = "utf-8"
def write(self, data):
- if isinstance(data,bytes):
+ if isinstance(data, str):
data = data.encode(self.encoding,"replace")
- self.stream.write(data)
- self.stream.flush()
+ self.stream.buffer.write(data)
+ self.stream.buffer.flush()
+
def __getattr__(self, attr):
return getattr(self.stream, attr)
# recursive zip creation support routine
def zipUpDir(myzip, tdir, localname):
currentdir = tdir
- if localname != u"":
+ if localname != "":
currentdir = os.path.join(currentdir,localname)
list = os.listdir(currentdir)
for file in list:
def uStrCmp (s1, s2, caseless=False):
import unicodedata as ud
- str1 = s1 if isinstance(s1, unicode) else s1.decode('utf-8')
- str2 = s2 if isinstance(s2, unicode) else s2.decode('utf-8')
+ str1 = s1 if isinstance(s1, str) else str(s1)
+ str2 = s2 if isinstance(s2, str) else str(s2)
if caseless:
return ud.normalize('NFC', str1.lower()) == ud.normalize('NFC', str2.lower())
else:
"""
Re-write zip (or ePub) fixing problems with file names (and mimetype entry).
"""
-from __future__ import print_function
+
__license__ = 'GPL v3'
__version__ = "1.1"