2020-09-27 12:54:49 +02:00
|
|
|
#!/usr/bin/env python3
|
2013-01-19 15:50:57 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2021-12-23 11:29:58 +01:00
|
|
|
# ignoblekeyGenPassHash.py
|
2022-02-23 00:16:03 +01:00
|
|
|
# Copyright © 2009-2022 i♥cabbages, Apprentice Harper et al.
|
2013-01-19 15:50:57 +01:00
|
|
|
|
|
|
|
# Released under the terms of the GNU General Public Licence, version 3
|
|
|
|
# <http://www.gnu.org/licenses/>
|
|
|
|
|
2013-10-02 20:59:40 +02:00
|
|
|
# Windows users: Before running this program, you must first install Python.
|
|
|
|
# We recommend ActiveState Python 2.7.X for Windows (x86) from
|
|
|
|
# http://www.activestate.com/activepython/downloads.
|
|
|
|
# You must also install PyCrypto from
|
|
|
|
# http://www.voidspace.org.uk/python/modules.shtml#pycrypto
|
|
|
|
# (make certain to install the version for Python 2.7).
|
|
|
|
# Then save this script file as ignoblekeygen.pyw and double-click on it to run it.
|
2013-01-19 15:50:57 +01:00
|
|
|
#
|
2013-10-02 20:59:40 +02:00
|
|
|
# Mac OS X users: Save this script file as ignoblekeygen.pyw. You can run this
|
|
|
|
# program from the command line (python ignoblekeygen.pyw) or by double-clicking
|
2013-01-19 15:50:57 +01:00
|
|
|
# it when it has been associated with PythonLauncher.
|
|
|
|
|
|
|
|
# Revision history:
|
|
|
|
# 1 - Initial release
|
2013-10-02 20:59:40 +02:00
|
|
|
# 2 - Add OS X support by using OpenSSL when available (taken/modified from ineptepub v5)
|
|
|
|
# 2.1 - Allow Windows versions of libcrypto to be found
|
|
|
|
# 2.2 - On Windows try PyCrypto first and then OpenSSL next
|
|
|
|
# 2.3 - Modify interface to allow use of import
|
|
|
|
# 2.4 - Improvements to UI and now works in plugins
|
|
|
|
# 2.5 - Additional improvement for unicode and plugin support
|
|
|
|
# 2.6 - moved unicode_argv call inside main for Windows DeDRM compatibility
|
|
|
|
# 2.7 - Work if TkInter is missing
|
2015-08-02 12:09:35 +02:00
|
|
|
# 2.8 - Fix bug in stand-alone use (import tkFileDialog)
|
2020-09-26 22:22:47 +02:00
|
|
|
# 3.0 - Added Python 3 compatibility for calibre 5.0
|
2022-02-23 00:16:03 +01:00
|
|
|
# 3.1 - Remove OpenSSL support, only PyCryptodome is supported now
|
2013-01-19 15:50:57 +01:00
|
|
|
|
|
|
|
"""
|
2013-10-02 20:59:40 +02:00
|
|
|
Generate Barnes & Noble EPUB user key from name and credit card number.
|
2013-01-19 15:50:57 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
__license__ = 'GPL v3'
|
2022-02-23 00:16:03 +01:00
|
|
|
__version__ = "3.1"
|
2013-01-19 15:50:57 +01:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
2013-10-02 20:59:40 +02:00
|
|
|
import hashlib
|
2020-10-04 21:36:12 +02:00
|
|
|
import base64
|
2013-01-19 15:50:57 +01:00
|
|
|
|
2023-08-06 15:49:52 +02:00
|
|
|
#@@CALIBRE_COMPAT_CODE@@
|
|
|
|
|
2022-02-23 00:16:03 +01:00
|
|
|
try:
|
|
|
|
from Cryptodome.Cipher import AES
|
|
|
|
except ImportError:
|
|
|
|
from Crypto.Cipher import AES
|
|
|
|
|
2023-08-03 20:45:06 +02:00
|
|
|
from .utilities import SafeUnbuffered
|
2013-01-19 15:50:57 +01:00
|
|
|
|
2023-08-03 20:45:06 +02:00
|
|
|
from .argv_utils import unicode_argv
|
2013-01-19 15:50:57 +01:00
|
|
|
|
2013-10-02 20:59:40 +02:00
|
|
|
class IGNOBLEError(Exception):
|
2013-01-19 15:50:57 +01:00
|
|
|
pass
|
|
|
|
|
2013-10-02 20:59:40 +02:00
|
|
|
def normalize_name(name):
|
|
|
|
return ''.join(x for x in name.lower() if x != ' ')
|
|
|
|
|
|
|
|
|
|
|
|
def generate_key(name, ccn):
|
|
|
|
# remove spaces and case from name and CC numbers.
|
2020-10-04 21:36:12 +02:00
|
|
|
name = normalize_name(name)
|
|
|
|
ccn = normalize_name(ccn)
|
|
|
|
|
|
|
|
if type(name)==str:
|
2013-10-02 20:59:40 +02:00
|
|
|
name = name.encode('utf-8')
|
2020-10-04 21:36:12 +02:00
|
|
|
if type(ccn)==str:
|
2013-10-02 20:59:40 +02:00
|
|
|
ccn = ccn.encode('utf-8')
|
|
|
|
|
2020-10-04 21:36:12 +02:00
|
|
|
name = name + b'\x00'
|
|
|
|
ccn = ccn + b'\x00'
|
2013-10-02 20:59:40 +02:00
|
|
|
|
|
|
|
name_sha = hashlib.sha1(name).digest()[:16]
|
|
|
|
ccn_sha = hashlib.sha1(ccn).digest()[:16]
|
|
|
|
both_sha = hashlib.sha1(name + ccn).digest()
|
2022-02-23 00:16:03 +01:00
|
|
|
crypt = AES.new(ccn_sha, AES.MODE_CBC, name_sha).encrypt(both_sha + (b'\x0c' * 0x0c))
|
2013-10-02 20:59:40 +02:00
|
|
|
userkey = hashlib.sha1(crypt).digest()
|
2020-10-04 21:36:12 +02:00
|
|
|
return base64.b64encode(userkey)
|
2013-01-19 15:50:57 +01:00
|
|
|
|
|
|
|
|
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("ignoblekeyGenPassHash.py")
|
2013-01-19 15:50:57 +01:00
|
|
|
progname = os.path.basename(argv[0])
|
|
|
|
if len(argv) != 4:
|
2020-09-27 12:54:49 +02:00
|
|
|
print("usage: {0} <Name> <CC#> <keyfileout.b64>".format(progname))
|
2013-01-19 15:50:57 +01:00
|
|
|
return 1
|
2013-10-02 20:59:40 +02:00
|
|
|
name, ccn, keypath = argv[1:]
|
|
|
|
userkey = generate_key(name, ccn)
|
|
|
|
open(keypath,'wb').write(userkey)
|
|
|
|
return 0
|
|
|
|
|
2013-01-19 15:50:57 +01:00
|
|
|
|
|
|
|
def gui_main():
|
2013-04-05 18:44:48 +02:00
|
|
|
try:
|
2020-10-14 17:23:49 +02:00
|
|
|
import tkinter
|
|
|
|
import tkinter.constants
|
|
|
|
import tkinter.messagebox
|
|
|
|
import tkinter.filedialog
|
2013-04-05 18:44:48 +02:00
|
|
|
import traceback
|
|
|
|
except:
|
|
|
|
return cli_main()
|
2013-01-19 15:50:57 +01:00
|
|
|
|
2020-10-14 17:23:49 +02:00
|
|
|
class DecryptionDialog(tkinter.Frame):
|
2013-01-19 15:50:57 +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="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
|
2013-01-19 15:50:57 +01:00
|
|
|
body.grid_columnconfigure(1, weight=2)
|
2020-10-14 17:23:49 +02:00
|
|
|
tkinter.Label(body, text="Account Name").grid(row=0)
|
|
|
|
self.name = tkinter.Entry(body, width=40)
|
2013-10-02 20:59:40 +02:00
|
|
|
self.name.grid(row=0, column=1, sticky=sticky)
|
2020-10-14 17:23:49 +02:00
|
|
|
tkinter.Label(body, text="CC#").grid(row=1)
|
|
|
|
self.ccn = tkinter.Entry(body, width=40)
|
2013-10-02 20:59:40 +02:00
|
|
|
self.ccn.grid(row=1, column=1, sticky=sticky)
|
2020-10-14 17:23:49 +02:00
|
|
|
tkinter.Label(body, text="Output file").grid(row=2)
|
|
|
|
self.keypath = tkinter.Entry(body, width=40)
|
2013-10-02 20:59:40 +02:00
|
|
|
self.keypath.grid(row=2, column=1, sticky=sticky)
|
2020-09-27 12:54:49 +02:00
|
|
|
self.keypath.insert(2, "bnepubkey.b64")
|
2020-10-14 17:23:49 +02:00
|
|
|
button = tkinter.Button(body, text="...", command=self.get_keypath)
|
2013-01-19 15:50:57 +01:00
|
|
|
button.grid(row=2, column=2)
|
2020-10-14 17:23:49 +02:00
|
|
|
buttons = tkinter.Frame(self)
|
2013-01-19 15:50:57 +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="Generate", width=10, command=self.generate)
|
2020-10-14 17:23:49 +02:00
|
|
|
botton.pack(side=tkinter.constants.LEFT)
|
|
|
|
tkinter.Frame(buttons, width=10).pack(side=tkinter.constants.LEFT)
|
|
|
|
button = tkinter.Button(
|
2020-09-27 12:54:49 +02:00
|
|
|
buttons, text="Quit", width=10, command=self.quit)
|
2020-10-14 17:23:49 +02:00
|
|
|
button.pack(side=tkinter.constants.RIGHT)
|
2013-01-19 15:50:57 +01:00
|
|
|
|
|
|
|
def get_keypath(self):
|
2020-10-14 17:23:49 +02:00
|
|
|
keypath = tkinter.filedialog.asksaveasfilename(
|
2020-09-27 12:54:49 +02:00
|
|
|
parent=None, title="Select B&N ePub key file to produce",
|
|
|
|
defaultextension=".b64",
|
2013-10-02 20:59:40 +02:00
|
|
|
filetypes=[('base64-encoded files', '.b64'),
|
2013-01-19 15:50:57 +01:00
|
|
|
('All Files', '.*')])
|
|
|
|
if keypath:
|
|
|
|
keypath = os.path.normpath(keypath)
|
2020-10-14 17:23:49 +02:00
|
|
|
self.keypath.delete(0, tkinter.constants.END)
|
2013-01-19 15:50:57 +01:00
|
|
|
self.keypath.insert(0, keypath)
|
|
|
|
return
|
|
|
|
|
2013-10-02 20:59:40 +02:00
|
|
|
def generate(self):
|
|
|
|
name = self.name.get()
|
|
|
|
ccn = self.ccn.get()
|
2013-01-19 15:50:57 +01:00
|
|
|
keypath = self.keypath.get()
|
2013-10-02 20:59:40 +02:00
|
|
|
if not name:
|
2020-09-27 12:54:49 +02:00
|
|
|
self.status['text'] = "Name not specified"
|
2013-01-19 15:50:57 +01:00
|
|
|
return
|
2013-10-02 20:59:40 +02:00
|
|
|
if not ccn:
|
2020-09-27 12:54:49 +02:00
|
|
|
self.status['text'] = "Credit card number not specified"
|
2013-01-19 15:50:57 +01:00
|
|
|
return
|
2013-10-02 20:59:40 +02:00
|
|
|
if not keypath:
|
2020-09-27 12:54:49 +02:00
|
|
|
self.status['text'] = "Output keyfile path not specified"
|
2013-01-19 15:50:57 +01:00
|
|
|
return
|
2020-09-27 12:54:49 +02:00
|
|
|
self.status['text'] = "Generating..."
|
2013-01-19 15:50:57 +01:00
|
|
|
try:
|
2013-10-02 20:59:40 +02:00
|
|
|
userkey = generate_key(name, ccn)
|
2020-09-26 22:22:47 +02:00
|
|
|
except Exception as e:
|
2020-09-27 12:54:49 +02:00
|
|
|
self.status['text'] = "Error: (0}".format(e.args[0])
|
2013-01-19 15:50:57 +01:00
|
|
|
return
|
2013-10-02 20:59:40 +02:00
|
|
|
open(keypath,'wb').write(userkey)
|
2020-09-27 12:54:49 +02:00
|
|
|
self.status['text'] = "Keyfile successfully generated"
|
2013-01-19 15:50:57 +01:00
|
|
|
|
2020-10-14 17:23:49 +02:00
|
|
|
root = tkinter.Tk()
|
2020-09-27 12:54:49 +02:00
|
|
|
root.title("Barnes & Noble ePub Keyfile Generator v.{0}".format(__version__))
|
2013-01-19 15:50:57 +01:00
|
|
|
root.resizable(True, False)
|
|
|
|
root.minsize(300, 0)
|
2020-10-14 17:23:49 +02:00
|
|
|
DecryptionDialog(root).pack(fill=tkinter.constants.X, expand=1)
|
2013-01-19 15:50:57 +01:00
|
|
|
root.mainloop()
|
|
|
|
return 0
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
sys.exit(cli_main())
|
|
|
|
sys.exit(gui_main())
|